0% found this document useful (0 votes)
2 views6 pages

EX7 Loopstm

The document outlines a Python program that demonstrates the use of looping statements such as for and while with lists, tuples, sets, and dictionaries. It includes examples of calculating totals, iterating through collections, reversing an integer, and using loop control statements like break, continue, and pass. The program showcases various outputs for each operation performed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

EX7 Loopstm

The document outlines a Python program that demonstrates the use of looping statements such as for and while with lists, tuples, sets, and dictionaries. It includes examples of calculating totals, iterating through collections, reversing an integer, and using loop control statements like break, continue, and pass. The program showcases various outputs for each operation performed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

EX7) To implement a python program using looping statement

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

Using range() with a Tuple


# Creating a tuple using range()
numbers_tuple = tuple(range(5, 11)) # Generates (5, 6, 7, 8, 9, 10)

for num in numbers_tuple:


print(num)
Output:
5
6
7
8
9
10
# Set of numbers
my_set = {100, 200, 300, 400, 500}
# Looping through the set using a for loop
for item in my_set:
print(item)
Output:-
100
200
300
400
500

2. Looping through a Tuple


numbers = (10, 20, 30, 40)

# Using for loop


for num in numbers:
print(num)

# Using while loop


i=0
while i < len(numbers):
print(numbers[i])
i += 1
Output:
10
20
30
40
range() with a Dictionary
Here, we use range() to create dictionary keys and assign values dynamically.
# Creating a dictionary using range()

num_dict = {x: x**2 for x in range(1, 6)}

for key, value in num_dict.items():


print(f"{key}: {value}")
Output:
Copy
1: 1
2: 4
3: 9
4: 16
5: 25

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

Using For Loop loop to print 1 to 10 numbers


for x in range(2,30,3):
print(x)

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

You might also like