Python If-Else Statement Tutorial 🎯

beginner
7 min

Python If-Else Statement Tutorial 🎯

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.

What are If-Else Statements?

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:

python
if condition: # code to be executed if the condition is True else: # code to be executed if the condition is False

Understanding Conditions 📝 Note:

Conditions in Python are expressions that can be either True or False. Python supports several comparison operators like ==, !=, <, >, <=, and >=. Here are some examples:

python
# 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
python
# 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 alphabet

Using Multiple Conditions 📝 Note:

You can use multiple conditions in an If-Else statement by combining them with the and and or operators.

python
# 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
python
# 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 50

Nesting If-Else Statements 📝 Note:

You can also nest If-Else statements to create more complex decision structures.

python
# 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 number

If-Else-If Statements 📝 Note:

If 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.

python
# 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 20

Quiz 🎯

Quick Quiz
Question 1 of 1

Which 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?

Practice Exercise 🎯

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.

python
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!