MY TECHWORLD JOURNEY AT BLOCKFUSELABS WEEK9
This week, we focused on one of the most important parts of programming, CONDITIONALS.
Conditionals are special aspect of programming that help you make decisions. They allow your code to check if something is true and then choose what to do next. This is how we make our programs behave differently in different situations.
What We Learned This Week
1. What Are Conditionals?
Conditionals are used to check something and then decide what to do.
In Python, we mainly use:
if – to check single condition
if else – to check for two possibilities (another condition if the first one wasn’t true)
if elif else for multiple exclusive possibilities
else – to do something if none of the above conditions were true
also, nested if conditions that depend on previous conditions beign true
The if Statement
example for if sttement
Example:
age = int(input("Enter your age: "))
if age >= 45:
print("You are eligible to be voted.")
in this case, if the age entered equals or greater than 45, the message will b printed.
The elif Statement
What if you have more than one condition to check? That’s where elif comes in.
Python will go down the list and stop at the first condition that is true.
Example:
score = 30
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")
in this case, since 30 is less than 70, it prints "Grade: F".
The else Statement
The else part runs only if none of the if or elif conditions were true.
Example:
temperature = 10
if temperature > 25:
print("It's hot outside.")
else:
print("It's cold outside.")
Since 10 is not more than 25, it prints “It’s cold outside.”
Using Conditionals Inside Other Conditionals (Nested if statement)
Sometimes, we need to check one condition inside another. This is called nesting.
Example:
score = int(input("Enter your score: "))
if score >= 90:
if score == 100:
print("Perfect score!")
else:
print("Grade: A")
elif score >= 70:
if score >= 85:
print("Grade: B+")
else:
print("Grade: B")
elif score >= 50:
print("Grade: C")
else:
print("Fail ")
This checks for the score entered to be sure is greater than 90, then also check if its == 100 then it prints out perfect score and prints just grade "A" for normal above 90.
Combining Conditions
Python lets you check more than one condition at a time using these words:
and – both conditions must be true
for "and"
T + T = T
T + F = F
F + T = F
F + F = F
or – at least one condition must be true
in "or"
T + T = T
T + F = T
F + T = T
F + F = F
not – reverses a condition
here we have,
!T = F
!F = T
Example:
age = 25
has_ticket = True
if age >= 15 and has_ticket:
print("You can enter the event.")
Both conditions are true, so the person is allowed in.
Shorter Way: One-Line Conditionals (Ternary)
Sometimes, you can write a condition in just one line. This is great for simple decisions.
Basic rule of thumb
Use if for the first condition.
Use elif for more options.
Use else when none of the other conditions are true.
Try not to make your conditions too complicated.
Practice makes perfect — the more you use conditionals, the easier they become!
in conclusion
Conditionals are a super important part of programming. They let your program make choices and act differently depending on the situation. Whether you're checking someone’s age, a score, or the weather — conditionals are your tool for decision-making in code....