Python Conditionals and Loops: Control Your Code
In this third article of our beginner-friendly Python tutorial series, we'll explore Python conditionals and loops. These concepts are essential for controlling the flow of your program, allowing you to execute certain parts of your code based on conditions and repeat code blocks as needed.
Conditionals in Python
Conditionals allow you to execute different parts of your code based on whether a condition is true or false. Python has three main conditional statements: if, elif (short for "else if"), and else.
1. If Statements
If statements are used to check if a condition is true. If the condition is true, the code block following the if statement will be executed. Here's an example:
x = 10
if x > 5:
print("x is greater than 5")
2. Elif Statements
Elif statements are used to check multiple conditions sequentially. If the condition in the if statement is false, the program will move on to the next elif statement. If an elif condition is true, the corresponding code block will be executed. Here's an example:
x = 10
if x > 20:
print("x is greater than 20")
elif x > 5:
print("x is greater than 5 but not greater than 20")
3. Else Statements
Else statements are used to execute a code block when none of the preceding conditions are true. Here's an example:
x = 3
if x > 10:
print("x is greater than 10")
elif x > 5:
print("x is greater than 5 but not greater than 10")
else:
print("x is not greater than 5")
Loops in Python
Loops allow you to execute a block of code multiple times. Python has two main types of loops: for loops and while loops.
1. For Loops
For loops are used to iterate over a sequence, such as a list or a range of numbers. The loop will continue for as many elements as there are in the sequence. Here's an example:
for i in range(5):
print(i)
This code will output the numbers 0 to 4, one per line.
2. While Loops
While loops are used to execute a block of code as long as a condition is true. Once the condition becomes false, the loop will exit. Here's an example:
count = 0
while count < 5:
print(count)
count += 1
This code will also output the numbers 0 to 4, one per line, just like the for loop example.
3. Loop Control Statements
Python provides two loop control statements to modify the behavior of loops: break and continue.
- break: The break statement is used to exit a loop prematurely when a certain condition is met. Here's an example:
for i in range(10):
if i == 5:
break
print(i)
This code will output the numbers 0 to 4, stopping when i reaches 5.
- continue: The continue statement is used to skip the rest of the code block in a loop and jump to the next iteration. Here's an example:
for i in range(10):
if i % 2 == 0:
continue
print(i)
This code will output the odd numbers between 1 and 9, skipping the even numbers.
With a good understanding of Python conditionals and loops, you can now create more complex and dynamic programs. In the next article, we'll cover Python functions, which will allow you to organize your code into reusable blocks. Stay tuned!