Python if else elif

Filed Under: Python

Python if else and elif are keywords for conditional logic in the program. In this tutorial we are going to learn about python if, else and elif. Previously we learned about Python Operators and Precedence.

Python if else – conditional logic

Well, so far we dealt about static decision program. That means in our program we didn’t have to choose between any options. But what if we have to make our program behave differently in different condition. That’s where we will use conditional logic. So conditional logic is how we can make logical decision in a program.

To implement conditional logic, Python’s keywords are if, else and elif.

Python if

Suppose we want to write a program, that will determine whether a number is odd or even. If the number is odd, we want to print – “the number is odd” and if the number is even we want to print – “the number is even”. We can write this program using if keyword.


n=input() #take a input from user

n=int(n)  #typecast the raw input into integer

#check if n is odd or even
#logic for odd/even is-
#if we divide an even number by 2, the remainder will be zero
#if we divide an odd number by 2, the remainder will be one

#we can perform this logic with modulus operator (%)

if n%2==0: #(n%2) is the remainder.Check if it's zero
    print("the number is even")
if n%2==1: #Check the remainder is one
    print("the number is odd")

If we execute this program and give input 2, the output will be like below image.

Python if else elif

Also, if we run the program again and give input 3, the output will be like below.
Python tutorial, python if else example

Pretty cool, right? As if we have made an intelligence 😉

Python else

Well, in the above scenario, the condition we have put, n%2, which has only two possible outcome. Either it’s zero or one. So here we can use else for the second condition. In that case we don’t have to write the second condition manually. We can write the first condition using a if and use else for other case.


n=input() #take a input from user

n=int(n)  #typecast the raw input into integer

#check if n is odd or even
#logic for odd/even is-
#if we divide an even number by 2, the remainder will be zero
#if we divide an odd number by 2, the remainder will be one

#we can perform this logic with modulus operator (%)

if n%2==0: #(n%2) is the remainder.Check if it's zero
    print("the number is even")
else       #this will consider every other case without the above mentioned condition in if
    print("the number is odd")

Python elif

What if we have to write a program that will have to handle three or more condition. Suppose, you have to take a number from the user and consider these three cases.

  1. If the number is between 1 to 10 – print “too low”
  2. If the number is between 11 to 20 – print “medium”
  3. If the number is between 21 to 30 – print “large”
  4. If the number is greater than 30 – print “too large”

So, in this scenario, we have to use if for the first condition and else for the last condition. That much we know till now. So what about the other two? We will use elif to specify the other condition just like if.


n=input() #take a input from user

n=int(n)  #typecast the raw input into integer

#Check If the number is between 1 to 10
if n>=1 and n<=10:
    print("too low");

#Check If the number is between 11 to 20
elif n>=11 and n<=20:
    print("medium");   

#Check If the number is between 21 to 30
elif n>=21 and n<=30:
    print("large");

#Check if the number is greater than 30 
else:
    print("too large")

If we run this program for values 3, 15, 23, 45 respectively, the output will be like this-
Python if else elif example

So, that’s about conditional logic in Python. Make sure you run every piece of code on your own. Also it is a better practice to make some problems on your own and do those.
#happy_coding 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages