Conditional Statement In Python

What is a Conditional Statement In Python?

A Conditional Statement In Python programming language is a feature that allows the execution of different code blocks based on certain conditions. It helps the program make decisions and execute particular code sections depending on whether a condition is true or false.

Conditional statements typically involve comparison operators (like ==, >, <, !=) and logical operators (and, or, not).

Types of Conditional Statement Python:

if Statement:

if-else Statement:

elif Statement (short for “else if”):


if Conditional Statement Python:


In Python, the if conditional statement executes a block of code if a specific condition is true. It allows the program to make decisions based on certain conditions. If the condition evaluates to True, the indented code block under the if statement is executed. If the condition is False, the program skips the code inside the if block.

Syntax of an if Statement:

if condition:
—-# Code to execute if the condition is True

Example of an if Statement:

temperature = 30
if temperature > 25:
—-print(“It’s a hot day.”)

In this example, if the value of temperature is greater than 25, the message "It's a hot day." will be printed.

Key Points:
  • The condition is any expression that evaluates to True or False.
  • Python uses indentation (spaces or tabs) to define the block of code inside the if statement.
  • If the condition is false, the block of code inside the if statement is skipped.

Example with Input:

age = int(input(“Enter your age: “))

if age >= 18:

       print(“You are eligible to vote.”)

In this case, the message is displayed if the user enters the age of 18 or older. If the age is less than 18, nothing happens.

Click the Below Link to watch the Video Content about if Statement in Python:

if Conditional Statement in Python:


if else Condition Statement Python:


In Python, the if-else statement executes one block of code if a condition is true, and another block of code if the condition is false. The else part ensures that when the if condition is not met, an alternative action is taken.

Syntax of an if-else Statement:

if condition:
—-# Code to execute if the condition is True
else:
—-# Code to execute if the condition is False

Example of an if-else Statement:

age = 16
if age >= 18:
—-print(“You are an adult.”)
else:
—-print(“You are not an adult.”)

In this example:

  • If age is 18 or more, the program prints "You are an adult."
  • If age is less than 18, the program prints "You are not an adult."
Key Points:
  • The if condition is checked first. If it’s True, the indented block under the if statement runs.
  • If the if condition is False, the block under else is executed.
  • Only one of the blocks (either if or else) is executed based on the condition.

Example with Input:

number = int(input(“Enter a number: “))
if number % 2 == 0:
—-print(“The number is even.”)
else:
—-print(“The number is odd.”)

In this case:

  • If the number is divisible by 2 (even), it prints "The number is even."
  • Otherwise, it prints "The number is odd."

The if-else structure is useful when handling two possibilities based on a single condition.

Click the Below Link to watch the Video Content about if-else Statement in Python:

If-else Conditional Statement in Python:


Read More About Python Syntax and Its Structure:
Video Tutorials about Operators in Python:

if-elif-else Statement

The if-elif-else statement is used to check multiple conditions. If the first condition is false, it checks the next elif condition. If all conditions are false, the else block is executed.

Syntax:

if condition:
—-# Code to execute if condition1 is True
elif condition2:
—-# Code to execute if condition1 is False and condition2 is True
else:
—-# Code to execute if all conditions are False

Example:

Program: Check Grades Based on Marks

marks = 75
if marks >= 90:
—-print(“Grade: A”)
elif marks >= 80:
—-print(“Grade: B”)
elif marks >= 70:
—-print(“Grade: C”)
else:
—-print(“Grade: D”)

How the program works:

  1. The user enters their marks as an integer.
  2. The if statement checks if the marks are 90 or above and assigns Grade A.
  3. The elif statements check for other ranges (80-89 for B, 70-79 for C, etc.).
  4. If none of the conditions are true, the else block assigns Grade F for marks below 60.

This is a typical use of elif to handle multiple conditions sequentially.


Here are 5 programming questions based on if-elif-else statements:

  1. Body Mass Index (BMI) Classification: Write a program that calculates the BMI from the user’s height and weight, then classifies it as underweight, normal weight, overweight, or obese using these ranges:
    • BMI < 18.5: Underweight
    • 18.5 ≤ BMI < 24.9: Normal weight
    • 25 ≤ BMI < 29.9: Overweight
    • BMI ≥ 30: Obese
  2. Grade Calculator: Write a program that takes the marks of a student as input and assigns grades based on the following criteria:
    • Marks ≥ 90: Grade A
    • 80 ≤ Marks < 90: Grade B
    • 70 ≤ Marks < 80: Grade C
    • 60 ≤ Marks < 70: Grade D
    • Marks < 60: Fail
  3. Determine the Type of Triangle: Write a program that takes the lengths of three sides of a triangle as input and determines whether the triangle is equilateral, isosceles, or scalene.
    • Equilateral: All three sides are equal.
    • Isosceles: Two sides are equal.
    • Scalene: All sides are different.
  4. Check Weather Condition: Write a program that takes the temperature as input and prints whether it’s freezing, cold, warm, or hot:
    • Temp < 0: Freezing
    • 0 ≤ Temp < 15: Cold
    • 15 ≤ Temp < 25: Warm
    • Temp ≥ 25: Hot
  5. Electricity Bill Calculation: Write a program to calculate the electricity bill based on the number of units consumed:
    • Units ≤ 100: Charge ₹5 per unit
    • 101 ≤ Units ≤ 300: Charge ₹7 per unit
    • Units > 300: Charge ₹10 per unit The program should calculate and print the total bill based on the number of units consumed.

These questions help practice the use of if-elif-else for making decisions based on multiple conditions.

Here are some programming questions based on the if statement:

  1. Check Positive or Negative Number:
    Write a program that asks the user to input a number and checks if the number is positive.
  2. Check if the Number is Even:
    Write a program that takes a number from the user and checks if the number is even.
  3. Check Voting Eligibility:
    Write a program that takes a person’s age as input and prints if the person is eligible to vote (18 years or older).
  4. Check Leap Year:
    Write a program that asks the user for a year and checks if it is a leap year (divisible by 4).
  5. Check If the Number is Greater Than 10:
    Write a program that takes a number from the user and checks if the number is greater than 10.
  6. Temperature Check:
    Write a program that takes the temperature as input and prints if it’s a hot day (temperature greater than 30°C).
  7. Check If Character is a Vowel:
    Write a program that takes a letter as input and checks if it is a vowel (a, e, i, o, u).
  8. Check Divisibility by 5:
    Write a program that asks for a number and checks if it is divisible by 5.
  9. Check If User Input is a Digit:
    Write a program that takes input from the user and checks if the input is a digit.
  10. Check If Age is Above 65:
    Write a program that takes a person’s age and checks if the person is a senior citizen (age greater than or equal to 65).

These questions focus solely on the use of the if statement for simple conditional checks.

 Conclusion:

Conditional statements in Python such as if, elif, and else are essential for controlling the flow of a program. Using these conditional statements allows you to make decisions based on specific conditions, allowing for more dynamic and responsive code. Understanding and mastering conditional statements in Python is crucial for writing efficient, logical programs and is a fundamental skill for any programmer.

Leave a Comment