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! ๐๐ฆ