In this tutorial you will learn about the Python Decision Making and its application with practical example.
In Python, decision-making statements allow you to make a decision, based upon the result of a test condition. The decision-making statements are also referred to as Conditional Statements. Decision Making Statement enable computer to decide which block of code to be execute based on some conditional choices. In Python, if statement, if else statements, elif statements, nested if conditions and single statement conditions used as decision-making statements.
Decision Making Statements In Python
In python, we have a rich set of Decision Making statements that enable computers to decide which block of code to be executed based on some conditional choices. Decision making statement evaluates single or multiple test expressions which results “TRUE” or “FALSE”. The outcome of the test condition helps to determine which block of statement(s) to executed if the condition is “TRUE” or “FALSE” otherwise.
Types of Decision-Making Statements
In Python, we have following types of decision making statements :
- if statement
- if-else statement
- if-elif-else ladder statement
- nested if statements
Python if statement
Block of statement executed only when specified test expression is true.
1 2 |
if(expression): statement |
Python if-else statement
When we want to execute some block of code if a condition is true and another block of code if a condition is false, In such a case we use if….else statement.
1 2 3 4 |
if(expression): statement else: statement |
Python if-elif-else ladder
1 2 3 4 5 6 7 8 9 10 |
if(expression1): statement elif(expression2) : statement elif(expression3): statement . . else: statement |
Python Nested if Statements
When there is an if statement inside another if statement then it is known as nested if else.
1 2 3 4 5 6 7 |
if (expression): if(expression): statement of nested if else: statement of nested if else statement of outer if statement outside if block |
tyu