# If-else In Python

If-else in python used to check conditions. These statements are also used to make decisions.

before we move further have a quick look at comparison operators, which we will use in if-else statements.

### Comparison Operators

There are 7 Types of operators in python including conditional operators.

Now, Let’s have a look at syntax :

## If-elif-else syntax

This is the syntax of if-elif-else statement.

``````#If Statement
if condition:
code
#elif Statement
elif condition:
code
#Else Statement
else:
code``````

## Flowchart of If-elif-else

This is the flowchart of how this conditional statement(if-elif-else) works.

### Quick Example

Think, you have to validate the number. Here are the conditions :

• If number is 5, print “Number is 5”
• Number is less than 5, print “Number is less than 5”
• Number is greater than 5 , print “Number is greater than 5”

And here is the code :

``````#Initial variable "number" and it's value is 5
number = 5

if number < 5:
print("Number is less than 5")
elif number == 5:
print("Number is 5")
else:
print("Number is greater than 5")``````

Explanation :

1. Here we first take a variable “number” and give it a value 5
2. Then we started with if statement which has a condition number < 5, if it will be true then print statement will execute, otherwise it will check for elif statement.
3. Now condition number == 5 will true then print statement of elif statement will be execute otherwise else statement will execute.

This is a quick example of if-elif and else statement.

### Nested if-else Statement in Python

If we have if-elif-else in another if-elif-else then it called nested in programming

Let’s have a look ate example,

``````number = int(input("Enter Number :"))

if number >= 0:
if number == 0:
print("Number is Zero")
else:
print("Number is greater then 0")
else:
print("Number is Negative")``````

Here we used input() function which is in-built in python and it is used for taking input from user.

### Output

``````Enter Number : 5
Number is greater then 0``````
``````Enter Number : 0
Number is Zero
``````
``````Enter Number : -8
Number is Negative``````

This is all about if-elif-else conditional statements in python.

### Project

• Take input hour from user
• If hour < 12 print “Good Morning”
• If 12 < hour < 5 “Good Afternoon”
• Else print “Good Evening”

## Variables In Python

Variables are used to store values in the memory of a computer. a variable name is a name that given to the memory block of the computer to store the…

## 7 Best IDEs And Text Editors For Python

I found 7 best IDEs and text editors for python. IDEs and text editors make coding fun!! But somewhat you wondering, How? Text editor and IDEs have some basic features…

## Python Lists

Lists are Non-Primitive data structure in python. Lists are the ordered collection of data. It is changeable. List contains different Primitive Data types like String, Integer, Float and Boolean. So…

## Operators In Python

Operators in python is a symbol that perform certain operation on one or more variable or a value. There are 7 types of operators in python Arithmetic operators Comparison operators…

## How to Create API in Python using Flask

There are many powerful frameworks to create an API like Django and Flask. in this tutorial I’ll show you How to Create API in Python using Flask. Before creating an…

## Loops In Python

There are 2 type of loops in python. for loop while loop Let’s have detailed information about loops in python. For loops in Python for loops in python used for…