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.
for iterating_var in sequence: print(iterating_var)
Looping through a String
for letter in "codejagd": print(letter)
c o d e j a g d
For Loop Example
friends = ['Ross', 'Jim', 'Pat', 'Steve'] for friend in friends: print("Hello " + friend)
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).
range( begin, end, step )
beginis the initial value given in the range; the default value becomes zero if this is not included.
endis the value that comes after the last value in the range; the end value is not deleted.
stepindicates 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)
0 1 2 3 4 5 6
range() function defaults to as a starting value. Now , lets specify the start value in
for number in range(2, 7): print(number)
2 3 4 5 6
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)
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)
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
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")
Mango Lemon Banana Apple Cherry Cherry Here That's It
The continue Statement
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)
37 15 19 37 45
else Keyword in For Loop
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")
0 1 2 3 Done
elseblock will NOT be executed if the loop is stopped by a