-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.py
More file actions
81 lines (68 loc) · 1.75 KB
/
Copy path1.py
File metadata and controls
81 lines (68 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'''
> Date Created: 19/07/2025
> Author: Ishaan Rastogi
> Purpose: To show different types of loops in Python.
> Operating System: This is only for Windows OS, it may or may not work on other OS
> Program Status: 100% Working
'''
# For Loop - for iterating over a range of numbers
# for i in range( start, end, step ):
# If we don't specify start, it defaults to 0 and if we don't specify step, it defaults to 1
# For loop with range function
for i in range(1, 11, 2): # This will print odd numbers from 1 to 10
print(f"Current number: {i}")
print()
# While Loop - for iterating until a condition is met
# while (condition):
i = 1 # Initializing i
while (i <= 10):
print(i)
i += 2 # Increment by 2 to get odd numbers
print()
l = [ 1, "Harry", 3.14, True, None ]
# Iterate over a list using a while loop
i = 0 # Initializing index
while (i < len(l)):
print(l[i])
i += 1
print()
# Iterate over a list using a for loop
# For loop for lists
for i in l:
print(i)
print()
# For loop for tuples
t = ( 1, "Harry", 3.14, True, None )
for i in t:
print(i)
print()
# For loop for strings
s = "Harry"
for i in s:
print(i)
print()
# For loop for dictionaries
d = { "name": "Harry", "age": 20, "is_student": True }
for key, value in d.items():
print(f"{key}: {value}")
print()
# For loop for sets
st = { 1, "Harry", 3.14, True, None }
for i in st:
print(i)
print()
# For Loop with else - to execute something when the loop completes normally using else
l = [1, 2, 3, 4, 5]
for i in l:
print(i)
else:
print("Loop completed successfully!")
print()
# While Loop with else - to execute something when the loop completes normally using else
i = 1
while (i <= 5):
print(i)
i += 1
else:
print("While loop completed successfully!")
print()