| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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 Trueage = int(input("Enter your age: "))
if age >= 18:
print("You are an adult!")
else:
print("You are a minor!")In this example:
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 Trueage = 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:
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 repeatfor 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 repeatcount = 0
while count < 5:
print("Count is:", count)
count += 1 # Increment to avoid infinite loopIn 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:
Write a program that asks for the user's age and:
Write a program that checks if a number entered by the user is:
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.
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
| Back | FazBrowse Home | New Git URL |