-
Notifications
You must be signed in to change notification settings - Fork 0
Control Flow in Python
Welcome to Section 4 of the Python One-Shot for Beginners 2025! In this section, we will cover how to control the flow of your Python programs using if statements, else statements, and loops. These concepts are vital for making decisions, repeating actions, and creating interactive programs.
An if statement lets your program make decisions based on certain conditions. You can control whether a block of code runs or not based on boolean expressions (True or False).
if condition:
# Code to run if the condition is True
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult!")
else:
print("You are a minor!")
In this example:
- If the user's age is 18 or older, the program prints
"You are an adult!"
. - If the age is less than 18, it prints
"You are a minor!"
.
-
Missing Colon: Always remember to add a colon (
:
) at the end of theif
orelse
statement. -
Indentation: Python relies on indentation to define code blocks. Make sure your code inside the
if
andelse
statements is indented correctly.
You can check multiple conditions using the elif
keyword. If the first condition is not met, Python will check the next condition in the elif
statement.
if condition1:
# Code if condition1 is True
elif condition2:
# Code if condition2 is True
else:
# Code if none of the above conditions are True
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult!")
elif age >= 13:
print("You are a teenager!")
else:
print("You are a child!")
In this example:
- The program first checks if the user is 18 or older.
- If not, it checks if the user is 13 or older.
- If neither condition is true, it prints "You are a child!"
- The order of conditions matters. Python will check each condition in sequence. If the first condition is true, the rest of the conditions won’t be checked.
Loops allow you to repeat code multiple times, which is essential for handling repetitive tasks.
A for
loop is used to iterate over a sequence (such as a list, range, or string). It's great for repeating an action a specific number of times.
for variable in sequence:
# Code to repeat
for i in range(5):
print("Iteration:", i)
This loop runs 5 times, printing the numbers from 0 to 4.
A while
loop repeats a block of code as long as a condition is True.
while condition:
# Code to repeat
count = 0
while count < 5:
print("Count is:", count)
count += 1 # Increment to avoid infinite loop
In this example, the loop continues until count
reaches 5.
An infinite loop happens when the loop's condition never becomes False, causing the loop to run forever.
while True:
print("This will never stop!")
Make sure you modify the condition inside the loop to eventually become False. You can use a break
statement to stop the loop if necessary.
You can also nest if
statements inside each other or place a loop inside an if
statement. This is useful for more complex conditions and repetitive tasks.
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult!")
if age >= 21:
print("You can also drink alcohol!")
else:
print("But you cannot drink alcohol yet.")
else:
print("You are a minor!")
In this example:
- If the user is 18 or older, we check if they are 21 or older to determine if they can drink alcohol.
Write a program that asks for the user's age and:
- Prints if they are eligible to vote (18 or older).
- Prints if they are eligible to drive (16 or older).
Write a program that checks if a number entered by the user is:
- Positive
- Negative
- Zero
Create a Guess the Number game where the user has to guess a number between 1 and 10. The program should give feedback based on whether the guess is correct or incorrect. Use a loop to allow the user to keep guessing until they get it right.
- If Statements help you make decisions in your program based on conditions.
- Elif lets you check for multiple conditions.
-
Loops allow you to repeat actions multiple times using
for
orwhile
loops. - Nested Statements and Loops help handle complex logic.
Now that you understand control flow, you're ready to handle more complex logic in your programs. In the next section, we'll dive into functions—a way to group your code into reusable blocks to make your programs more organized and efficient.
This section has equipped you with the foundational skills of controlling the flow of your Python programs using conditions and loops. By mastering these concepts, you can start making your programs more dynamic and interactive, responding to user input and repeating tasks when necessary.
Stay tuned for the next section, where we will tackle functions and learn how to structure our code for better readability and reuse.
© TechShade | 2025