Python If..Else

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.

Explanation: 
In the above example, print(2) and print(“Hello World”) is inside if block 
while print(“Hello Earth”) is outside the block
 
There are various types of if statements in Python as follows:
 
1. if statement:
The if statement checks a condition, and if it evaluates to True, the block of code inside the if statement will execute.
 
Syntax:
if condition :
# code to run if condition is met
 
Example:
Write a program that checks if a person is eligible to create a bank account based on their age. If the age is greater than or equal to 18, it should print “You can create your BankAccount” on the console.
 

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

2. if-else Statement
The if-else statement checks a condition, and if it evaluates to True, the block of code inside the if statement executes. If the condition is False, the block inside the else statement executes.
 
Syntax: 
if condition :
# code to run if condition is met
else:
#code to run if condition is not met
Example:
Write a program that checks if a person is eligible to create a bank account based on their age. The program should print a different message if the person is under 18. If the condition is true, print “You can create your Bank Account”.
If the condition is false, print “You can’t create your Bank Account, your age is less than 18” .

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

3. if-elif-else Statement
The if-elif-else statement is used to check multiple conditions. The first, if condition is checked. If it’s True, the corresponding block executes. If it’s False, the next elif condition is checked, and so on. You can have ‘n’ numbers of elif blocks. If none of the if or elif conditions are True, the else block executes.
Syntax: 
if condition :
# code to run if above condition is met
elif condition :
# code to run if above (elif) condition is met
else:
#code to run if none of the above conditions are met
Example:
Write a program that checks a person’s age and categorizes them as a Senior Citizen, eligible to create a bank account, or too young to create a bank account by using if else ladder
If the age is 60 or older, print “You are a Senior Citizen.”
If the age is 18 or older but less than 60, print “You can create your Bank Account.”
If the age is less than 18, print “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”.

Course Video

YouTube Reference :