Instagram
youtube
Facebook
Twitter

if else statement

  • Conditional statements in python are used in a way like any other programming language.
  • In general, there are three conditional statements in python if, elif, and else.
  • if statement is the most widely used conditional statement.
  • There could be one or more than one elif depending on the requirements. elif is used in the same way as else if is used in most programming languages.
  • Usage of else is mostly optional.

Example - Program to check whether the number is zero, positive or negative.

my_num = int(input())
if num == 0:
    print("The number is zero")
elif num > 0:
    print("The number is a positive number")
else:
    print("The number is a negative number")

 

  • Here in the above example, we have taken an integer input 
  • According to the problem statement, we need to check whether the number is positive, negative, or zero.
  • if num == 0 is the first statement used to check if the number is zero and if it is true print the statement written inside if.
  • elif num>0 is used to check if the number is greater than zero then run the statement written inside elif.
  • else is used when there is no remaining range than the negative number.

 

and & or in the conditional statement

In Python and(&&) in conditional statement is used when there is more than one condition to be checked. And it is required that both the conditions are true to run any statement.

Example - Print "Excellent" if the marks obtained are greater than 90 and lesser than 100.

marks = int(input())
if marks > 90 and marks <100:
    print("Excellent")

In Python or(||) in conditional statement is used when there are more than one conditions to be checked. And it is required for one of them to be True.

Example - Print "Summer", if the temperature in the city is greater than 32 degrees C or the humidity index, is more than 10.

temp = int(input())
humidity = int(input())

if temp > 32 or humidity > 10:
    print("Summer")


 

Here are 20 practice coding questions on conditional statements in Python:

1. Write a Python program to check if a number is positive, negative, or zero.

2. Create a program that takes a user's age as input and determines if they are eligible to vote (18 or older).

3. Write a Python function that checks if a given year is a leap year.

4. Create a program that calculates the discount on a purchase based on the total amount. If the total is greater than $100, apply a 10% discount; otherwise, apply a 5% discount.

5. Write a program that takes a temperature in Celsius as input and converts it to Fahrenheit using the formula: F = (C * 9/5) + 32.

6. Build a Python program that calculates the BMI (Body Mass Index) of a person based on their weight (in kg) and height (in meters) and provides a classification (Underweight, Normal, Overweight, or Obese).

7. Create a program that determines whether a given number is even or odd.

8. Write a Python function that checks if a given string is a palindrome (reads the same forwards and backwards).

9. Develop a program that calculates the area of a triangle given its base and height.

10. Implement a Python program that simulates a basic calculator. It should take two numbers and an operator (+, -, *, /) as input and perform the corresponding calculation.

11. Write a program that takes a user's score as input and assigns a grade (A, B, C, D, or F) based on the following grading scale: A >= 90, B >= 80, C >= 70, D >= 60, F < 60.

12. Create a program that determines if a given year is a "century year" (ends in 00) and whether it's a leap year.

13. Write a Python function that finds the maximum of three numbers.

14. Build a program that checks if a given character is a vowel or a consonant.

15. Create a program that determines if a given number is a prime number.

16. Write a Python function that computes the factorial of a non-negative integer.

17. Develop a program that calculates the sum of all even numbers in a given range.

18. Implement a program that calculates the roots of a quadratic equation (ax^2 + bx + c = 0).

19. Write a Python program that checks if a given year is a "special year" (a year that is divisible by 4 but not by 100 or divisible by 400).

20. Create a program that takes two integers as input and finds their greatest common divisor (GCD) using the Euclidean algorithm.