0% found this document useful (0 votes)
54 views7 pages

Decision Making Statement in Python

Uploaded by

santoshdvg1997
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
54 views7 pages

Decision Making Statement in Python

Uploaded by

santoshdvg1997
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

Python Programming

Topperworld.in

Decision Making Statement

• 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 The if-else statement is similar to if statement except the fact


Statement 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 Nested if statements enable us to use if ? else statement inside


Statement 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.

©Topperworld
Python Programming

• If two statements are at the same indentation level, then they are the part
of the same block.
• 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.

❖ 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.

©Topperworld
Python Programming

Syntax:

1. if expression:
2. statement

Example:

# 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


1. if num%2 == 0:
2.
3. # Here, we are checking the condition.
4. print("The Given number is an even number")

Output:

enter the number: 10


The Given number is an even number

©Topperworld
Python Programming

❖ 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.

Syntax:

1. if condition:
2. #block of statements
3. else:
4. #another block of statements (else-block)

©Topperworld
Python Programming

Example:

1. # Simple Python Program to check whether a person is eligible to vot


e or not.
2. age = int (input("Enter your age: "))
3. # Here, we are taking an integer num and taking input dynamically
4. if age>=18:
5. # Here, we are checking the condition. If the condition is true, we wil
l enter the block
6. print("You are eligible to vote !!");
7. else:
print("Sorry! you have to wait !!");

Output:

Enter your age: 90


You are eligible to vote !!

❖ 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.

©Topperworld
Python Programming

Syntax:

if expression 1:
# block of statements
1.
elif expression 2:
# block of statements
2.
elif expression 3:
# block of statements
3.
else:
# block of statements

Example:

1. # Simple Python program to understand elif statement


2. number = int(input("Enter the number?"))
3. # Here, we are taking an integer number and taking input dynamica
lly
4. if number==10:
5. # Here, we are checking the condition. If the condition is true, we w
ill 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 w
ill 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 w
ill enter the block
print("The given number is equal to 100");
else:
6. print("The given number is not equal to 10, 50 or 100"); ©Topperworld

7.
Python Programming

Output:

Enter the number?15


The given number Is not equal to 10,50 or 100

©Topperworld

You might also like