Mastering Multiple Conditions in Python For Loops
rajneesh
4 min read
- python

If you want to use a for loop with multiple conditional statements, then you are it right post because i am going to explain you how to use multiple conditional statements in a for loop in Python with an example. I got a method for Python documentation. In Python documentation, it is very complex, i am going to explain in very easy miner. I am 100% sure that by using this example, you can easily understand this concept.
introduction
For loop is used for iterating over a sequence like lists, tuples, strings, and more. we can use multiple conditional statement inside the for loop. it is a powerful method by which you can filter or process elements based on complex condition. most of time, we want to execute any code after some Conditions inside the for loop. for that you can use this methods.
Let’s dive into it how you can use multiple conditions in for
loops in Python.
Basics for loop Structure
First of all, see how to use for loop. Here is their structure:
for item in iterable:
# code block to execute
In this loop:
iterable: it can be a list, tuple, string, or any iterable object.
item: it represent each value of iterable object that when we loop over it.

for loop with condition
use condition with for loop we can use if statement inside the for loop block. it allow you to filter items based on the condition.
let see a simple example where we can print only even number for a list of numbers.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] #here is over list where we want to iterate
for num in numbers:
if num % 2 == 0:
print(num)
In this code:
We have a list of numbers. On that number list, we can iterate over it and check if we divide the number by two and its reminder is equal to zero, then we can print the number.
for loop with multiple conditions
to use multiple conditional statements, we can use logical operators "and, or" and "not" to combine the multiple statement. its allow you to filter more complex condition inside the for loop block.
let see a simple example where we can print only even number which is greater then 5 inside the numbers list.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for num in numbers:
if num % 2 == 0 and num > 5: # to use multiple condition statment we can use "and" operators with your canditions
print(num)
In this updated code:
in this code, we updated a smeller thing and added one more condition, then number greater then 5.
use of continue and break
break statement is used for stop the loop before it has looped through all the items and continue statement is use for stop the current iteration of the loop, and continue with the next.
Here’s is the example of continue and break statement.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for num in numbers:
if num == 8:
break # this will use to break the loop on a certain condition. in this case we are stoping loop it the value of 8.
if num % 2 != 0:
continue # Skip an condition using "continue" keyword. in this case we are skiping odd numbers.
print(num)
In this code:
we have list of numbers If the number is odd, then its continue the loop and not print the number, and if num is 8 its stop the loop.
Conclusion
Now you know that. How to use multiple conditional statement with for loop in effective way by combining the multiple logical statement like "and or" and "not" and you have control on loop to break and continue it any point. if you like my post then please like share and subscribe. if you want to learn mern stack then i have also created a complete learning roadmap You can follow this link .