Skip to main content

Python for loop syntax

  • numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    for x in numbers:
      print(x)
    
    For loop syntax in python, for loop is used for iterating over a sequence that can be either a list, a tuple, a dictionary, a set, or a string. You have to just pass the comma separated value as a list and you can also pass string value. With the for loop we can execute a set of statements, once for each item in a list.
  • for letter in 'Python language':     
       print 'Current Letter :', letter
    
    Python for loop on string, you can use for loop on string. In python strings are iterable objects, they contain a sequence of characters. You can print each character of a string one by one and whitespace is also treated as a character in for loop.
  • list_of_lists = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]]
    for list in list_of_lists:
        for x in list:
            print(x)
    
    Python for loop over Lists of lists, you can use for loop on lists of lists.
  • Back to code snippet queries related python