Definition
Python For Loop is used to iterate over a sequence of Python’s iterable objects like list, strings, tuple, and sets or a part of the program several times.
Syntax
for iterating_variable in python_sequence:# part of code
Examples
Example1
# example for stringmyStr = "I am a string"for x in myStr:print(x) # spaces will also print
Output
Iamastring
Example2
# example for listmyList = ["I", "am", "a", "list"]for x in myList:print(x)
Output
Iamalist
Example3
# example for TuplemyTuple = ("I", "am", "a", "Tuple")for x in myTuple:print(x)
Output
IamaTuple
Example4
# example for setmySet = {1, 2, 3, 4, 5}for x in mySet:print(x)
Output
12345
Example5
# example for dictionarymyDict = {"key1":"value1", "key2":"value2", "key3":"value3"}for x in myDict:print(x,":",myDict[x])
Output
key1 : value1key2 : value2key3 : value3
range()
"range()"
is an in-built function in Python which takes three integers as parameters and returns a sequence of numbers.
Syntax
range(start, stop, step)
Parameter | Description |
---|---|
start | optional, default is 0 |
stop | Required, topmost position(stop-1) |
step | optional, this is the increment in value, default is 1 |
Examples
Example1
for x in range(5):print(x) # starts with 0(default)# 5 not included (5-1=4)
Output
0123
Example2
for x in range(1, 5):print(x) # starts with 1
Output
1234
Example3
for x in range(2, 11, 3):print(x) # starts with 2# ends on 11-1=10# 2+3=5# 5+3=8# 8+3=11(not included)
Output
258
Example4
myList = ["This", "is", "my", "list"]for x in range(len(myList)):print(myList[x])
Output
Thisismylist
Here "len(myList)"
wil return 3 since the "range()"
function will return a sequence upto 3 i.e. 0, 1, 2, 3 and myList[x] will print the lists’s element present at those index numbers.
break statement
"break"
statement is used to terminate a loop. If the program executes the "break"
then the loop will automatically get terminated whether the loop has iterated all the elements or not.
Example1
for x in range(5):print(x)if x == 3:break # nothing will print after 3
Output
0123
Example2
myList = [1, 2, 3, 4, 5]for x in myList:if x == 3:breakprint(x)
Output
12
continue statement
If the program reaches the continue statement then the program skips the current step of iteration of the loop and continues with the next step of the loop.
Example
myList = [1, 2, 3, 4, 5]for x in myList:if x == 3:continueprint(x)
Output
1245
pass statement
Any loop or function in Python can not be empty as it will give an error but Python gives pass statement if you want an empty loop or function.
Example
for x in range(5):pass
for…else…
Python provides a feature where we can use else with for loop and while loop as well, while most of the programming does not have this feature.
The else block will get executed if the for/while loop is not terminated with a break statement.
Example1
# for loop without breakfor x in range(5):print(x)else:print("else block")
Output
01234else block
Example2
# for loop with breakfor x in range(5):print(x)if x == 3:breakelse:print("else block will not executed")
Output
0123
Nested for loop
We can have loop inside a loop which is called nested loops.
For each step of outer loop, inner loop will execute whole i.e. all steps every time.
Example
for x in range(2):for y in range(2):print(x, y)
Output
0 00 11 01 1