Day 2: Control Flow & Loops – Python 30-Day Challenge ??
Welcome to Day 2 of my Python learning journey! Today, I explored one of the most powerful concepts in programming: Control Flow & Loops. These structures allow us to make decisions in our code and repeat actions efficiently.
What I Learned Today
Conditional Statements (if-else)
Python supports the usual logic conditions used in mathematics: ? Equals: a == b ? Not Equals: a != b ? Less than: a < b ? Less than or equal to: a <= b ? Greater than: a > b ? Greater than or equal to: a >= b
These are commonly used with if statements and loops, making them essential for decision-making in Python.
?? Python’s If-Else Structure
x = 10
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is equal to 5")
else:
print("x is less than 5")
?? Python relies on indentation (whitespace) to define blocks of code instead of curly braces {} like in other languages.
?? Ternary Operators (Short-Hand If-Else)
Python allows one-liner if-else statements for concise logic.
age = 18
status = "Adult" if age >= 18 else "Minor"
print(status) # Output: Adult
?? Logical Operators
? and: Returns True if both conditions are True. ? or: Returns True if at least one condition is True. ? not: Reverses the result (True becomes False, and vice versa).
Example:
a = 10
b = 5
if a > 5 and b < 10:
print("Both conditions are met!")
?? Loops in Python
Loops allow us to repeat tasks efficiently. Python provides two main types:
1?? While Loop – Runs While a Condition is True
x = 1
while x <= 5:
print(x)
x += 1 # Don’t forget to increment, or it will loop forever!
?? Break Statement: Stops the loop before the condition becomes False. ?? Continue Statement: Skips the current iteration and moves to the next.
Example:
x = 0
while x < 5:
x += 1
if x == 3:
continue # Skip when x is 3
print(x)
2?? For Loop – Iterates Over Sequences
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
?? Looping Through a String: Strings are iterable, meaning we can loop through their characters.
领英推荐
for char in "Python":
print(char)
?? Using range() in For Loops The range() function helps iterate over a sequence of numbers.
for num in range(1, 6): # Loops from 1 to 5
print(num)
? We can define start, stop, and step values in range().
for num in range(2, 10, 2): # Start at 2, end before 10, step by 2
print(num)
?? Else in Loops: Runs when the loop finishes normally (i.e., not interrupted by break).
for x in range(5):
print(x)
else:
print("Loop completed!")
?? Nested Loops: A loop inside another loop.
for i in range(2):
for j in range(3):
print(f"i={i}, j={j}")
?? Pass Statement: Used to prevent syntax errors when a loop must exist but does nothing yet.
for x in range(5):
pass # Placeholder for future code
?? Exercise: Putting It into Practice
? 1. Check If a Number is Even or Odd
num = int(input("Enter a number: "))
if num % 2 == 0:
print(f"{num} is even")
else:
print(f"{num} is odd")
? 2. Print Numbers from 1 to 20, Skipping Multiples of 3
for i in range(1, 21):
if i % 3 == 0:
continue # Skip multiples of 3
print(i)
?? Resources Used
Final Thoughts
Today was all about making decisions and repeating tasks efficiently in Python. Learning if-else conditions and loops gives us the power to write dynamic, intelligent programs.
?? Next up: Functions & Modules! Can’t wait to dive deeper. If you have any tips or feedback, drop a comment below! Let’s keep learning together. ??
#Python #Day2 #30DayChallenge #Learning #Coding #Programming #PythonForBeginners