w3resource

Python: Print all even numbers from a given numbers list in the same order and stop the printing if any numbers that come after 237 in the sequence

Python Basic: Exercise-28 with Solution

Write a Python program to print out all even numbers from a given numbers list in the same order and stop the printing if any numbers that come after 237 in the sequence.

Sample numbers list:

numbers = [    
    386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345, 
    399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217, 
    815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717, 
    958,743, 527
    ]

Sample Solution:-

Python Code:

numbers = [    
    386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345, 
    399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217, 
    815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717, 
    958,743, 527
    ]

for x in numbers:
    if x == 237:
        print(x)
        break;
    elif x % 2 == 0:
        print(x)
		

Sample Output:

386                                                                                                           
462                                                                                                           
418                                                                                                           
344                                                                                                           
236                                                                                                           
566                                                                                                           
978                                                                                                           
328                                                                                                           
162                                                                                                           
758                                                                                                           
918  
237                                                                                                 

Flowchart:

Flowchart: Print all even numbers from a given numbers list in the same order and stop the printing if any numbers that come after 237 in the sequence.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to concatenate all elements in a list into a string and return it.
Next: Write a Python program to print out a set containing all the colors from a list which are not present in another list.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Try-catch-else construct:

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")