Python For Loops
iterable: iterable mean something we repeat over and over is called iterable.
It will print all the value from 0 to 4
# For Loops
for i in range(5):
print(i)
output:
0
1
2
3
4
1
2
3
4
It will print only 0 five times
for i in range(5):
print(0)
output:
0
0
0
0
0
0
It will print only 1 five times
# For Loops
for i in range(5):
print(1)
output:
1
1
1
1
1
1
1
It will print only 2 five times
# For Loops
for i in range(5):
print(2)
output:
2
2
2
2
2
22
It will print only 3 five times
# For Loops
for i in range(5):
print(3)
output:
3
3
3
3
3
33
It will print only 4 five times
# For Loops
for i in range(5):
print(4)
output:
4
4
4
4
4 4
This will print whole list
for i in [1,3,4,5,5,6,7,8,9,0]:
print(i)
output:
1
3
4
5
5
6
7
8
9
0
3
4
5
5
6
7
8
9
0
End = " " ----> this will print vertically with a space
for i in range(7):
print(i, end = " ")
output:
0 1 2 3 4 5 6
End = "" ----> this will print vertically without space
for i in range(7):
print(i, end = "")
output:
0123456
End = " " ----> this will print 6 seven times vertically with space
for i in range(7):
print(6, end = " ")
6 6 6 6 6 6 6
for i in range(6):
print(4, end = " Hello world ")
output:
4 Hello world 4 Hello world 4 Hello world 4 Hello world 4 Hello world 4 Hello world
for i in range(6 ):
print(4, end = " $ ")
output:
4 $ 4 $ 4 $ 4 $ 4 $ 4 $
It will change the line every time because of " \n "
for i in range(6):
print(4, end = "\n")
output:
44
4
4
4
4
for i in range(10,2,-1):
print(i)
output:
10
9
8
7
6
5
4
3
9
8
7
6
5
4
3
for i in range(10,2,-1):
print(i, end = " ")
10 9 8 7 6 5 4 3
for i in range(15,5,-2):
print(i)
output:
15
13
11
9
7
13
11
9
7
for i in range(15,5,-2):
print(i, end = " ")
output:
15 13 11 9 7
without " i "
for _ in range(15,5,-2):
print(11, end = " ")
output:
11 11 11 11 11
You can take any variable name in "i" name.
example:
for ali in range(15,5,-2):
print(ali, end = " ")
output:
15 13 11 9 7
It will print 'Hello' 50 times
for _ in range(50):
print("Hello", end = " ")
output:
Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello
Hello Hello Hello
Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello
Hello Hello Hello
Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello
Hello Hello Hello
Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello
another one understand it.
for i in range(8):
print("Hey There", i, end = " ")
output:
Hey There 0 Hey There 1 Hey There 2 Hey There
3 Hey There 4 Hey There 5 Hey There 6 Hey There 7
it will print 15. how understand
sum = 0
for i in range(1,6):
sum = sum + i
print(sum)
output: 15
sum = sum + i
0 + 1 =1
1 + 2 =3
3 + 3 = 6
6 + 4 = 10
10 + 5 = 15
See how below code will print 15
sum = 0
for i in range(1,6):
print(sum, i)
sum = sum + i
print(sum)
output:
0 1
1
1 2
3
3 3
6
6 4
10
10 5
15
1
1 2
3
3 3
6
6 4
10
10 5
15
Another for loop
for i in "London":
print(i)
output:
L
o
n
d
o
n
o
n
d
o
n
for i in "London":
print(i, end = " ")
output: L o n d o n
for i in "London":
print(i, end = "")
for i in "London":
print(i, end = "''")
output: L''o''n''d''o''n''
Break: Break the loop.
for i in "Sudhakar":
print(i)
if i == "a":
break
output:
S
u
d
h
a
u
d
h
a
for i in "Sudhakar":
print(i, end = "")
if i == "a":
break
output: Sudha
Iteration: Every step of a loop called iteration.
Continue: it skips the current iteration.
pass: Pass mean don't do anything or nothing.
pass example:
count = 0
for i in "sudhakar":
pass
count = count + 1 ## or count += 1
print(count)
These are the same:
a = a + 1
or
a += 1
b = b - 1
or
b -= 1
c = c/d
or
c /= d
Table of 2
## Table of Two
for i in range(1, 11):
print("2 x", i, "=",2*i)
output:
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
Print table of 1
## print table of 1
for i in range(1, 11):
print("1 x",i,"=",1*i)
output:
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
1 x 10 = 10
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
1 x 10 = 10
Print table from 1 to 5
## print table from 1 to 5
for i in range(1, 6):
for j in range(1,11):
print(i,"x",j,"=",i*j)
print("*********************")
output:
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
1 x 10 = 10
*********************
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
*********************
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
*********************
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
*********************
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
1 x 10 = 10
*********************
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
*********************
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
*********************
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
*********************
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
*********************
Range Function
range(n)-----> 0,1,2,3,4,5........n-1
range(3)------> 0,1,2
range(5)------> 0,1,2,3,4
note: range function always start from Zero.
range(1,5)----> 1,2,3,4
range(4,5)-----> 4
range(100,1000)------> 100, 101, 102,103,104,105,106,107,108.........999
note: ending number always excluded in range function.
in range function starting number if not provided then python start from 0.
Example: range(10)----> 0,1,2,3,4,5....9
range(2,12)-----> 2,3,4,5,6,7,8,9,10,11
'2' is a starting number and 12 is an ending number.
range(2,12,2)-------> 2,4,6,8,10
'2' is a starting number.
'12' is an ending number.
'2' is a step number like 2 ,4, 6, 8, 10, 12, 14
in range function
Default starting number 0
Default step size 1
Three types of range function uses we saw.
range(10)---> 0,1,2,3,4,5,6,7,8,9
range(1, 10)----> 1,2,3,4,5,6,7,8,9
range(1,10,2)-----> 1,3,5,7,9