Article by: Manish Methani
Last Updated: October 6, 2021 at 10:04am IST
Python's conditional statements are used for decision-making in programs. One of the most commonly used conditional statements is the 'if-else' statement. The 'if-else' statement is used when you want to execute a block of code based on certain conditions. In this tutorial, we will discuss the 'if-else' statement in Python, along with practical codes, examples, and explanations.
The syntax for the 'if-else' statement in Python is as follows:
if condition: # Execute this block of code if the condition is true else: # Execute this block of code if the condition is false
2. Practical Examples of If-Else Statement in Python:
Example 1: Comparing values
x = 10 y = 20 if x > y: print("x is greater than y") else: print("y is greater than x")
Output: y is greater than x
Explanation: In this example, we have two variables x and y. We are comparing their values using the 'if-else' statement. Since x is not greater than y, the output will be "y is greater than x".
Example 2: Checking user input
age = int(input("Enter your age: ")) if age >= 18: print("You are eligible to vote") else: print("You are not eligible to vote")
Output: You are eligible to vote
Explanation: In this example, we are taking user input for their age. We are then using the 'if-else' statement to check whether the user is eligible to vote or not. If the user's age is greater than or equal to 18, the output will be "You are eligible to vote". Otherwise, the output will be "You are not eligible to vote".
Example 3: Checking if a number is even or odd
num = int(input("Enter a number: ")) if num % 2 == 0: print("The number is even") else: print("The number is odd")
Output:
Enter a number: 10 The number is even
Explanation: In this example, we are taking user input for a number. We are then using the 'if-else' statement to check if the number is even or odd. If the number is divisible by 2, it is even. Otherwise, it is odd.
The if-else-elif statement is a decision-making statement in Python programming that allows the programmer to execute specific code based on certain conditions. The if statement checks for a particular condition and executes the code within the block if the condition is true. The else statement executes a block of code when the condition in the if statement is false. The elif statement, which is short for "else if", allows the programmer to check for multiple conditions and execute different blocks of code based on the conditions that are true.
Syntax of if-else-elif statement:
if condition1: statement1 elif condition2: statement2 else: statement3
num = float(input("Enter a number: ")) if num > 0: print("The number is positive.") elif num == 0: print("The number is zero.") else: print("The number is negative.")
2. Program to check if a student has passed or failed a subject:
marks = int(input("Enter your marks: ")) if marks >= 40: print("Congratulations! You have passed.") else: print("Sorry, you have failed.")
In this tutorial, we have discussed the 'if-else' statement in Python, along with practical codes, examples, and explanations. The 'if-else' statement is an essential tool for decision-making in programs. By understanding how to use the 'if-else' statement, you can write more efficient and effective programs in Python.