Welcome to the Python If-Else Statement lesson! In this tutorial, we will learn about conditional statements in Python and how to make decisions in your code based on certain conditions. 📝 Note: If you are new to Python, make sure to check out our Python Tutorial for Beginners first.
In Python, the If-Else statement is used to test a condition and perform specific actions based on whether the condition is True or False. It helps to make decisions in your code, making it more flexible and interactive.
Here's the basic structure of an If-Else statement in Python:
if condition:
# code to be executed if the condition is True
else:
# code to be executed if the condition is FalseConditions in Python are expressions that can be either True or False. Python supports several comparison operators like ==, !=, <, >, <=, and >=. Here are some examples:
# Example 1: Comparing numbers
x = 10
y = 20
if x < y:
print("x is less than y")
else:
print("x is greater than or equal to y")
# Output: x is less than y# Example 2: Comparing strings
a = "apple"
b = "banana"
if a < b:
print("a comes before b in the alphabet")
else:
print("a comes after b in the alphabet")
# Output: a comes after b in the alphabetYou can use multiple conditions in an If-Else statement by combining them with the and and or operators.
# Example 1: Multiple conditions with 'and'
x = 10
y = 20
if x < y and x > 5:
print("x is between 5 and y")
# Output: x is between 5 and y# Example 2: Multiple conditions with 'or'
x = 10
y = 20
if x < y or x > 50:
print("x is less than y or greater than 50")
# Output: x is less than y or greater than 50You can also nest If-Else statements to create more complex decision structures.
# Example: Nested If-Else statement
x = 10
if x > 0:
if x % 2 == 0:
print("x is a positive even number")
else:
print("x is a positive odd number")
else:
print("x is a negative number")
# Output: x is a positive even numberIf you have multiple conditions to check, you can use If-Else-If statements. This structure checks each condition in order, and as soon as it finds a True condition, it executes the corresponding code and stops checking the rest of the conditions.
# Example: If-Else-If statement
x = 10
if x > 20:
print("x is greater than 20")
elif x > 10:
print("x is greater than 10 but less than or equal to 20")
else:
print("x is less than 10")
# Output: x is greater than 10 but less than or equal to 20Which of the following options correctly checks if a number `n` is greater than 10 or less than or equal to 20 using an If-Else-If statement in Python?
Write an If-Else-If statement that checks if a number n is greater than 0, less than or equal to 10, or greater than 10 but less than or equal to 20.
n = 15
if n > 0:
if n <= 10:
print("n is less than or equal to 10")
elif n <= 20:
print("n is greater than 10 but less than or equal to 20")That's it for the Python If-Else lesson! Practice this concept as much as you can, and you'll soon be able to create more sophisticated and interactive code. Keep learning, and happy coding! 💡 Pro Tip: Don't forget to use comments in your code to explain complex parts and make your code easier for others (and future you) to understand. 📝 Note: If you have any questions or need help, feel free to ask in the comments below.
Good luck, and happy coding! 🎯 Note: This lesson was written for beginners and intermediates, so there's enough depth for both groups. Enjoy learning Python!