Computer Notes
Computer Notes
In Python, we use if , elif (short for “else if”), and else keywords to
create these decision-making rules.
1. The if Statement
if condition:
# code to run if condition is True
Example:
age = 12
if age >= 10:
print("You can join the science club!")
https://stackedit.io/app# 06/02/25, 8 50 PM
Page 1 of 5
:
the message prints.
if condition:
# code for True
else:
# code for False
Example:
weather = "sunny"
if weather == "rainy":
print("Take an umbrella!")
else:
print("Wear sunglasses! ")
Checks mul!ple condi!ons in order. Python will test each elif un!l it
finds one that’s True.
Syntax:
if condition1:
# code 1
elif condition2:
https://stackedit.io/app# 06/02/25, 8 50 PM
Page 2 of 5
:
# code 2
else:
# code for all other cases
marks = 75
if marks >= 90:
print("Grade A")
elif marks >= 70:
print("Grade B") # This runs!
elif marks >= 50:
print("Grade C")
else:
print("Try harder!")
Output: Grade B
You can use comparison operators ( > , < , == , != , >= , <= ) and logical
operators ( and , or , not ) to build condi!ons.
num = 15
if num > 10 and num < 20:
print("Number is between 10 and 20!")
fruit = "apple"
https://stackedit.io/app# 06/02/25, 8 50 PM
Page 3 of 5
:
if fruit == "apple" or fruit == "banana":
print("This is a common fruit!")
if x > 0:
print("Positive") # Not indented!
Correct:
if x > 0:
print("Positive")
score = 1200
if score >= 1500:
https://stackedit.io/app# 06/02/25, 8 50 PM
Page 4 of 5
:
print("Level 3 Unlocked! ")
elif score >= 1000:
print("Level 2 Unlocked! ") # This runs!
else:
print("Keep playing! ")
Key Takeaways
Prac!ce Time!
https://stackedit.io/app# 06/02/25, 8 50 PM
Page 5 of 5
: