forked from Adios-official/10-Day-Python-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_for_loop_intro.py
More file actions
69 lines (50 loc) · 1.98 KB
/
Copy path1_for_loop_intro.py
File metadata and controls
69 lines (50 loc) · 1.98 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
# ==========================================
# DAY 4: Automating with Loops
# ==========================================
# print("Welcome to Day 4! Today, we'll learn how to make Python repeat tasks using loops.")
# print("This is how we automate the boring stuff and work with our data collections efficiently!")
# print("\n" + "="*50)
# ==========================================
# SECTION 1: The 'for' Loop - Doing Something for Each Item
# ==========================================
print("SECTION 1: The 'for' Loop")
print("-" * 25)
# Exercise 1.1: Looping Through a List
# A 'for' loop iterates over a sequence (like a list) and performs an action for each item.
print("📋 Looping through a list of tasks:")
tasks = ["clean room", "do homework", "walk the dog", "read a book"]
# The variable 'task' will hold one item from the 'tasks' list in each iteration.
for task in tasks:
print(f"To do: {task}")
print("All tasks have been listed!")
print("\n" + "="*50)
# Exercise 1.2: Looping Through a Dictionary
# When you loop through a dictionary, you get its keys by default.
print("\n📖 Looping through a dictionary:")
player_stats = {
"name": "Alex",
"level": 5,
"health": 100
}
print("Player Stat Keys:")
for stat_key in player_stats:
# You can use the key to access the value inside the loop
stat_value = player_stats[stat_key]
print(f"-> Key: '{stat_key}', Value: {stat_value}")
print("\n" + "="*50)
# Exercise 1.3: Drawing a Shape with a Loop
# Since turtle graphics don't work in all online IDEs, let's draw with text!
# We can use a loop to print a square made of asterisks.
print("\n🎨 Drawing a square with a for loop:")
size = 5
# The top border
print("*" * size)
# The middle part: loop `size - 2` times for the inner rows
for _ in range(size - 2):
# Print a star, then spaces, then a star
print("*" + " " * (size - 2) + "*")
# The bottom border (only if size > 1)
if size > 1:
print("*" * size)
print("Square drawing complete!")
print("\n" + "="*50)