In real life, there are times when we must make choices and decide what to do next based on those decisions. Similar scenarios occur in programming where we must make decisions and then execute the next block of code based on those decisions.
In programming languages, decision making statements determine the flow of program execution.
If..Else Statement in Python
1)If statement
The if statement is the simplest decision-making statement. It is used to determine whether or not a specific statement or block of statements will be executed, i.e. if a certain condition is valid, then a block of statements is executed, otherwise not.
Syntax:
if condition: # statements which should be executed
In this case, the condition will be either true or false after evaluation. The if statement accepts boolean values – if the value is valid, the block of statements below it will be executed; otherwise, it will not. We may also use condition with bracket ‘(‘ ‘)’.
Python, as we know, uses indentation to define a block. As a result, the block inside an if statement will be found, as seen in the example below.
if condition: statement1 statement2 #If the condition is valid, the if block will only consider # statement1 to be within its block.
Example:
age = 18 if (age > 15): print("age is greater than 15") print("Statement after if block")
Output:
age is greater than 15 Statement after if block
2)If-else statement
When the interpreter encounters an if argument, it evaluates the condition in the if-statement, and if that condition evaluates to True, it executes the suite in the if-block, i.e., the statements in the if-block and skips the statements in the else portion.
If the condition in the if-statement evaluates to False, the suite in the else block is executed directly, i.e., the statements in the else block are executed instead of the statements in the if block.
Syntax:
if condition: statement_1 statement_2 statement_3 else: statement_4 statement_5 statement_6
3)Examples of If else statement
age = 13 if (age > 15): print("age is greater than 15") else: print("age is less than 15")
Output:
age is less than 15
Explanation:
Here age is less than 15 so if condition is false and else statement is executed
4)If statement with multiple conditions
In all of the preceding instances, we provide a single condition with the if-statement, but we can also provide multiple conditions.
age = 13 # checking if the age is between 10 to 20 if (age > 10 and age < 20): print("ageis between 10 to 20") else: print("age is less than 15")
Output:
age is between 10 to 20
Explanation:
The if-statement in this case checks several conditions, and if all of them satisfy and evaluate to True, the interpreter executes the code in the if-block.
Related Programs:
- python programming compound statement functions
- python programming compound statement built in functions
- python programming compound statement parameter
- python programming simple statement
- python programming compound statement
- python open a file using open with statement and benefits explained with examples
- python if elif else statement