Python Java JavaScript SQL C++ HTML CSS React
← prev next →
Python If-else statements
Decision making is the most important aspect of almost all the programming
languages. As the name implies, decision making allows us to run a particular block of
code for a particular decision. Here, the decisions are made on the validity of the
particular conditions. Condition checking is the backbone of decision making.
In python, decision making is performed by the following statements.
Statement Description
If Statement The if statement is used to test a specific
condition. If the condition is true, a block
of code (if-block) will be executed.
If - else Statement The if-else statement is similar to if
statement except the fact that, it also
provides the block of the code for the false
case of the condition to be checked. If the
condition provided in the if statement is
false, then the else statement will be
executed.
Nested if Statement Nested if statements enable us to use if ?
else statement inside an outer if
statement.
Indentation in Python
For the ease of programming and to achieve simplicity, python doesn't allow the use
of parentheses for the block level code. In Python, indentation is used to declare a
block. If two statements are at the same indentation level, then they are the part of
the same block.
Generally, four spaces are given to indent the statements which are a typical amount
of indentation in python.
Indentation is the most used part of the python language since it declares the block of
code. All the statements of one block are intended at the same level indentation. We
will see how the actual indentation takes place in decision making and other stuff in
python.
The if statement
The if statement is used to test a particular condition and if the condition is true, it
executes a block of code known as if-block. The condition of if statement can be any
valid logical expression which can be either evaluated to true or false.
The syntax of the if-statement is given below.
if expression:
statement
Example 1
# Simple Python program to understand the if statement
num = int(input("enter the number:"))
# Here, we are taking an integer num and taking input dynamically
if num%2 == 0:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("The Given number is an even number")
Output:
enter the number: 10
The Given number is an even number
Example 2 : Program to print the largest of the three numbers.
# Simple Python Program to print the largest of the three numbers.
a = int (input("Enter a: "));
b = int (input("Enter b: "));
c = int (input("Enter c: "));
if a>b and a>c:
# Here, we are checking the condition. If the condition is true, we will enter the block
print ("From the above three numbers given a is largest");
if b>a and b>c:
# Here, we are checking the condition. If the condition is true, we will enter the block
print ("From the above three numbers given b is largest");
if c>a and c>b:
# Here, we are checking the condition. If the condition is true, we will enter the block
print ("From the above three numbers given c is largest");
Output:
Enter a: 100
Enter b: 120
Enter c: 130
From the above three numbers given c is largest
The if-else statement
The if-else statement provides an else block combined with the if statement which is
executed in the false case of the condition.
If the condition is true, then the if-block is executed. Otherwise, the else-block is
executed.
The syntax of the if-else statement is given below.
if condition:
#block of statements
else:
#another block of statements (else-block)
Example 1 : Program to check whether a person is eligible to vote
or not.
# Simple Python Program to check whether a person is eligible to vote or not.
age = int (input("Enter your age: "))
# Here, we are taking an integer num and taking input dynamically
if age>=18:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("You are eligible to vote !!");
else:
print("Sorry! you have to wait !!");
Output:
Enter your age: 90
You are eligible to vote !!
Example 2: Program to check whether a number is even or not.
# Simple Python Program to check whether a number is even or not.
num = int(input("enter the number:"))
# Here, we are taking an integer num and taking input dynamically
if num%2 == 0:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("The Given number is an even number")
else:
print("The Given Number is an odd number")
Output:
enter the number: 10
The Given number is even number
The elif statement
The elif statement enables us to check multiple conditions and execute the specific
block of statements depending upon the true condition among them. We can have
any number of elif statements in our program depending upon our need. However,
using elif is optional.
The elif statement works like an if-else-if ladder statement in C. It must be succeeded
by an if statement.
The syntax of the elif statement is given below.
if expression 1:
# block of statements
elif expression 2:
# block of statements
elif expression 3:
# block of statements
else:
# block of statements
Example 1
# Simple Python program to understand elif statement
number = int(input("Enter the number?"))
# Here, we are taking an integer number and taking input dynamically
if number==10:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("The given number is equals to 10")
elif number==50:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("The given number is equal to 50");
elif number==100:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("The given number is equal to 100");
else:
print("The given number is not equal to 10, 50 or 100");
Output:
Enter the number?15
The given number is not equal to 10, 50 or 100
Example 2
# Simple Python program to understand elif statement
marks = int(input("Enter the marks? "))
# Here, we are taking an integer marks and taking input dynamically
if marks > 85 and marks <= 100:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("Congrats ! you scored grade A ...")
elif marks > 60 and marks <= 85:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("You scored grade B + ...")
elif marks > 40 and marks <= 60:
# Here, we are checking the condition. If the condition is true, we will enter the block
print("You scored grade B ...")
elif (marks > 30 and marks <= 40):
# Here, we are checking the condition. If the condition is true, we will enter the block
print("You scored grade C ...")
else:
print("Sorry you are fail ?")
Output:
Enter the marks? 89
Congrats ! you scored grade A ...
Next Topic Python Loops
← prev next →
Related Posts
Python While Loop
s In coding, loops are designed to execute a specified code block repeatedly.
We'll learn how to construct a while loop in Python, the syntax of a while
loop, loop controls like break and continue, and other exercises in this tutorial.
Introduction of In this article, we...
9 min read
Python Break
Python break statement The break is a keyword in python which is used to bring
the program control out of the loop. The break statement breaks the loops one
by one, i.e., in the case of nested loops, it breaks the inner loop first and then...
3 min read
Python Pass
Statement In this tutorial, we will learn more about past statements. It is
interpreted as a placeholder for future functions, classes, loops, and other
operations. What is Python's Pass Statement? The pass statement is also
known as the null statement. The Python mediator doesn't overlook a
Remark,...
2 min read
Python Loops
The following loops are available in Python to fulfil the looping needs. Python
offers 3 choices for running the loops. The basic functionality of all the
techniques is the same, although the syntax and the amount of time required
for checking the condition differ. We can run...
6 min read
Python For Loop
Python for loop Python is a strong, universally applicable prearranging language
planned to be easy to comprehend and carry out. It is allowed to get to because
it is open-source. In this tutorial, we will learn how to use Python for loops, one of
the most fundamental...
4 min read
Python Continue
Python continue Statement Python continue keyword is used to skip the
remaining statements of the current loop and go to the iteration. In Python,
loops repeat processes on their own in an efficient way. However, there might be
occasions when we wish to leave the...
3 min read
Subscribe to Tpoint Tech
We request you to subscribe our newsletter for upcoming
updates.
Your Email Subscribe
Learn Important Tutorial
Python Java
Javascript HTML
Database PHP
C++ React
B.Tech / MCA
Data
DBMS
Structures
DAA Operating
System
Computer Compiler
Network Design
Computer Discrete
Organization Mathematics
Ethical Computer
Hacking Graphics
Web Software
Technology Engineering
Cyber
Automata
Security
C C++
Programming
Java .Net
Python Programs
Control Data
System Warehouse
Preparation
Aptitude Reasoning
Verbal Interview
Ability Questions
Company
Questions