Python – Control Statements
As we read earlier about Python Functions. Now we will study about if else elif statements in Python. In python programming language , python if else elif statements are considered to be control statements. In Python programs, when there is a condition, we use Python control statements like if , if-else, if-elif-else. so lets start.
if statement in python
“if” statement are the control statement which helps us to run a particular code when a certain conditions is satisfied.
if you want to print a message on the screen only when a condition is true then you can use if statement to accomplish this in programming.
Python “if” syntax :
if ( condition ): statement
Flow chart of “if” statement python :

Example :
x = 20 y = 100 if y > x : print("y is greater than x")
Output :
y is greater than x
Remember the Indentation ( white spaces at the beginning of a line ).
if – else statement python
“if-else” statement evaluates test expression and will execute body of if only when test condition is true. If the condition is false, body of else if executed. Indentation is used to separate the blocks.
python “if else” syntax :
if ( condition ): statement else: statement
Flowchart of “if-else” statement :

Example :
x = 100 y = 20 if y > x: print("y is greater than x") else: print("y is not greater than x")
Output :
y is not greater than x
elif statement python
The “elif” statement allows you to check multiple expression for TRUE and execute a block of code as one of the conditions evaluates to TRUE.
python “elif” statement :
if ( condition ): statement elif ( condition ): statement else: statement
Flowchart of “elif” statement :

Example :
x = 44 y = 44 if y > x: print("y is greater than x") elif x==y: print("x and y are equal")
Output :
x and y are equal
Nested if statements python
We can have a if…elif…else statement inside another if…elif…else statement. This is called nesting in computer programming. In other words, you can have if statement inside if statement.
Indentation is the only way to figure out the level of nesting.
Example :
x = 60 if x > 10: print("above ten") if x > 20: print("above twenty") else: print("but not above twenty")
Output :
above ten above twenty
Pass statement python
The “if” statement cannot be empty, but if you for some reason have an if statement with no content, put in the “pass” statement to avoid an error.
Example :
x = 20 y = 100 if y > x: pass
Output :
The above output will be blanked.
Some logical conditions
Python programming supports some logical conditions are :-
- Equals : x == y
- Not equals: x != y
- Less than : x < y
- Less than or equal to : x <= y
- Greater than : x > y
- Greater than or equal to : x >= y
The above conditions can be used in, most commonly in “if” statements and “Loops”.
Some Exercise Questions
Question 1 : Write a program to print Highest number in given three input number ?
Question 2 : Write a program to check input number is even or odd ?
«Previous Next» >