Python If-Else Statement: Learn Conditional Statements with Codes and Examples

Article by: Manish Methani

Last Updated: October 6, 2021 at 10:04am IST
6 min 13 sec read

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.

  1. Syntax of If-Else Statement in Python:

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.

Python If-else-elif

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

Explanation:

  • The if statement checks for a particular condition (condition1) and executes the code within the block if the condition is true.
  • The elif statement allows the programmer to check for multiple conditions (condition2) and execute different blocks of code based on the conditions that are true.
  • The else statement executes a block of code (statement3) when the condition in the if statement is false.

Examples:

  1. Program to check if a number is positive, negative, or zero:
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.")

Explanation:

  • The program accepts a number as input from the user using the input() function.
  • The if statement checks if the number is greater than 0. If it is true, then the message "The number is positive." is printed.
  • If the condition in the if statement is false, the elif statement checks if the number is equal to 0. If it is true, then the message "The number is zero." is printed.
  • If both the conditions in the if and elif statements are false, then the else statement is executed, and the message "The number is negative." is printed.

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.")

Explanation:

  • The program accepts the marks obtained by a student in a subject as input using the input() function.
  • The if statement checks if the marks obtained by the student are greater than or equal to 40. If it is true, then the message "Congratulations! You have passed." is printed.
  • If the condition in the if statement is false, then the else statement is executed, and the message "Sorry, you have failed." is printed.

Conclusion:

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.

Explore Tech Guide:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com