6 August 2014
For loop in Python loops through a sequence of statements and iterates the code with current variable.
Source code viewer
- # Loop through letters of string.
- for letter in 'String':
- print('Current letter:', letter)
- # Loop through array.
- cars = ['mitsubishi', 'toyota', 'jeep']
- for car in cars:
- print('Current car:', car)
- # Use else in for loop.
- for num in range(1, 20):
- for i in range(2, num):
- if num % i == 0:
- j = num / i
- print('%d equals %d * %d' % (num, i, j))
- print(num, 'is a prime number')
- # Early stop of loop.
- for x in range(3):
- if x == 1:
- breakProgramming Language: Python