for loop in python
The for loop is a common iterator in Python. Python for loops iterate over an object until it is complete. For example, you can iterate over the contents of a list or a string. It can step through the items in an ordered sequence or other iterable objects.
The for loop statement supports strings, lists, tuples, and other built-in iterables as well as new user-defined objects.
Syntax
for iterating_var in sequence: print(iterating_var)
Looping through a String
for letter in "codejagd": print(letter)
Output
c o d e j a g d
For Loop Example
friends = ['Ross', 'Jim', 'Pat', 'Steve'] for friend in friends: print("Hello " + friend)
Output
Hello Ross Hello Jim Hello Pat Hello Steve
for in loop python
The range() Function
We can generate a sequence using the range()
function. The range(10)
will generate numbers from 0 to 9 (10 numbers).
Syntax
range( begin, end, step )
Here ,
begin
is the initial value given in the range; the default value becomes zero if this is not included.end
is the value that comes after the last value in the range; the end value is not deleted.step
indicates the amount to increment or decrement; it defaults to 1 (increments by one) if the change parameter is omitted.
for number in range(7): print(number)
Output
0 1 2 3 4 5 6
The range()
function defaults to as a starting value. Now , lets specify the start value in range()
for number in range(2, 7): print(number)
Output
2 3 4 5 6
The range()
function defaults to increment the sequence by 1
.However it is possible to specify the increment value by adding a third parameter.
for number in range(1, 9, 2): print(number)
Output
1 3 5 7
for python loop
Nested For Loops
A nested loop is a loop inside a loop. The inner loop will be executed one time for each iteration of the outer loop.
teams = ['LiverPool', 'Real Madrid', 'Manchester City', 'Chelsea'] for home_team in teams: for away_team in teams: if home_team != away_team: print(home_team + ' vs ' + away_team)
Output
LiverPool vs Real Madrid LiverPool vs Manchester City LiverPool vs Chelsea Real Madrid vs LiverPool Real Madrid vs Manchester City Real Madrid vs Chelsea Manchester City vs LiverPool Manchester City vs Real Madrid Manchester City vs Chelsea Chelsea vs LiverPool Chelsea vs Real Madrid Chelsea vs Manchester City
for loop python
Break/Continue
The break Statement
When you create for loops, sometimes you may want to end the loop early, stopping the loop when a certain criterion has been fulfilled, rather than iterating through the entire loop. The break
keyword is utilized to end a loop early by exiting a loop and moving on to the next line in the program
fruits = ['Mango', 'Lemon', 'Banana', 'Apple', 'Cherry', 'Watermelon', 'Orange'] for fruit in fruits: print(fruit) if(fruit == 'Cherry'): print("Cherry Here") break print("That's It")
Output
Mango Lemon Banana Apple Cherry Cherry Here That's It
The continue Statement
With the continue
statement we can stop the current iteration of the loop, and continue with the next
numbers = [24, 46, 22, 37, 62, 15, 19, 37, 45] for i in numbers: if i % 2 == 0: continue print(i)
Output
37 15 19 37 45
else Keyword in For Loop
The else
keyword in a for loop specifies a block of code to be executed when the loop is finished.
for x in range(4): print(x) else: print("Done")
Output
0 1 2 3 Done
Note: The
else
block will NOT be executed if the loop is stopped by abreak
statement.