Python If-else
In Python programming, the if statement is used to test the condition.
Before getting started with conditional statements, let’s first understand how a block of code is defined in python.
Indentation:
Indentation indicates a block of code that belongs to a specific structure like a function, loop, or conditional statement. You must use the same number of spaces (or tab) for each block of code. The standard practice is to use Tab for each level of indentation. Every time you start a new block (like inside a function, loop, or if statement), the code in that block must be indented.
For e.g.
In the above example the green tab space is representation of indentation: anything below if statement is written with indentation means it is inside of if block and when a statement is written without indentation that means the block ends there.
For e.g.
age = 20
if age >= 18: #checking condition if age is greater than equal to 18
print(“You can create your Bank Account”)
#output: You can create your Bank Account
age = 11
if age >= 18: #checking condition if age is greater than equal to 18
print(“You can create your Bank Account”)
else: #if condition is false this block will run
print(“You can’t create your Bank Account, your age is less than 18”)
#Output: You can’t create your Bank Account, your age is less than
# 18
# Example of if-elif-else statement
age = 30
if age >= 60: #checking first condition
print(“You are a Senior Citizen.”)
elif age>= 18: #checking second condition if first is false
print(“You can create your Bank Account.”)
else: #executes this if none of the above condition is true
print(“You can’t create your Bank Account. Your age is less than 18.”)
#output: You can create your Bank Account
Task:
1. Do you want Health Insurance ?
a.If no, then print Thank you
Output:
b.if yes then continue below 2nd question
Output:S
c.if input is not equals to yes or no then show error.
Output:
2. a.If age is less then 21 then insurance cost is 1500
Output:
b.If age is between 21 and 50 then insurance cost is 2000
Output:
c. If age is more then 50 then insurance cost is 3000
Output:
3. In which Standard you are ?
a. if you are from 5th standard then you have to go at 1st floor for exam.
Output:
b. if you are from 6th standard then you have to go at 2nd floor
Output:
c. if you are from 7th standard then you have to go at 3rd floor.
Output:
d. other wise goto library for exam.
Output:
4.What’s your budget for car purchasing ?
a.if less then 50 lakhs you can take fortuner.
Output:
b.if 50 lakhs you can take Mercedes
Output:
c.if greater then 50 lakhs you can take BMW M4.
Output:
d.if 1Crore or greater then 1 Crore you can take ferrari.
Output:
5. Class and If-Else:
Create a class named Person with properties Name, Age, and Gender. Create an object of Person and set its properties. Use an if-else statement to print “Adult” if the Age is 18 or older, otherwise print “Minor”.
6. Constructor and If-Else:
Create a class named Student with __init __method that initializes Name and Grade.
create an object of Student. Use an if-else statement to print “Pass” if the Grade is 50 or above, otherwise print “Fail”.
7. Static Members and If-Else:
Create a class named Utility with a static method IsEven(int number) that returns true if the number is even.
In the main method, use the IsEven method to check if a number (e.g., 10) is even. Print “Even” if true, otherwise print “Odd”.
8. DateTime and If-Else:
Get the current date using DateTime
Use an if-else statement to print “Weekend” if today is Saturday or Sunday, otherwise print “Weekday”.
9. Class and Constructor with If-Else:
Create a class named Book with properties Title and Price, and __init__ method to initialize them. Create an object of Book. Use an if-else statement to print “Expensive” if the Price is above 500, otherwise print “Affordable”.