Table of Contents
Define If Statements or Conditional Statements in Python?
For example, a student can get passing marks if he can score the given limit of marks in the examination.
These kinds of decisions are usually checked using ” if..elif..else ” statements in python.
What are the types of conditional statements in Python?
1. if statement
2. if … elif
3. if … else
4. Nested if statements
Indentation is necessary for Python.
Indentation (whitespace at the beginning of a line) is used by Python to define the scope in the code. Curly brackets are often used in other programming languages for this purpose.
How to write an if statement in Python?
block of code... ( execute this code if the condition is true)
The condition can be true or false. The block of code inside of if statements will execute only if the given condition is true.
### # Example for if statement (conditional statements in python) marks = 100 if marks > 40: print("passing marks...") #output: passing marks... # indentation example (this code will result in an error) marks = 100 if marks > 40: print("passing marks...") # because the print('...') method is not in the scope of if statement # output: IndentationError: expected an indented block ###
The elif keyword in Python means “try this condition if the previous conditions were not valid.”
For example, in the above example if marks are below 40 then the code does nothing. We should print a failure message in this case.
We modified the above example by using elif keyword.
### # Example for if...elif statement (conditional statements in python) marks = 20 if marks > 40: print("passing marks...") elif marks < 40: print("sorry, minimum passing marks are 40 ...") #output: sorry, minimum passing marks are 40 ... ###
You can use as many elif statements as you want inside if statements. for example, we will check the marks.
### #example elif statement (conditional statements in python) marks = 100 if marks == 10: print("your marks are 10") elif marks == 20: print("your marks ar 20") elif marks == 30: print("your marks are 30") elif marks == 40: print("your marks are 40") elif marks == 100: print("your marks are 100") else: print("marks not defined!") #output: your marks are 100 ###
The else keyword is used to output a default behavior. This section only executes when all the previous conditions are false.
Before the else statement, you can add one or more conditions. If none of the conditions are met, then else statement executes by default.
You can also check the above example (if…elif statement) for
if...else statement. There the else part (‘ marks not defined! ‘) will be printed if all the above conditions are false.
Short Hand If statement in Python
You can place it on the same line if you only have one statement to run. The given example explains the shorthand if statement.
### example conditional statements in python marks = 100 if marks > 40: print("passing marks...") #output: passing marks... ###
Short Hand If … Else
If there is only one statement to execute, one for if and one for else, then you can place everything on the same line like:
### example (conditional statements in python) num1 = 1 num2 = 2 print(" Num 1 is greater ") if num1 > num2 else print(" Num 2 is greater ") #output: Num 2 is greater ###
Nested If statements
You can use if statements inside an if statement. This is known as nested if in Python.
It will further filter the result and its behavior.
### example (conditional statements in python) num = 40 if num > 10: print("Num greater than 10") if num > 20: print("Num greater than 20") else: print("Num is not greater than 20") #output: Num greater than 10 Num greater than 20 ###
The pass Statement
It is not allowed to write empty if statements in python. If you have an empty if statement, simply add the pass statement to prevent an error.
### Example (conditional statements in python) if marks > 90: pass ###
The code in the above example returns or outputs nothing. It also won’t send you an error.
Video Explains conditional statements in python
Read more about Python Lists in python.