Conditional Statements:
Conditional statements are expressions that evaluate to either True
or False
. They are generally referred as if-elif-else
statements in python. Sometimes you may want a part of your program to be executed only if it meets a certain criteria. Or a computer program may have to make a choice of it's own and execute a part of the code depending on the input provided by user. Such scenariors can be easily dealt with by using conditional statements.
Before proceeding further note that the following objects are treated as False
in python.
- Boolean value
False
- The Value
None
- Empty Objects (Strings, Tuples, Lists, Dictionaries, etc..)
- Numerical zero
All other values are considered to be True
.
The if Statement:
The syntax of the if
statements is as follows:
if condition: statement1 statement2 # ... more statements
Here the program checks the condition fisrt. If it is evaluated to True
the statements will be executed else they will be skipped. In python, a block of the code is identified by the indentation. The statements having having the same amount of blanks or tabs as indentation will be treated as one block. Hence in the below example only the statements 5 and 6 treated as the body of the if
but not statement7.
if condition: statement5 statement6 statement7
The following example demonstrates the usage of if
condition:
number = int(input("Enter a number:")) if number % 2 == 0: print("even") print("finished")
In the above example the number provided by the user will be converted to integer and stored in the variable number
. Then the condition is evaluated. If the number is divisible by 2 the string even
will be printed on the console else not. The string finished
is always printed on the console irrespective of the condition as it not indented as part of the if
block.
>>> Enter a number:47 finished >>> Enter a number:84 even finished
When user entered 47 only the string finished
is printed because the condition is evaluated to False
and the if
block is skipped.
The if..else statement:
The if
block can have an optional else
block without a condition. The else block will be executed only when the if condition is evaluated to false. Again the body of the else block will be identified by using indentation.
The syntax of the if..else
condition will be as follows:
if condition: statement1 statement2 # ... more statements else: statement3 statement4 # ... more statements
The following example demonstrates the usage of if..else
condition:
number = int(input("Enter a number:")) if number % 2 == 0: print("even") else: print("odd") print("finished")
Again the condition is evaluated first and if the number is divisible by 2 the string even
will be printed on the console. If it is not divisible by 2 the sting odd
will be printed. And the string finished
will always be printed on the console irrespective of the condition as it is not indented as part of the if
or else
block.
>>> Enter a number:47 odd finished >>> Enter a number:84 even finished
When user entered 47 the string odd
will be printed as the condition is evaluated to False
and the else
block is executed. When 84 is entered, the string even
will be printed as the condition is evaluated to True
and the if
block is executed.
The if..elif..else statement:
This statement can be used to check for multiple conditions. The syntax will be as follows:
if condition: statement1 statement2 # ... more statements elif condition: statement1 statement2 # ... more statements else: statement3 statement4 # ... more statements
The elif stands for else if. Below example demonstrates the usage of if..elif..else
condition:
marks = int(input("Enter your marks:")) if marks >= 90: print("Your grade is A+.") elif marks >= 80 and marks < 90: print("Your grade is A.") elif marks >= 70 or marks < 80: print("Your grade is B.") else: print("Your grade is C.") print("finished")
"Intellignece cannot be defined by exams."
The if block can have only one else block where as there can multiple elif blocks. If you are familiar with C programming you might hvae heard about switch statement. There is no switch statements in python. If you are wondering why, you may want to read the zen of python specified at the end of introduction chapter.
The nested statements:
We can have a conditional statement inside another conditional statement. This is called nesting in computer programming. The level of nesting is be identified by using indentation. The following pseudocode shows a simple nested statement:
if condition: statement(s) if condition: statement(s) elif condition: statement(s) else: statement(s) elif condition: statement(s) else: statement(s)
The following example explains the usage of a simple nested statement:
number = int(input("Enter a number:")) if number > 0: print(""Checking if the number is even or odd.") if number % 2 == 0: print("You entered a positive even number.") else: print("You entered a positive odd number.") elif number < 0: print("The number is negative.") else: print("You entered a Zero.")