Decision Making in Python is primarily accomplished using conditional statements. Conditional statements allow you to execute different blocks of code based on whether a certain condition or expression is True or False. Python provides several ways to perform decision-making, with the most common ones being the if, elif (else if), and else statements.
Decision Making in Python
There might come some situation where we need to take some decision , on that decision further execution of the code depends.
Decision making statements decides the output or result of a program. In python we can make decision using –
If Statement
If -Else statements
Nested If statements
If-Elif Ladder
Short- Hand if-else statement
Short-Hand If statement
If-Statement :
If statement is the simplest decision making statemet in Python programming. It is use to decide weather a particular statement is True or not.
If the condition holds true , the statemet inside the’IF’ will execute else it will not.
#if-elif statement
me=''
if(me=='Prepster'):
print('Welcome to PrepInsta')
elif(me!='PrepSter' and len(me)>0):
print('You are welcome to become a Prepster.')
else:
print('You should not be here.')
Output :
You should not be here.
Short Hand If statement :
Whenever there is only one statement inside the If loop.
The statement can be put on the same line as the if statement.
Login/Signup to comment