-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConditional Statements
More file actions
351 lines (323 loc) · 11.1 KB
/
Conditional Statements
File metadata and controls
351 lines (323 loc) · 11.1 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
=============================================================================
CONDITIONAL STATEMENTS - IF/ELIF/ELSE
=============================================================================
print("--- Understanding Conditional Statements ---")
print("Conditional statements control program flow based on True/False conditions")
print("Three main types: if, elif, else")
When to use them together - mutually exclusive conditions
print("\n--- When to Use if/elif/else Together ---")
def check_grade(score):
"""Demonstrate mutually exclusive conditions."""
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "F"
Test the grading function
test_scores = [95, 87, 72, 65, 45]
print("Grade checking (mutually exclusive conditions):")
for score in test_scores:
grade = check_grade(score)
print(f" Score {score} -> Grade {grade}")
print("\nWhy if/elif/else together?")
print(" - Only ONE condition block executes")
print(" - Conditions are checked in order")
print(" - First True condition wins")
print(" - 'else' catches everything that doesn't match")
When to use discrete if statements - independent conditions
print("\n--- When to Use Discrete 'if' Statements ---")
def check_weather_conditions(temperature, weather, humidity):
"""Demonstrate independent conditions that can all be true."""
messages = []
if temperature > 80:
messages.append("It's hot outside!")
if weather == "sunny":
messages.append("Don't forget sunscreen!")
if humidity > 70:
messages.append("It's quite humid today.")
if temperature > 85 and weather == "sunny":
messages.append("Perfect beach weather!")
return messages
Test independent conditions
print("Weather condition checking (independent conditions):")
conditions = [
(85, "sunny", 75),
(70, "cloudy", 60),
(90, "sunny", 80)
]
for temp, weather, humid in conditions:
messages = check_weather_conditions(temp, weather, humid)
print(f" Temp: {temp}°F, Weather: {weather}, Humidity: {humid}%")
for msg in messages:
print(f" -> {msg}")
print("\nWhy discrete if statements?")
print(" - Multiple conditions can be True simultaneously")
print(" - Each condition is evaluated independently")
print(" - All matching conditions execute")
=============================================================================
FOR LOOPS WITH RANGE FUNCTION
=============================================================================
print("\n" + "="*60)
print("FOR LOOPS WITH RANGE FUNCTION")
print("="*60)
Understanding Start/Stop/Step parameters
print("--- Understanding range(start, stop, step) ---")
print("range() function signature: range(start=0, stop, step=1)")
print(" - start: where to begin (default: 0)")
print(" - stop: where to end (exclusive - not included)")
print(" - step: increment amount (default: 1)")
def demonstrate_range_parameters():
"""Show different range() parameter combinations."""
print("\nrange() parameter examples:")
# Basic range (stop only)
print("range(5) - stop only:")
for i in range(5):
print(f" {i}", end=" ")
print(" # Prints 0 through 4")
# Start and stop
print("\nrange(2, 8) - start and stop:")
for i in range(2, 8):
print(f" {i}", end=" ")
print(" # Prints 2 through 7")
# Start, stop, and step
print("\nrange(1, 10, 2) - start, stop, step:")
for i in range(1, 10, 2):
print(f" {i}", end=" ")
print(" # Prints 1, 3, 5, 7, 9")
# Negative step (counting backwards)
print("\nrange(10, 0, -1) - counting backwards:")
for i in range(10, 0, -1):
print(f" {i}", end=" ")
print(" # Prints 10 down to 1")
# Step by larger amounts
print("\nrange(0, 100, 25) - larger steps:")
for i in range(0, 100, 25):
print(f" {i}", end=" ")
print(" # Prints 0, 25, 50, 75")
demonstrate_range_parameters()
When to use range() - practical examples
print("\n--- When to Use range() ---")
def repeat_action_examples():
"""Show when to use range() for repetition."""
# Repeat an action N times
print("Repeating an action 3 times:")
for i in range(3):
print(f" Action {i+1}: Hello World!")
# Create number sequences
print("\nCreating a sequence of even numbers:")
even_numbers = []
for i in range(0, 20, 2):
even_numbers.append(i)
print(f" Even numbers: {even_numbers}")
# Access list items by index
print("\nAccessing list items by index:")
fruits = ["apple", "banana", "orange", "grape", "kiwi"]
for i in range(len(fruits)):
print(f" Index {i}: {fruits[i]}")
# Countdown timer simulation
print("\nCountdown timer:")
for i in range(5, 0, -1):
print(f" {i}...")
print(" Blast off!")
repeat_action_examples()
How to use range() effectively
print("\n--- How to Use range() Effectively ---")
def range_best_practices():
"""Demonstrate best practices with range()."""
# Processing chunks of data
data = list(range(1, 21)) # Numbers 1-20
chunk_size = 5
print(f"Processing data in chunks of {chunk_size}:")
for i in range(0, len(data), chunk_size):
chunk = data[i:i+chunk_size]
print(f" Chunk starting at index {i}: {chunk}")
# Creating multiplication table
print("\nMultiplication table (5x5):")
for i in range(1, 6):
row = []
for j in range(1, 6):
row.append(i * j)
print(f" {i}: {row}")
# Nested ranges for coordinates
print("\nGrid coordinates (3x3):")
for x in range(3):
for y in range(3):
print(f" ({x}, {y})", end=" ")
print() # New line after each row
range_best_practices()
=============================================================================
FOR LOOPS WITH ITERABLES
=============================================================================
print("\n" + "="*60)
print("FOR LOOPS WITH ITERABLES")
print("="*60)
What is an iterable?
print("--- What is an Iterable? ---")
print("An iterable is any object that can be looped through one item at a time")
print("You can use a for loop to go through each element")
print("Common iterables: lists, strings, tuples, dictionaries, sets, files")
Five iterable data types with examples
print("\n--- Five Iterable Data Types ---")
def demonstrate_iterables():
"""Show examples of different iterable types."""
# 1. Lists
print("1. LISTS - Ordered, mutable collections")
colors = ["red", "green", "blue", "yellow"]
print(f" List: {colors}")
print(" Iterating through list:")
for color in colors:
print(f" I like the color {color}")
# 2. Strings
print("\n2. STRINGS - Sequences of characters")
word = "Python"
print(f" String: '{word}'")
print(" Iterating through string:")
for letter in word:
print(f" Letter: {letter}")
# 3. Tuples
print("\n3. TUPLES - Ordered, immutable collections")
coordinates = (10, 20, 30)
print(f" Tuple: {coordinates}")
print(" Iterating through tuple:")
for coord in coordinates:
print(f" Coordinate: {coord}")
# 4. Dictionaries
print("\n4. DICTIONARIES - Key-value pairs")
student_grades = {"Alice": 95, "Bob": 87, "Charlie": 92}
print(f" Dictionary: {student_grades}")
print(" Iterating through dictionary keys:")
for name in student_grades:
print(f" {name}: {student_grades[name]}")
print(" Iterating through key-value pairs:")
for name, grade in student_grades.items():
print(f" Student {name} scored {grade}")
# 5. Sets
print("\n5. SETS - Unordered collections of unique items")
unique_numbers = {1, 2, 3, 4, 5, 2, 3} # Duplicates automatically removed
print(f" Set: {unique_numbers}")
print(" Iterating through set:")
for number in unique_numbers:
print(f" Number: {number}")
demonstrate_iterables()
Advanced iterable examples
print("\n--- Advanced Iterable Examples ---")
def advanced_iterable_usage():
"""Show more complex iterable usage patterns."""
# Nested lists
print("Nested lists (2D data):")
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for row in matrix:
print(f" Row: {row}")
for item in row:
print(f" Item: {item}")
# List of dictionaries
print("\nList of dictionaries (common data structure):")
students = [
{"name": "Alice", "age": 20, "major": "CS"},
{"name": "Bob", "age": 21, "major": "Math"},
{"name": "Charlie", "age": 19, "major": "Physics"}
]
for student in students:
print(f" Student: {student['name']}")
print(f" Age: {student['age']}, Major: {student['major']}")
# String methods creating iterables
print("\nString methods that create iterables:")
sentence = "Python is awesome"
words = sentence.split() # Creates a list
print(f" Original: '{sentence}'")
print(f" Words: {words}")
for word in words:
print(f" Word: '{word}' (length: {len(word)})")
advanced_iterable_usage()
=============================================================================
WHILE LOOPS FOR DATA STATE CHANGES
=============================================================================
print("\n" + "="*60)
print("WHILE LOOPS FOR DATA STATE CHANGES")
print("="*60)
Reducing a value to a point
print("--- Reducing a Value to a Point ---")
def demonstrate_value_reduction():
"""Show examples of reducing values with while loops."""
# Countdown example
print("Countdown example:")
countdown = 10
while countdown > 0:
print(f" Countdown: {countdown}")
countdown -= 1
print(" Blast off!")
# Division until small enough
print("\nDividing number until it's less than 1:")
number = 1000
divisions = 0
while number >= 1:
print(f" Step {divisions}: {number}")
number = number / 2
divisions += 1
print(f" Final result: {number:.6f} after {divisions} divisions")
# Removing items from a list
print("\nRemoving items from list until empty:")
items = ["apple", "banana", "cherry", "date"]
while items: # While list is not empty
removed = items.pop()
print(f" Removed '{removed}', remaining: {items}")
print(" List is now empty!")
# Factorial calculation (reducing multiplier)
print("\nFactorial calculation (5!):")
n = 5
result = 1
original_n = n
while n > 0:
print(f" {result} × {n} = {result * n}")
result *= n
n -= 1
print(f" Final result: {original_n}! = {result}")
demonstrate_value_reduction()
Increasing a value to a point
print("\n--- Increasing a Value to a Point ---")
def demonstrate_value_increase():
"""Show examples of increasing values with while loops."""
# Counting up to a target
print("Counting up to target:")
counter = 1
target = 5
while counter <= target:
print(f" Count: {counter}")
counter += 1
print(f" Reached target: {target}")
# Doubling until reaching threshold
print("\nDoubling value until reaching threshold:")
value = 1
threshold = 100
steps = 0
while value < threshold:
print(f" Step {steps}: {value}")
value *= 2
steps += 1
print(f" Final value: {value} (exceeded threshold of {threshold})")
# Building a list until condition met
print("\nBuilding list until reaching desired length:")
numbers = []
target_length = 5
current = 1
while len(numbers) < target_length:
numbers.append(current ** 2) # Add square of current number
print(f" Added {current}² = {current**2}, list: {numbers}")
current += 1
print(f" Reached target length: {len(numbers)}")
# Fibonacci sequence until threshold
print("\nFibonacci sequence until value exceeds 50:")
a, b = 0, 1
fibonacci_sequence = [a, b]
while b <= 50:
next_fib = a + b
fibonacci_sequence.append(next_fib)
print(f" {a} + {b} = {next_fib}")
a, b = b, next_fib
print(f" Complete sequence: {fibonacci_sequence}")
demonstrate_value_increase()