Python For Loop Tutorial With Examples and Range/Xrange Functions. Just use a for loop, and break to end it: If you must have a one-liner (which would be counter to Python's philosophy, where readability matters), use the next() function and a generator expression: which will set i to None if there is no such matching element. Let’s go into this useful Python feature with a few more examples. Rather than iterating through a range(), you can define a list and iterate through that list. Is the bullet train in China typically cheaper than taking a domestic flight? But instead of calling append() function, we will use List comprehension to iterate over the sequence and add each number at the end of the empty list. Write the Sieve of Eratosthenes. Read a text file in Python. One-Liner #1: To input space separated integers in a list: Run this program with "Sea Rose" as input: $ python word_count.py