Python Conditional Statements
Python If-Else Statements Explained with Examples for Beginners
Introduction
Python is one of the most popular programming languages. It is easy to learn and useful for creating applications, websites, automation programs, and solving problems.
In Python programming, decision-making is an important concept. We use if-else statements to make decisions based on conditions.
In this tutorial, you will learn Python if-else statements with simple explanations, syntax, practical programs, and exercises.
What Is an If-Else Statement in Python?
An if-else statement is a conditional statement that allows a program to execute different instructions depending on whether a condition is True or False.
For example, a program can check whether a student has passed or failed an examination based on their marks.
1. Python If Statement
The if statement executes a block of code when a given condition is True.
Syntax
if condition:
statementExample
age = 18
if age >= 18:
print("You are eligible to vote.")Output:
You are eligible to vote.Explanation: The condition checks whether the age is 18 or above. Since the condition is True, the message is displayed.
2. Python If-Else Statement
The if-else statement executes one block when the condition is True and another block when it is False.
Example
marks = 35
if marks >= 40:
print("You are Pass")
else:
print("You are Fail")Output:
You are FailExplanation: The student's marks are 35, which is less than 40. Therefore, the else block executes.
3. Python If-Elif-Else Statement
The elif keyword means "else if". It allows us to check multiple conditions.
Example
marks = 85
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 60:
print("Grade B")
elif marks >= 40:
print("Grade C")
else:
print("Fail")Output:
Grade AExplanation: Python checks the conditions from top to bottom. When it finds the first True condition, it executes that block.
4. Nested If Statement
A nested if statement means using one if statement inside another if statement.
Example
age = 20
citizen = True
if age >= 18:
if citizen:
print("You are eligible to vote.")
else:
print("Citizenship is required.")
else:
print("You are not eligible to vote.")Output:
You are eligible to vote.5. Real-Life Practical Example: Even or Odd Number
We can use an if-else statement with the modulus operator (%) to check whether a number is even or odd.
number = 10
if number % 2 == 0:
print("Even Number")
else:
print("Odd Number")Output:
Even NumberExplanation: If a number is divided by 2 and the remainder is zero, it is an even number. Otherwise, it is odd.
6. Important Points About Python If-Else
The if keyword is used to check a condition.
The else keyword executes when the if condition is False.
The elif keyword checks additional conditions.
A colon (:) is required after the condition.
Indentation is important in Python.
Conditions return True or False.
7. Python If-Else Practice Questions
Try solving these programs yourself:
Write a program to check whether a person is eligible to vote.
Write a program to check whether a number is positive or negative.
Write a program to find the largest of two numbers.
Write a program to check whether a student has passed or failed.
Write a program to check whether a number is even or odd.
Conclusion
Python if-else statements are important for decision-making in programming. They help programs execute different instructions according to given conditions.
Practise these examples regularly to improve your Python programming skills.
Keep learning and keep practising Python!
Frequently Asked Questions (FAQs)
1. What is an if-else statement in Python?
An if-else statement is used to execute different blocks of code depending on whether a condition is True or False.
2. What is the difference between if and else?
The if block executes when a condition is True, while the else block executes when the condition is False.
3. What is elif in Python?
The elif keyword is used to check additional conditions when the previous condition is False.
4. Why is indentation important in Python?
Indentation defines blocks of code and helps Python identify which statements belong together.
5. Can we use multiple elif statements in Python?
Yes. We can use multiple elif statements to check multiple conditions.
Comments
Post a Comment