Python If Else Statements – Conditional Statements with Examples

Tutorial Playlist

Python if-else statements are used extensively in decision-making code. In real-world applications, there are numerous cases where you must run a certain group of code depending on certain conditions. These statements will help you write logical and efficient code as well as assist you in managing user inputs, validating data, and controlling programs. Here in this article, you will see various ways to implement if-else statements in Python with real-time examples with step-by-step illustrations.

Table of Contents:

Python Conditional Statements

In Python, conditional statements control the flow of a program by making decisions based on the given conditions. These statements help in determining the execution of the programs by evaluating the conditions, such as True or False. This helps in making the Python program more interactive, as it responds to different situations automatically and returns the output. Adding conditional statements to your Python code is necessary to control the program flow and improve the process of decision-making, which makes the code organized and efficient.

There are 4 types of conditional statements in Python:

  • if statement
  • if-else statement
  • if-elif-else statement
  • Nested if statement

1. If Statement in Python

If Statement in Python

The if statement checks a condition and executes the code only if the condition is True and does not return any output if the condition is False. It is very useful for controlling the flow of the program and helps ensure that the specific code runs only when it is needed.

Syntax:

if condition:
# Code to execute if the condition is True
# This block runs only when the condition is met

Example:

Python
# Defining the variable named age
age = 20
# Checking if the age is greater than or equal to 16
if age >= 16:
print("You can enroll in the Intellipaat course!")
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

If Statement in Python Output

Explanation: Here, the program checks if the age is greater than or equal to 16, prints a message if the condition is true, and does not return any output if the condition is False.

2. If…Else Statement in Python

The if-else statement runs one block of code if the condition is True and another if the condition does not match and is False. This ensures that the Python programs return some statements.

The following flowchart will show you how the If…Else statement in Python works:

If...Else Statement in Python

Syntax:

if condition:
# Statement executes if the condition is True
else:
# Statement executes if the condition is False

Example:

Python
# Defining the variable named marks
marks = 75
# Checking if marks are greater than or equal to 60
if marks >= 60:
print("You are eligible for the Intellipaat certification!")
else:
print("Keep learning and try again!")
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

If...Else Statement in Python Output

Explanation: Here, it is checked whether the marks are equal to or more than 60, and prints the message only if the condition is satisfied.

3. If…Elif…Else Statement in Python

If…Elif...Else Statement in Python

The if-elif-else statement checks multiple conditions one after the other and executes the condition that is satisfied first and is true. It is useful if there is a possibility of multiple results.

Syntax:

if condition1:
# Code executes if condition1 is True
elif condition2:
# Code executes if condition1 is False and condition2 is True
else:
# Code executes if neither condition1 nor condition2 is True

Example:

Python
# Defining the variable named score
score = 85
# Check if the score is 90 or above
if score >= 90:
print("You are in the top category for the Intellipaat course!")
elif score >= 60: # Added a colon (:) after elif condition
print("You qualify for the standard course!")
else: # 'Else' should be lowercase ('else')
# Prints if the score is below 60
print("Keep learning to improve your score!")
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

If…Elif...Else Statement in Python Output

Explanation: Here, different messages are assigned based on the value of the score.

4. Nested if Statement in Python

Nested if Statement in Python

A nested if statement is an if statement inside another if. It is used when multiple conditions should be met and returns the statement only if all the conditions are satisfied.

Syntax:

if condition1:
if condition2:
# Statement if both conditions are True

Example:

Python
# Defining the age and the status of course registration
age = 18
course_registered = True
# Checking if the user is 16 or older
if age >= 16:
# Checking if the user has registered for the course
if course_registered:
print("Welcome to Intellipaat's advanced course!")
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Nested if Statement in Python Output

Explanation: Here, it is checked if the age of the user is more than 16,  then confirms course registration before displaying the message.

Shorthand If and If…Else in Python

Shorthand if and if-else is nothing but a way to write the if and the else block without any indentation in a single line. This is very useful when we have only one statement to be executed in the if block and the else block.

Shorthand If Syntax:

# Syntax for a shorthand if statement
if condition: statement

Example:

Python
# Checking if a student completed 80% of the course
completion_percentage = 90
# Using shorthand if
if completion_percentage >= 80: print("You are eligible for the Intellipaat certification!")
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Shorthand If and If…Else in Python Output

Explanation: Here, if the completion_percentage is more than 80 percent, then the message is printed.

Shorthand If…Else Syntax:

# Syntax for a shorthand if-else statement
statement_if_true if condition else statement_if_false

Example:

Python
# Checking if a student passed the Intellipaat exam
marks = 75
# Using shorthand if-else
print("Congratulations! You passed the Intellipaat exam!") if marks >= 60 else print("Keep learning and try again!")
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Shorthand If...Else Syntax Output

Explanation: Here, if marks are 60 or more, it prints “Congratulations! You passed the Intellipaat exam!”. Otherwise, it prints “Keep learning and try again!”.

Logical Operators with If…Else Statements in Python

Logical operators help to combine multiple conditions in if-else statements, which makes the decision-making more flexible. They allow checking multiple conditions at once instead of writing separate if statements.

1. Using ‘and’ Logical Operator with If-Else in Python

The and keyword is used when multiple conditions need to be checked. It makes sure that, to execute the statement, both conditions must be true. If any condition is False, it returns another statement.

Example:

Python
# Check if a number is within a specific range
num = 15
# Both conditions must be met
if num > 10 and num < 20:
print("The number is between 10 and 20.")
else:
print("The number is out of range.")
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Using ‘and’ Logical Operator with If-Else in Python

Explanation: Here, the and operator ensures that both conditions (num > 10 and num < 20) must be True.

2. Using or Logical Operator with If…Else in Python

The or keyword is used when at least one of the conditions mentioned is True. If any condition is True, the block under it is executed. If both conditions are False, the other statement, which is given inside the else block, is executed.

Example:

Python
# Check if a number is either too low or too high
num = 5
# At least one condition must be met
if num < 10 or num > 50:
print("The number is out of the normal range.")
else:
print("The number is within the normal range.")
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Using or Logical Operator with If...Else in Python

Explanation: Here, the ‘or’ operator checks if at least one condition is True. Since num is less than 10, the statement is executed, even though num is not greater than 50.

3. Using not Logical Operator with If…Else in Python

The not keyword is used to reverse a condition. If the condition is True, then this keyword makes it False. This is useful when checking for the opposite of a condition.

Example:

Python
# Check if a number is not positive
num = -3
# If the number is not positive
if not num > 0:
print("The number is not positive.")
else:
print("The number is positive.")
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Using not Logical Operator with If…Else in Python

Explanation: Here, the not operator gives the condition num > 0. Since num is not greater than 0, the statement under the if is executed successfully.

Using If…Else Statements Inside Functions in Python

Using if-else statements inside Python functions makes the code more structured, reusable, and efficient. Functions allow you to encapsulate logic and execute conditional checks based on user input or given values.

Example:

Python
# Function to check if a person is eligible to vote
def check_voting_eligibility(age):
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
# Call the function with an age value
check_voting_eligibility(16)
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Using If…Else Statements Inside Functions in Python Output

Explanation: Here, the function check_voting_eligibility(age) checks if the age of the voter is greater than or equal to 18 and prints the eligibility status.

Working with If…Else Statements in Loops

If-Else statements inside loops allow decision-making during each iteration, which helps to execute different blocks of code based on conditions.

Using If…Else in a For Loop

A for loop runs a block of code a fixed number of times, and if-else inside it checks conditions for each iteration.

Example:

Python
# For loop with if-else to check even and odd numbers
for num in range(1, 6):
if num % 2 == 0:
print(num, "is even")
else:
print(num, "is odd")
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Using If…Else in a For Loop Output

Explanation: The loop runs from 1 to 5. The if condition checks whether the number is even or odd and prints the result accordingly.

Using If…Else in a While Loop

A while loop runs repeatedly as long as a condition is True, and if-else inside it helps make decisions during each iteration.

Example:

Python
# While loop with if-else to count down
num = 5
while num >= 0:
if num == 0:
print("Reached zero!")
else:
print("Counting down:", num)
num -= 1
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Using If…Else in a While Loop Output

Explanation: The loop starts at 5 and decreases until it reaches 0, printing a different message for zero.

Best Practices for Using If Statements in Python

  • Use Proper Indentation: Always keep code within if, elif, and else properly indented, which helps to prevent mistakes and increase the readability.
  • Keep Conditions Simple: Complex conditions should not be used, and always try to break the condition into smaller parts to make it simple to understand the condition.
  • Use elif instead of Multiple if: When there are multiple conditions to be checked, elif has to be used so that the checking can be stopped once the condition is matched.
  • Include else: The else statement has to be added to return some message when the condition is not True.
  • Use Boolean Variables: A proper variable name has to be given for the True or false condition. You can use marks when you are adding marks instead of the variables x and y, which helps in improving the readability.

Comparison of Conditional Statements in Python

Features if Statement If…Else Statement If…Elif…Else Statement Nested if Statement
Speed Fast, as it checks only one condition. Fast, as it checks one condition, then stops, Moderate, checks conditions one after other until a match is found. Slow, checks conditions inside conditions, which increases the time for execution.
Readability Very easy to understand, as it’s a single condition. Clear and simple, as it handles two outcomes directly. Clear but can be long, mainly when there are multiple conditions. Difficult to read, as there are multiple statements used with a lot of indentations.
Efficiency Runs only when needed, but lacks a fallback option. Efficient because there are no unnecessary checks. More efficient compared to multiple if statements Less efficient as each condition must be checked separately.
Flexibility No, only for one condition. Flexible, but only for two conditions. Very flexible, handles multiple conditions. Less flexible, as one condition depends on another.
Code Length Short and simple with only one condition. Short and structured, as it handles two conditions. Moderate length and structured Lengthy compared to others, as there are multiple nested conditions,
Best for Simple and single conditions, like checking if the numbers are positive. For two choices, like checking pass or fail. Selecting one from many, like choosing colours. When one condition depends on the other.

Real-World Examples of the If…Else Statement in Python

The Python If…Else statement is used in many real-world cases. Now let’s see some of the most common use cases of it.

  1. Checking Exam Results
Python
marks = 75
if marks >= 60:
print("Congratulations! You passed the exam.")
else:
print("Sorry, you failed. Don't give up try again!")
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Checking Exam Results Output

Explanation: If the marks are greater than or equal to 60, it prints a pass message. Otherwise, it prints a fail message.

  1. Checking Login Credentials
Python
username = "Lithin"
password = "1234"
if username == "admin" and password == "1234":
print("Login Successful!")
else:
print("Invalid username or password.")
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Checking Login Credentials

Explanation: If both the username and password match, the login is successful. Otherwise, it fails.

  1. ATM Cash Withdrawal
Python
balance = 500
withdraw = 300
if withdraw <= balance:
print("Transaction successful!")
else:
print("Insufficient funds.")
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

ATM Cash Withdrawal

Explanation: If the withdrawal amount is less than or equal to the balance, the transaction proceeds; Otherwise, it fails.

Conclusion

The Python if-else statement is essential in controlling program flow and decision making. It is important to use the conditional operators like if statements, if-elif-else blocks, and shorthand expressions, which can be used to write simple 1 line statements along with the condition to write a clean, efficient, and organized Python Code. Learn these conditional statements thoroughly, and you will be able to write logic in different situations and improve the logic of your program.

To take your skills to the next level, enroll in our Python training course and gain hands-on experience. Also, prepare for job interviews with our Python interview questions, prepared by industry experts.

Python If...Else Statements - Conditional Statements with Examples - FAQs
1. What is the purpose of if-else in Python?

The if-else statement enables decision-making in Python by running a piece of code if a condition is True and another code if the condition is False.

2. Can we use else without if in Python?

No, the else has to be used after an if statement since it serves as a default statement if the condition is not satisfied.

3. What happens if there is no else after an if statement?

If there is no else, Python simply skips the if block when the condition is False and executes the rest of the code.

4. Is it possible to have more than one else block in an if-else statement?

No, an if-else statement can only have a single else block, but many conditions can be addressed with elif.

5. What is the shorthand If...Else in Python?

It is a concise method to express if-else in one line, which makes the code more readable and compact.

Our Python Courses Duration and Fees

Program Name
Start Date
Fees
Cohort Starts on: 5th Apr 2025
₹20,007

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.