-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path01_for_loop.py
More file actions
228 lines (178 loc) · 6.29 KB
/
01_for_loop.py
File metadata and controls
228 lines (178 loc) · 6.29 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""
================================================================================
File: 01_for_loop.py
Topic: For Loops in Python
================================================================================
This file demonstrates the 'for' loop in Python, which is used to iterate
over sequences (lists, tuples, strings, ranges, etc.) or any iterable object.
Key Concepts:
- Basic for loop syntax
- range() function for numeric iterations
- Iterating over different data types
- enumerate() for index and value
- zip() for parallel iteration
- Nested for loops
================================================================================
"""
# -----------------------------------------------------------------------------
# 1. Basic For Loop
# -----------------------------------------------------------------------------
# Iterate over items in a sequence
print("--- Basic For Loop ---")
fruits = ["apple", "banana", "cherry", "date"]
for fruit in fruits:
print(f"I like {fruit}")
# -----------------------------------------------------------------------------
# 2. Using range() Function
# -----------------------------------------------------------------------------
# range(start, stop, step) - generates numbers from start to stop-1
print("\n--- Using range() ---")
# range(5) generates 0, 1, 2, 3, 4
print("range(5):")
for i in range(5):
print(i, end=" ")
print()
# range(2, 8) generates 2, 3, 4, 5, 6, 7
print("\nrange(2, 8):")
for i in range(2, 8):
print(i, end=" ")
print()
# range(0, 10, 2) generates 0, 2, 4, 6, 8 (step of 2)
print("\nrange(0, 10, 2):")
for i in range(0, 10, 2):
print(i, end=" ")
print()
# Counting backwards: range(10, 0, -1)
print("\nrange(10, 0, -1) - Countdown:")
for i in range(10, 0, -1):
print(i, end=" ")
print("Blastoff!")
# -----------------------------------------------------------------------------
# 3. Iterating Over Strings
# -----------------------------------------------------------------------------
# Each character in a string is an item
print("\n--- Iterating Over Strings ---")
word = "Python"
print(f"Characters in '{word}':")
for char in word:
print(f" {char}")
# -----------------------------------------------------------------------------
# 4. Using enumerate() - Get Index and Value
# -----------------------------------------------------------------------------
# enumerate() provides both the index and the item
print("\n--- Using enumerate() ---")
languages = ["Python", "JavaScript", "C++", "Rust"]
for index, language in enumerate(languages):
print(f"{index + 1}. {language}")
# Start from a custom number
print("\nWith custom start:")
for rank, language in enumerate(languages, start=1):
print(f"Rank {rank}: {language}")
# -----------------------------------------------------------------------------
# 5. Using zip() - Parallel Iteration
# -----------------------------------------------------------------------------
# zip() combines multiple iterables and iterates over them together
print("\n--- Using zip() ---")
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
cities = ["New York", "London", "Tokyo"]
# Iterate over multiple lists simultaneously
for name, age, city in zip(names, ages, cities):
print(f"{name} is {age} years old and lives in {city}")
# -----------------------------------------------------------------------------
# 6. Iterating Over Dictionaries
# -----------------------------------------------------------------------------
# Different ways to loop through dictionary data
print("\n--- Iterating Over Dictionaries ---")
person = {
"name": "Baraa",
"age": 25,
"city": "Gaza",
"profession": "Developer"
}
# Keys only (default)
print("Keys:")
for key in person:
print(f" {key}")
# Values only
print("\nValues:")
for value in person.values():
print(f" {value}")
# Both keys and values
print("\nKey-Value pairs:")
for key, value in person.items():
print(f" {key}: {value}")
# -----------------------------------------------------------------------------
# 7. Nested For Loops
# -----------------------------------------------------------------------------
# A loop inside another loop
print("\n--- Nested For Loops ---")
# Multiplication table
print("Multiplication Table (1-5):")
for i in range(1, 6):
for j in range(1, 6):
print(f"{i * j:3}", end=" ")
print() # New line after each row
# Working with 2D lists (matrices)
print("\n2D Matrix:")
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
for row in matrix:
for cell in row:
print(f"{cell}", end=" ")
print()
# -----------------------------------------------------------------------------
# 8. Else Clause in For Loop
# -----------------------------------------------------------------------------
# The else block executes when the loop completes without break
print("\n--- For-Else Clause ---")
# Search for a number
numbers = [1, 3, 5, 7, 9]
search_for = 5
for num in numbers:
if num == search_for:
print(f"Found {search_for}!")
break
else:
print(f"{search_for} was not found.")
# -----------------------------------------------------------------------------
# 9. List Comprehension (Related to For Loops)
# -----------------------------------------------------------------------------
# A concise way to create lists using for loops
print("\n--- List Comprehension Preview ---")
# Traditional way
squares_traditional = []
for x in range(1, 6):
squares_traditional.append(x ** 2)
print(f"Traditional: {squares_traditional}")
# List comprehension way
squares_comprehension = [x ** 2 for x in range(1, 6)]
print(f"Comprehension: {squares_comprehension}")
# -----------------------------------------------------------------------------
# 10. Practical Examples
# -----------------------------------------------------------------------------
print("\n--- Practical Examples ---")
# Sum of numbers
numbers = [10, 20, 30, 40, 50]
total = 0
for num in numbers:
total += num
print(f"Sum of {numbers} = {total}")
# Finding maximum
values = [23, 45, 12, 78, 34]
maximum = values[0]
for value in values:
if value > maximum:
maximum = value
print(f"Maximum in {values} = {maximum}")
# Counting vowels in a string
text = "Hello, World!"
vowels = "aeiouAEIOU"
vowel_count = 0
for char in text:
if char in vowels:
vowel_count += 1
print(f"Vowels in '{text}': {vowel_count}")