Table of contents | |
Introduction | |
Syntax of if/else Statements | |
Examples of if/else Statements | |
Nested if/else Statements | |
Sample Problems |
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:
if condition:
# Code to execute if the condition is true
else:
# 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")
else:
print("The number is odd")
Output:
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")
else:
print("num1 is not greater than num2")
Output:
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")
else:
print("The number is greater than 10 but odd")
else:
print("The number is not greater than 10")
Output:
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")
else:
print(year, "is not a leap year")
Output:
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'
else:
grade = 'F'
print("Grade:", grade)
Output:
Grade: B
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":
print("Authentication successful")
else:
print("Authentication failed")
Sample output:
Enter username: admin
Enter password: password123
Authentication successful
Enter username: guest
Enter password: pass456
Authentication failed
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.
49 videos|38 docs|18 tests
|
|
Explore Courses for EmSAT Achieve exam
|