If - Else Statement in Nagul's Blog
NAGUL'S BLOG
Python Assignment.no - 1
Understanding If–Else Statements in Python
If -Else Statement
Introduction:
-
If we are hungry, we eat food.
-
If the traffic light is red, we stop.
-
If marks are above 40, we pass; else, we fail.
In the same way, a computer program also needs to make decisions based on different situations. This is where if–else statements in Python become important.
An if–else statement allows a program to check a condition and choose what to do next.
It works just like our thinking:
-
If the condition is true → do one action
-
Else (if the condition is false) → do a different action
This makes the program smarter because it can react to inputs, data, and different cases instead of doing the same thing every time.
If–else statements are one of the most basic and powerful tools in Python programming, and they are used in almost every type of application — from calculators and games to websites and mobile apps.
Uses of If–Else Statements in Python
If–else statements are very important in Python because they help the program make decisions. They are used in many situations, both in coding and in the real world.
1. Controlling Program Decisions
Python uses if–else to choose which block of code should run.
This makes the program flexible instead of repeating the same steps every time.
2. Checking User Input
Programs can check if the user entered the correct password, age, OTP, or any value.
This helps in making applications secure and error-free.
3. Handling Different Conditions in Apps
Games, calculators, and mobile apps use if–else to react based on situations.
Example: If your score is high → move to next level; else → try again.
4. Data Validation
If–else helps check whether data is correct or valid.
Example: If marks are above 40 → pass; else → fail.
5. Real-World Decision Making
Many systems use simple if–else logic behind the scenes:
*If traffic is heavy → show alternate route (Google Maps)
*If a product is in stock → allow purchase
*If battery is low → show warning
6. Automation and Smart Devices
Robots and smart devices use if–else rules to react:
“If temperature is high → turn on cooling.”
This makes devices work automatically without human control.
Now let us see If-else with example
Examples:
Code.1:
Explanation:
The program checks if age is 18 or more
*If yes → it prints “You are an adult”.
*If not → it prints “You are a minor”.
Code.2:
Explanation:
The program asks the user to enter their marks.
It then checks the marks step by step:
*If marks ≥ 90 → Grade A
*Else if marks ≥ 75 → Grade B
*Else if marks ≥ 50 → Grade C
*Otherwise → Grade F
Code.3 : { This is complex than other two examples }
balance = 5000 # current bank balance
amount = int(input("Enter amount to withdraw: "))
if amount <= 0:
print("Invalid amount! Please enter a positive value.")
elif amount > balance:
print("Insufficient balance!")
elif amount % 100 != 0:
print("Amount must be in multiples of 100.")
else:
balance -= amount
print(f"Withdrawal successful! Remaining balance: {balance}")
Explanation:
This program checks many conditions :
1. amount <= 0
If the user enters zero or a negative number → not valid.
2. amount > balance
If the user wants more money than available → not allowed.
3. amount % 100 != 0
ATMs usually give money in 100s, so it checks if the amount is divisible by 100.
4. else block
If all conditions are correct → complete the withdrawal.
Conclusion:
![]() |



Comments
Post a Comment