EX7 Loopstm
EX7 Loopstm
Aim:-
To implement a python program looping statement(like for and while) in
iterations List, tuple,set,Dictionary.
Algorithm:
List: Iterate through each element in a list.
Tuple: Iterate through each element in a tuple.
Set: Iterate through each unique element in a set.
Dictionary: Iterate through keys or values or both in a dictionary.
# Creating an empty list
L=[1,2,3,5,6]
i=1
total = 0
while i <= 5:
total += i # Add the current value of i to total
i += 1
print("Total:", total)
Output:-
Total: 15
Reverse an integer:-
num=int(input(“Enter an integer”))
rev=0
while num!=0:
digit=num%10
rev=rev*10+digit
num=num//10
Print(“Reversed number “,rev)
Output:-
Enter an integer
123456
654321
output:-
2
5
8
11
14
17
20
23
26
29
Loop manipulations:-
Break:-
for i in range(5):
if i == 3:
break
print(i)
Output:-
0
1
2
Continue:-
for i in range(5):
if i == 3:
continue
print(i)
Output:-
0
1
2
4
Pass statement:-
for i in range(5):
if i == 3:
pass # Do nothing
else:
print(i)
Output:-
0
1
2
4