Welcome back, Python adventurer! π§ββοΈβ¨ Now that youβre cozy with variables, data types, and operators, itβs time to learn how to make your code think and repeat β kind of like a magical AI assistant! Control structures are the brain of your program, helping it make decisions and perform repetitive tasks.
Ready to take control? Letβs dive in! π
Imagine youβre deciding what to wear based on the weather. Conditional statements let your code do the same kind of decision-making! They allow your program to take different actions based on different conditions.
Basic Structure
Hereβs how if, elif, and else work:
weather = "rainy"
if weather == "sunny": print("Wear sunglasses! π") elif weather == "rainy": print("Take an umbrella! β") else: print("Dress comfortably! π")
Whatβs Happening Here?
if checks the first condition. If itβs True, it runs the block of code inside.elif (short for βelse ifβ) checks other conditions if the if condition was False.else runs if none of the conditions are True.When you run this, since weather is "rainy", it will print:
Take an umbrella! β
print) matters in Python! It tells the program what belongs inside the conditional block. Always use 4 spaces or a tab.Ever had to fold a hundred paper cranes? π¦’ With loops, Python can repeat tasks like this for you! Loops are perfect for when you need to do something multiple times.
Letβs meet the two types of loops in Python:
a) for Loop: Looping Through a Collection π
A for loop lets you iterate over items in a list, string, or other collection.
Example:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits: print(f"I love {fruit}! πππ")
Explanation:
for loop goes through each item in fruits.fruit takes the value of each item one by one.I love apple! π I love banana! π I love cherry! π
b) while Loop: Repeat Until a Condition is False β³
A while loop keeps running as long as a condition remains True.
Example:
countdown = 5
while countdown > 0: print(f"Countdown: {countdown} β³") countdown -= 1 # Decrease by 1 each time
print("Blast off! π")
Explanation:
countdown is greater than 0.countdown by 1.countdown reaches 0, the loop stops, and it prints βBlast off!βCountdown: 5 β³ Countdown: 4 β³ Countdown: 3 β³ Countdown: 2 β³ Countdown: 1 β³ Blast off! π
while loops β if the condition never becomes False, youβll create an infinite loop (yikes!). πLetβs put loops and conditionals together in a simple example. Imagine you want to find all the even numbers in a list:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for num in numbers: if num % 2 == 0: print(f"{num} is even! β
")
Explanation:
for loop goes through each num in the list.if condition checks if num is even (i.e., num % 2 == 0).Output:
2 is even! β
4 is even! β
6 is even! β
8 is even! β
10 is even! β
break and continue πͺSometimes, you want more control over your loops. Enter the break and continue keywords!
break stops the loop entirely.continue skips the rest of the loopβs code and jumps to the next iteration.Example:
for num in range(1, 10): if num == 5: break # Stop the loop when num is 5 if num == 3: continue # Skip number 3 print(num)
Output:
1 2 4
Loops and conditionals are the secret sauce that makes your code dynamic and powerful. They allow your program to make decisions, repeat tasks, and handle any scenario you throw at it.
At Codigo Aldea, we believe that everyone has the potential to master Python and become a coding wizard! π§ββοΈπ» Our mentorship courses are designed to help you learn Python, understand real-world applications, and build confidence through hands-on projects. Whether youβre just starting out or looking to level up your skills, weβve got your back!
π‘ Ready to dive deeper? Join our upcoming Python Full Stack Mentorship Program.
Learn Python, Git, Django, HTML, CSS, Bootstrap, build 6 real-world projects, and get expert guidance on communication skills, LinkedIn optimization, and mock interviews.
Letβs code, create, and conquer the tech world together! π
π Enroll now and start your coding journey with Codigo Aldea!
Happy looping and decision-making! ππ¦
ADMIN