**Python if else and elif** are keywords for conditional logic in a program. In this tutorial, we are going to learn about python `if`

, `else`

and `elif`

. Previously we learned about Python Operators.

Table of Contents

## How to use Python if else condition?

Well, so far we dealt with a 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 conditions. That’s where we will use conditional logic. So conditional logic is how we can make a logical decision in a program.

To implement conditional logic, Python’s keywords are `if`

, `else`

and `elif`

.

### 1. Using the Python if else condition

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 the below image.

Also, if we run the program again and give input 3, the output will be like below.

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

Well, in the above scenario, the condition we have put, `n%2`

which has only two possible outcomes. 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 as shown below:

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

### 2. Handling Multiple Conditions with Python elif

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

- If the number is between 1 to 10 – print “too low”
- If the number is between 11 to 20 – print “medium”
- If the number is between 21 to 30 – print “large”
- 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-

## Conclusion

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 ๐

There’s a “:” left out in the “Python else” example. It should be “else: “

Thanks for pointing out the typo error, I have fixed it.