All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Python For Loop With Condition

Last Updated : Mar 11, 2024

Python For Loop With Condition

In this article we will show you the solution of python for loop with condition, you can iterate via a series of elements in Python the use of the for loop, which is a robust construct.

It offers you the capacity to time and again carry out a series of obligations based on a particular condition.

The for loop executes a block of code once for every new release even as iterating over every element in a series, like a list or a text.

To regulate the loop's behaviour and define when it should end, you can use a condition.

You can be flexible and tailor the iteration procedure to meet your particular needs by using a for loop with a condition.

It assists you in processing—if necessary—only those components that meet the specified requirement. Now move to the concept of python for loop with condition.

Step By Step Guide On Python For Loop With Condition :-

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = []
odd_numbers = []
for num in numbers:
    if num % 2 == 0:
print("Even numbers:")
for even_num in even_numbers:
print("Odd numbers:")
for odd_num in odd_numbers:
sum_of_even_numbers = sum(even_numbers)
average_of_odd_numbers = sum(odd_numbers) / len(odd_numbers)
print("Sum of even numbers:", sum_of_even_numbers)
print("Average of odd numbers:", average_of_odd_numbers)
  1. We start through defining a list of numbers from 1 to 10 in this code.
  2. In order to report the even and ordinary numbers, respectively, we moreover create empty lists, even_numbers & odd_numbers.
  3. The subsequent element to do is to apply a for loop to time and again iterate over every variety in the numbers list.
  4. Using the modulo operator %, we determine whether or not the quantity is divisible by way of 2 for every generation.
  5. The quantity is added to the listing of even numbers if the remainder is zero, which shows that it's far an even variety.
  6. If the remainder isn't zero, the range is taken into consideration unusual and is delivered to the list of abnormal numbers.
  7. We divide the integers into their appropriate categories in this manner.
  8. To indicate the beginning of displaying the even numbers, we print the header "Even numbers:" after the loop.
  9. Then, each even number in the even_numbers list is iterated over and printed using a different for loop.
  10. Similar to that, we print the heading "Odd numbers:" and print every odd number inside the list of odd_numbers one at a time using a for loop.
  11. The sum() function, which accepts the even_numbers list as enter, is then used to compute the sum of the even numbers.
  12. The sum_of_even_numbers variable holds the outcome.
  13. The average of the odd numbers is then determined by adding up the values in the odd_numbers list using the sum() function and dividing the result by the list's length, which is determined using the len() function.
  14. The average_of_odd_numbers variable contains the average.
  15. The average of the odd numbers and the total of the even numbers are printed as the calculations' final results.

Conclusion :-

Thus, we have successfully acquired the knowledge of the for loop with condition in Python.

Additionally, we determined that the for loop with a circumstance permits us to iterate over a listing of factors and take precise moves in reaction to a given condition.

It offers us freedom in handling the execution's go with the flow and lets in us to method best the portions that comply with the requirement.

I hope this article on python for loop with condition helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪