You are on page 1of 7

09-for-loops

February 27, 2023

[1]: i = 1
j = i+5
k = j+10
print(i,j,k)

i = 2
j = i+5
k = j+10
print(i,j,k)

i = 3
j = i+5
k = j+10
print(i,j,k)

1 6 16
2 7 17
3 8 18

1 Loops
• There may be a situation when you need to execute a block of code several number of times.
• to iterate over items or elements in a given seq like str, list, tup, set, or dict
• in general. statements are execueted sequentially: The first statement in a function is executed
first. followed by the second and so on.
Iteration or loops - repeat actions
• for loop –> iterates for all the items present in iterating_ sequence:
• while loop –> until the condition satisfies

1.1 For Loop


• The for loop in python is used to iterate over a sequence(list, tuple, string) or other iterable
objects.
• Loop continues until we reach the last item in sequence.
• The body of for loop is saperated from the rest of the code using indentation
<b> Syntax: </b>

1
for element in sequence: Body of for
Here, element is the variable that takes that value of the item inside the sequence on each iteration.
Loop continues until all the items completed
[2]: for i in range(1,4):
j = i+5
k = j+10
print(i,j,k)

1 6 16
2 7 17
3 8 18

[3]: for i in range(1,4):


j = i+5
k = j+10
print(i,j,k)
print('krishna')

1 6 16
2 7 17
3 8 18
krishna

[4]: for i in range(1,4):


j = i+5
k = j+10

print(i,j,k)

3 8 18
print all the numbers between 10 and 50 (both inclusive)

[5]: for i in range(10,51):


print(i,end = ' ')

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45 46 47 48 49 50
print even numbers in descending order from 1 to 100
[6]: for i in range(100,1,-1):
print(i,end = ' ')

100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75
74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48
47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2

2
write a python program to get all the even numbers for a given range both are inclusive
[9]: lb = int(input('enter lower bound values'))
ub = int(input('enter upper bound values'))

for i in range(lb,ub+1):
if i%2==0:
print(i,end=' ')

enter lower bound values1


enter upper bound values50
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
write program that gives numbers between 1 to 100 which are divisible by 5 and divisible by 7
[10]: for num in range(1,101):
if num%5 == 0 and num%7 == 0:
print(num)

35
70

[11]: seq = [1,2,3,4,5]


# iterating over the list

for i in seq:
print(i)

1
2
3
4
5

[12]: seq = [1,2,3,4,5]


for item in seq:
print(item)

1
2
3
4
5

[13]: seq = [1,2,3,4,5]


for item in seq:
print('srk')

srk

3
srk
srk
srk
srk

[14]: items = ['item1','item2','item3','item4']


for i in range(len(items)):
print(items[i],'is at index ',i)

item1 is at index 0
item2 is at index 1
item3 is at index 2
item4 is at index 3
sum of items in a given list
[16]: l = [1,3,5,7]

sum = 0 # initialization

for i in l:
sum = sum+i

print(sum)

16
sum of even number in between given range (both inclusive

[19]: lb = int(input('Enter lower bound values'))


ub = int(input('Enter upper bound values'))

s = 0

for i in range(lb,ub+1):
if i % 2 ==0:
s = s+i
print(i,end = ' ')

print('\nsum is',s)

Enter lower bound values1


Enter upper bound values10
2 4 6 8 10
sum is 30
Program to print multiple mathematical tables
[ ]:

4
[20]: a = int(input('enter multiplication table: '))

for i in range(1,11):
print(a, '*',i,'=', a*i)

enter multiplication table: 5


5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Write a program which can compute the factorial of a given number.
[21]: a = int(input())

fact = 1
for i in range(a,0,-1):
fact = fact * i

print('factorial of',a,'is',fact)

5
factorial of 5 is 120

1.2 Nested for loop


[22]: for i in range(3):
for j in range(2):
print('i=',i,'j=',j)

i= 0 j= 0
i= 0 j= 1
i= 1 j= 0
i= 1 j= 1
i= 2 j= 0
i= 2 j= 1

[23]: for i in range(2):


for j in range(2):
print('ds')

ds

5
ds
ds
ds

1.3 Loops Termination


1.3.1 Break
[24]: numbers = [1,2,3,4]

for i in numbers:
if i == 3:
break

print(i)

1.3.2 Continue
[25]: number = [1,2,3,4,5]

for num in numbers:


if num % 2 == 0:
print(num)
continue

2
4

1.4 For loop with else


• A for loop can have an optional else block as well,
• The else part will be executed if the items in the sequence in for loop exhausts.
• break statement can be used to stop a for loop. In such case else part is ignored
• Hence a for loop’s else part runs if no break occurs.
• if break gets executed with in the for loop then ignore (or) don’t execute the else part of for
loop Vice versa.
• if break does not gets executed with in the for loop then excute the else part of for loop Vice
versa.
[36]: numbers = [1,2,3,4]
for items in numbers:
print(items)
if items == 4:
break

else:
print('no item left in the list')

6
1
2
3
4

[37]: numbers = [1,2,3,4]


for item in numbers:
print(item)
if item == 4:
break

else:
print('no item left in the list')

1
2
3
4

You might also like