Python Conditional Statements
Python Conditional Statements help you to perform different actions based on some conditions. There are the following ways to handle or use Conditional Statements in Python programming:
- if statement
- if-else statement
- if elif statement
- nested if statement
If statement
This is the easiest way to use a conditional statement. With the help of if
keyword, we define the if statement.
Let’s see a simple example of Python if statement:
Suppose we want to check whether the age of a person is greater than 18.
age = 19
if age > 18: #condition is age is greater than 10
print("person's age is greater than 18")
person’s age is greater than 18
Here we have used if statement to check our condition. If our condition is matched, then python will read the print statement and will give us the output.
Note: If the condition is not matched then there will be no output or blank output. Let’s see an example:
age = 17
if age > 18: #condition is age is greater than 10
print("person's age is greater than 18")
Here we will get no Output because the condition is not satisfied.
If else statement
This way is more helpful than if statement. With the help of if
and else
keyword, we define the if else statement.
Example of if else statement
Let’s see a simple example of Python if else statement:
Suppose we want to check whether the age of a person is greater than 18 or not.
age = 19
if age > 18: # condition is age is greater than 10
print("person's age is greater than 18")
else:
print("person's age is less than 18")
Here we have used if statement to check our condition. If our condition is true, then python will read the print statement and will give us the output.
person’s age is greater than 18
Note: If the condition of if
the statement is satisfied, then code inside the body of the if statement will be executed. However, if given if the condition is not satisfied then the inside the else
statement will execute.
if elif statements
If we have multiple conditional statements then we can handle those statements by the help of the if elif statements.
Here is a simple example to check whether a student got first division, second division, or third division, or has failed.
marks=float(input("enter the marks = "))
if marks >= 80 and marks <= 100 :
print("Merit ")
elif marks >= 60 and marks < 80 :
print("First Division")
elif marks >= 45 and marks < 60 :
print("Second Division")
elif marks >= 33 and marks < 45 :
print("Third Division")
else:
print("has failed")
enter the marks = 81
Merit
nested if statements
If a conditional statement dependent on other conditional statements then we use nested if statements. We must take care of proper indentation in the nested if statement.
Example of Python nested if statement
Here is a simple example using nested if statement to check can a student get the scholarship.
marks = float(input("enter the marks = "))
passing_year = float(input("enter the year in which you got passed"))
if marks >= 90:
if passing_year <= 2018 and passing_year >= 2016:
print("you can get the scholarship")
else:
print("Your passing year doesn't match our requirements")
else:
print("your marks are not enough")
enter the marks = 98
enter the year in which you got passed 2019
you can get the scholarship
Exercises & Assignments |
---|
No Content Found. |
Interview Questions & Answers |
---|
No Content Found. |