Introduction

Setup

Operators

variable

String

Number

Dtype & Comment

Lists

Tuples

Sets

Dictionaries

if else statement

For Loop

While Loop

iterator & iterable

Pre-Built functions

Function

Decorators

generators

Types of errors

Exceptions Handling

OOP

Regular expression

Create module

OS module

Date module

Random module

Math

Statistics module

Exercise 1

Exercise 2

Exercise 3

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Python if else statement

What is if..elif..else in python?

To use conditions inside a code if, elif, and else statements are used. For the first condition, always if statements is used. For the last condition, always else statements is used. If there are conditions more than 2 then between the first and last condition then always use elif statements. In condition, we will use comparison Operators.

The comparison Operators are:

1. Equals: a == b
2. Not Equals: a != b
3. Less than: a < b
4. Less than or equal to: a <= b
5. Greater than: a > b
6. Greater than or equal to: a >= b

How if else statement works?

In the if-else statement, we check conditions one by one. If the first condition is get matched then the code inside of that condition gets executed.
If the first condition doesn't match then it doesn't check or go to that code written inside that condition. It will go for the next condition but if the first condition get matched then it will don't check the other conditions. It will go inside that matched condition and will run those codes written inside that condition. If any condition doesn't get matched then the code written inside else will get automatically executed.

Example:1
Input
x=5
if x==5:
  print("Yes x is 5")
else:
  print("No x is not 5")
Output
Yes x is 5

In example 1 we have x variable and it has a value of 5. So we wrote a condition that if x is equal to 5 then print Yes x is 5 and if not then print x is not 5. In this case, x is 5 so it's printed Yes x is 5.
How this code works?
So in the first condition, it checks that if x is equal to 5. In this case, x is equal to five. So it goes inside the first condition and executes the code written inside it. Because one condition gets matched so it will never go to check another condition.

Example:2
Input
x=6
if x==5:
  print("Yes x is 5")
else:
  print("No x is not 5") Output
No x is not 5

In example 2 we have x variable and it has a value of 6. So we wrote a condition that if x is equal to 5 then print Yes x is 5 and if not then print x is not 5. In this case, x is not 5, so it's printed No x is not 5.
How this code works?
So in the first condition, it checks if x is equal to 5 or not. In this case, x is not equal to five so this condition is false. Because we don't have any other condition so the code written inside else will automatically get executed.

Example:3
Input
if x>7:
  print(" sorry")
elif x==6:
  v=x**2
  print(v)
else:
   y=x**3
  print(y)
Output
36

In example 3 you have x variable and it has a value of 6. What you want to do here is that, if x is less than 5 then print sorry and if x is equal to 6 then do square of x and if all this condition doesn't match then do cube of x. The program will check the condition one by one and will give us a result. IN this case, x==6 condition is matched so you got the square of x.
How this code works?
So in the first condition, it checks that if x is greater than 7. In this case, x is less than 7. Because this condition is false so it will check the next condition. Next condition x is equal to 6. This condition is true so it will go inside this condition and execute code written inside it. So inside the second condition first it will do a square of x and then print the value.

Example:4
Input
x="121"
y=x[::-1]
if x==7:
  print(" Yes")
else:
  print(y)
Output
Yes

Nested if elif else statement in python

You can use if elif else inside an if elif or else. In nested conditions, the program will check first the parent condition or the outer condition and if the parent condition gets matched then it will check the inner or child condition. If the parent condition doesn't get matched then it will never check the child or inner condition.

Example:1
Input
x=4
if x< 5:
  if x<=4:
    print("yes")
  else:
    print("no")
elif x==6:
  v=x**2
  print(v)
elif x==1:
  v=x/2
  print(v)
else:
  y=x**3
  print(y)
Output
yes

In example 1 we have x variable and it has a value of 4. What we want to do is that, if x is less than 5 then if x is less than equal to 4 print, yes, and if not then print no. And if x is equal to 6 then do the square of x. If x is equal to 1 then divide x by 2 and if all these conditions don't get matched then do a cube of x. The program will check the conditions one by one condition and will give a result. In this case, x is less than equal to 4 so we got the result yes.

Example:2
Input
x=6
if x<5:
  if x<=4:
    print("yes")
  else:
    print("no")
elif x==6:
  if x<=3:
     v=x+2
     print(v)
  elif x<=2:
      z=x+3
     print(z)
  else:
      b=x**2
     print(b)
else:
   y=x**3
  print(y)
Output
36

In the example, we have x variable and it has a value of 6. What you want to do is that, if x is less than 5 then, if x is less than equal 4 then print yes and if not then print no. If x is equal to 6 and if x<=3 then do the sum of x+2 and if x<=2 then do the sum of x+3 and if not then do the square of x.And if all conditions are wrong then do cube of x. In this case, x is equal to 6 and because x<=3 and x<=2 are wrong, so we got the resulting square of x.

Use of or

To use more than one condition together and the code written inside of the condition will run, if at least one condition is true, then we will use or.

Example:
Input
x=6
if x>10 or x< 7:
  print("Yes x is 6")
else:
  print("No x is not 5")
Output
Yes x is 6

In the example, we have a variable x and the value of x is 6. We use the condition that if x is greater than 10 or less than equal to 6 then execute the code inside it or go next. In this case, the second condition(x< 7) gets matched so we got the result Yes x is 6.

Use of and

To use more than one condition together and the code written inside will run if all the conditions are true, then we will use and.

Example:
Input
x=6
if x< 10 and x< =6:
  print("Yes x is 6")
else:
  print("No x is not 5")
Output
Yes x is 6

In the example, we have a variable x and its value is 6. We will use the condition that if x is less than 10 and less than 7 then print Yes x is 6. If not then print x is not 5. Here x< 10 is true and x< 7 is also true so we got the result Yes x is 6.

Use of pass

If, elif and else statements cannot be empty. But if you have one for some reason with no content then put the pass statement inside if, elif and else to avoid getting an error.

Example:
Input
a = 30
b = 20
if b > a:
  pass
Output

Let's create a grading system mini program

Input
mark = int(input("Enter number to see the grade:"))
if mark>=80:
  print("A+")
elif mark>=70:
  print("A")
elif mark>=60:
  print("A-")
elif mark>=50:
  print("B")
elif mark>=40:
  print("C")
elif mark>=33:
  print("D")
else:
  print("Fail")

Let's create a mini program to identify even or odd number

Input
Number = int(input("Enter number to see the grade:"))
if Number%2==0:
  print("It is an even number")
else:
  print("It is an odd number")

Let's create a mini program to identify vowel or consonant word

Input
Word = input("Enter the word:")
if Word=="a" or Word=="e" or Word=="i" or Word=="o" or Word=="u":
  print("It is a vowel")
else:
  print("It is a consonant")

Let's create a mini guessing game

Input
Number = int(input("Enter number to guess between 1-60:"))
actual_number=55
if Number==actual_number:
  print("You win the game")
else:
  if Number>actual_number:
    Print("Too high")
  else:
    print("Too low")

Let's create a program to sell the movie tickets according to age

Input
age = int(input("Enter your age:"))
if 18< age<24:
  print("Price is 50")
elif 25< age<30:
  print("Price is 80")
elif 31< age<60:
  print("Price is 100")
elif age>60:
  print("Price is 50")
else:
  print("Sorry you are under age")

Let's create a program to sell adult movie tickets and those who will pay by card or online will get discount.

Input
age = int(input("Enter your age:"))
if age >=18:
payment = input("Enter payment method:")
  if age>=18 and(payment=="Card" or payment=="card" or payment=="Online" or payment=="online"):
    print("Price is 70 with 30% discount")
  elif :
    print("Price is 100")
else:
  print("Sorry you are under age")

Let's create a program to sell the adult movie tickets and whose name starts with the alphabet A and R will get discount

Input
age = int(input("Enter your age:"))
if age>=18:
    Name = input("Enter your Name:")
    if age>=18 and(Name[0]=="A" or Name[0]=="a" or Name[0]=="R" or Name[0]=="r"):
        print("Price is 70 with 30% discount")
    elif age >=18 :
        print("Price is 100")
else:
  print("Sorry you are under age")

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.