In this tutorial, we are going to learn about the python if..else..elif statement with the help of examples and am using pycharm to execute the command and create some decision-making programs.
The if statement in Python allows you to specify a block of code to be executed if a certain condition is satisfied. Here we use the statement and give value to the output return when a certain condition is met and extract the block code.
Read also : student registration form a python with database
For example:
In school, the student gets results based on the marks. if the student marks get above 35 marks, the output is a pass and if the student gets below 35 marks, the output is a fail.
same process as the Assigning in grades systems like A,B,C,D,E based on the marks obtained by a student.
In Python , there are three types of forms in if...else statement
- if statement
- if.. else statement
- if ...elif...else statement
Python if statement
student = 45
# Here the check if number is greater than 35
if student > 35:
print("student is pass ")
In the above example python code, we have created student marks variable named as student. the variable is student = 45 ,if the condition is true inside the if statement is executed.In case the condition is false , the if statement is skipped.
The if statement in Python allows you to specify a block of code to be executed if a certain condition is satisfied.
Syntax:
if condition:
# block of statement
For Example:
student = 45
if student > 35:
print("student is pass ")
Python If..Else statement:
Syntax:
if condition:
# block of code is True if condition executed
else:
# block of code is false if condition executed
For Example:
student = 35
if student >= 35:
print("student is pass")
else:
print('student is fail')
print('Above given statement is passed')
Python If ...Elif...Else statement:
Syntax:
if condition1:
# block 1
elif condition2:
# block 2
else:
# block 3
For Example:
student = 0
if student >= 35:
print("student is pass")
elif student == 0:
print('student is absent')
else:
print('student is fail')
print('Above given statement is passed')
student = 65
student = 12