How Can We Help?
Introduction to Decision making in Python
Decision-making is an expectation of conditions occurring while executing the program and specifying actions taken according to the conditions.
Its structures evaluate multiple expressions which produce a boolean value as TRUE (non zero/non null) or FALSE (zero/null).
It separates the programs into their logic component and their control component. The logic component determines the solutions produced, and the control component can be varied to provide alternative ways of executing a logic program.
In Decision making, if the condition is true, the body code of the respective condition is executed; otherwise, it is skipped. (See the below image)
Suite: A group of individual statements which make a single code block is called ”Suites”.
Header Syntax:
<Keyword Header name>: (colon)
Eg If a>b: , elif a=b : etc
Complex statements, such as if, else, elif, for, while, etc., require a header line and a suite.
Header lines start the statement with the keyword, terminate with a colon ( : ), and are followed by one or more lines that make up the suite.
E.g., if expression: suite
elif expression: suite
else: suite
Use of Indentation:
In python, any type of brackets like (),{} etc. are not used to indicate the blocks of code under a header but use indentation of the statement under a header to group the statement.
Example (1):
a=10
if a==10:
print(a+5)
Output:
15
In the above example, we have used indentation to show a block of “if” code.
Note: The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount.
Python programming provides the following types of Condition Statements, which contain a logical expression.
Sr. | Condition Statements | Description |
---|---|---|
1 | “if” Statement | In this statement, an “if” statement consists of a boolean expression followed by one or more statements. |
2 | “if…else” Statement | It is used when only two conditions need to check, one for True and another for False. In this statement, an “if” statement is followed by an optional “else” statement, which executes when the boolean expression is False for the “if” statement. |
3 | “if…elif…else ” Statement | It is used when multiple conditions need to check. In this statement, an “if” statement is followed by an optional “elif” statement, which executes when the boolean expression is False for the “if” statement or previous “elif” statement. |
4 | Nested if Statement | A nested statement means one” if” or “elif” statement inside another “if” or “elif” statement. |
“if” Statement in Python
An“if” statement in python is the same as in other languages, used to test a condition.
The “if” statement contains a logical expression, using which data is compared and a decision is made based on the result of the comparison.
If the condition is true, the statement of the “if “block is executed; otherwise “if” block is skipped, and the code cursor moves to the next first set of code of the program to execute it. (See the below image.)
Syntax:
if <condition> :
<body of "if" >
Example (2):
year=2000
if year%4 == 0:
print("It's a leap year")
Output:
It’s a leap year
In the above example, year=2000, and the result of year%4=0 means the condition is True, so the code cursor moves to the if block, and the output shows a print statement of the if block.
“if….else” Statement in Python
An “if………else” statement in python is the same as in other languages, which is used to test two conditions.
The “else” statement is optional, and only one “else” statement follows the “if” statement.
An “else” block is executed if the logical expression of the “if” statement is False. (See the below image)
Syntax:
if <condition> :
<if body>
else:
<else body>
Example (3)
password="admin"
if password == 'admin' :
print("Successfully Login")
else:
print("Incorrect Password.retry!!!!")
Output:
Successfully Login
In the above example, the variable password is admin, and the condition password is also admin means the password=admin condition is true, so the code cursor moves to the “if” block, and the output shows a print statement of the ” if” block.
Example (4):
password="admin"
if password == 123 :
print("Successfully Login")
else:
print("Incorrect Password.retry!!!!")
Output:
Incorrect password.retry!!!!
In the above example, the variable password is 123, and the condition password is admin means the password=admin condition is false, so the code cursor moves to the “else” block, and the output shows the print statement of the “else” block.
“if…elif…else” Statement in Python
The “if…elif…else” statement is used to test multiple conditions, or when we need to check for multiple conditions to be true, then we use the “elif “Statement with “if…else”.
An “if…elif…else” statement takes the form of an “if” expression, followed by one or more optional “elif” expressions, and ends with an optional “else” block.
The “elif” statement is an optional statement like “else”.
An “elif” block is executed if the logical expression of the “if” statement is False or the previous “elif” statement is False. (See the below image).
An “elif” execute a block of code as soon as one of the conditions evaluates to True.
Syntax:
if <condition> :
<if body>
elif <condition_1>:
<elif body_1>
:
:
:
elif <condition_n>:
<elif body_n>
else:
<else body>
Note: Python does not provide a switch or case statements like other languages, but we can use “if..elif”…statements to simulate switch cases.
Example (5):
num=21
guess=10
if guess==num:
print("Wow, You guess the right number..")
elif guess<num:
print("Sorry, wrong guess. number is a little higher..")
else:
print("Sorry, wrong guess. number is a little lower..")
Output:
Sorry, wrong guess. Number is a little higher..
In the above example, variables as num=21 and guess=10, which shows guess\<num, means the second condition is true, which is given in the “elif” block, so the output shows the print statement of the “elif” block.
Nested if Statements in Python
When we need to check another condition after a condition, then we use Nested if Statements
This statement is like executing an “if” statement inside an “if” or “else” statement.
In nested if statements, we can have an “if…elif…else” statement inside another “if…elif…else” statement.
Syntax :
if<condition1>:
<if body>
if<condition1>:
<if body>
elif <test condition2>:
<elif body>
elif <test condition3>:
<elif body>
else:
<else body>
elif <test condition2>:
<elif body>
else:
<else body>
Example (6):
deposit=150000
if deposit>100000:
print("Thanks for choosing our bank.")
if deposit< 200000:
print("We give you a VIP locker.")
elif deposit<500000:
print("We need to talk to the manager.")
else:
print("Go our another branch for deposit.")
elif deposit>10000:
print("Do you want to deposit it as an FD?")
elif deposit > 1000:
print("Sure, I am doing it right now.")
else:
print("Sorry, we are not processing less amount.")
Output:
Thanks for choosing our bank.
We give you a VIP locker.
In the above example, variable deposit=150000, and if we compare this value with given conditions, then it satisfied two conditions as first “if” condition as deposit>100000 and the “nested if” condition as deposit\< 200000, so output display the print statement of both conditions.
IF Condition Statements with Operators
A condition statement is worked with all python operators.
Let’s see some examples below.
NOT, > Operator
Example (7):
num1=2
num2=4
if not (num1 > num2):
print(num1 > num2)
Output:
False
AND, OR, = =, % Operator
Example (8):
a=10
b=20
if a%2= = 0 and b%2 = = 0:
print("Both numbers are even numbers")
elif a%2 = = 0 or b%2 = = 0:
print("Either a or b is an even number")
else:
print("Both numbers are odd numbers")
Output:
Both numbers are even numbers
IN Operator
Example (9):
password=input("Enter your password: ")
if password in ['admin','jlbets123']:
print("Successfully login")
else:
print("Wrong password")
Output:
Successfully login
Conclusion
Decision-making is a common concept in all programming languages that lets the developer build their algorithm to cover all business cases. We have three main types of decision-making in Python, which are (if statement, if…else statement, and if…elif…else statement) that we demonstrate it in this article by giving multiple examples for better understanding.