|Table of contents|
|Syntax of if/else Statements|
|Examples of if/else Statements|
|Nested if/else Statements|
If/else statements are an essential part of programming in Python. They allow you to make decisions based on certain conditions and execute different blocks of code accordingly. In this article, we will explore the basics of if/else statements, understand their syntax, and see how they can be used in various scenarios. By the end, you'll have a solid understanding of how to use if/else statements in your Python programs.
The basic syntax of an if/else statement in Python is as follows:
# Code to execute if the condition is true
# Code to execute if the condition is false
The 'condition' is an expression that evaluates to either 'True' or 'False'. If the condition is 'True', the code block inside the 'if' statement will be executed. Otherwise, if the condition is 'False', the code block inside the 'else' statement will be executed.
Let's look at some examples to better understand how if/else statements work in Python.
Example 1: Checking if a Number is Even or Odd
num = 7
if num % 2 == 0:
print("The number is even")
print("The number is odd")
The number is odd
In this example, we check if the number 'um' is even or odd. The condition 'num % 2 == 0' checks if 'num' is divisible by 2. If it is, the condition is 'True', and the code block inside the 'if' statement is executed. Otherwise, the code block inside the 'else' statement is executed.
Example 2: Comparing Two Numbers
num1 = 10
num2 = 5
if num1 > num2:
print("num1 is greater than num2")
print("num1 is not greater than num2")
num1 is greater than num2
In this example, we compare two numbers, 'num1' and 'num2'. If 'num1' is greater than 'num2', the condition 'num1' > 'num2' is True, and the code block inside the if statement is executed. Otherwise, the code block inside the 'else' statement is executed.
In addition to simple if/else statements, you can also nest them inside each other to handle more complex conditions. This allows you to create multiple levels of decision-making.
Example 1: Nested if/else Statements
num = 15
if num > 10:
if num % 2 == 0:
print("The number is greater than 10 and even")
print("The number is greater than 10 but odd")
print("The number is not greater than 10")
The number is greater than 10 but odd
In this example, we check if the number 'num' is greater than 10. If it is, we further check if it's even or odd. Depending on the conditions, different code blocks are executed.
Now that we've covered the basics, let's try some sample problems to reinforce your understanding. Here are a few problems along with their solutions:
Problem 1: Check if a Year is a Leap Year
Write a program that checks if a given year is a leap year or not. A leap year is divisible by 4 but not divisible by 100, except if it's divisible by 400.
year = 2024
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
print(year, "is a leap year")
print(year, "is not a leap year")
2024 is a leap year
Problem 2: Grade Calculation
Write a program that takes a student's score as input and prints their corresponding grade based on the following conditions:
score = 85
if score >= 90:
grade = 'A'
elif score >= 80:
grade = 'B'
elif score >= 70:
grade = 'C'
elif score >= 60:
grade = 'D'
grade = 'F'
Problem 3: User Authentication
Write a program that asks the user to enter their username and password. If the username is "admin" and the password is "password123", display a message saying "Authentication successful". Otherwise, display a message saying "Authentication failed".
username = input("Enter username: ")
password = input("Enter password: ")
if username == "admin" and password == "password123":
Enter username: admin
Enter password: password123
Enter username: guest
Enter password: pass456
In this article, we explored the basics of if/else statements in Python. We learned their syntax, saw various examples, and understood how they can be used to make decisions based on conditions. By practicing with sample problems, you can further enhance your understanding and apply if/else statements in real-world scenarios. Keep experimenting and exploring the power of if/else statements in your Python programming journey.