CSC 183 · Introduction to Problem Solving
Our notes
1 min readConditions
A condition is an expression that is True or False.
Comparison operators: == equal, != not equal, < less than, > greater than, <= at most, >= at least.
Logical operators: and (both true), or (at least one true), not (reverses).
if, elif, else
score = int(input("Score: "))
if score >= 40:
print("Pass")
else:
print("Fail")
The colon and the indentation (four spaces) mark the block that belongs to each branch.
Worked example: largest of three
a = int(input("a: "))
b = int(input("b: "))
c = int(input("c: "))
if a >= b and a >= c:
largest = a
elif b >= c:
largest = b
else:
largest = c
print("Largest:", largest)
Trace: 12, 30, 7 → the first test fails (12 ≥ 30 is False); 30 ≥ 7 is True, so largest = 30. Inputs 5, 5, 2 → the first test is True: 5 (ties handled).
Worked example: leap year
A year is a leap year if it is divisible by 4 but not by 100, unless it is also divisible by 400.
year = int(input("Year: "))
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
2024 → leap; 1900 → not leap (divisible by 100 but not 400); 2000 → leap.
Truth table for and / or
| A | B | A and B | A or B |
|---|---|---|---|
| True | True | True | True |
| True | False | False | True |
| False | True | False | True |
| False | False | False | False |
Testing selection
Test every branch and every boundary: for "score ≥ 40" try 39, 40 and 41, and unusual inputs such as 0, 100 and a negative number.
Exams usually ask
- Write a program with if/elif/else for grading, tax bands or the largest number.
- Evaluate a compound condition for given values.
Common mistake
Writing = (assignment) where == (comparison) is meant, or getting the order of elif tests wrong (testing score >= 40 before score >= 70 gives every pass the same grade).
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
Read
-
Best pick Introduction to Python Programming, 4.2 If-else statements
(opens openstax.org in a new tab)
OpenStax, Introduction to Python Programming, 4.2 If-else statements, CC BY-NC-SA 4.0 · licence
-
Introduction to Python Programming, 4.5 Chained decisions
(opens openstax.org in a new tab)
OpenStax, Introduction to Python Programming, 4.5 Chained decisions, CC BY-NC-SA 4.0 · licence
-
Conditional (computer programming)
(opens en.wikipedia.org in a new tab)
Wikipedia, Conditional (computer programming), CC BY-SA 4.0 · licence
-
Boolean expression
(opens en.wikipedia.org in a new tab)
Wikipedia, Boolean expression, CC BY-SA 4.0 · licence
-
OpenStax, Introduction to Python Programming (whole free textbook)
The whole book, free to read online or download as a PDF (the PDF is large: Wi-Fi).
(opens openstax.org in a new tab)
OpenStax, OpenStax, Introduction to Python Programming (whole free textbook), CC BY-NC-SA 4.0 · licence
-
NPTEL: The Joy of Computing using Python (IIT Ropar, whole course)
Short lessons for beginners: first programs, input, if conditions, loops and lists, through small games and puzzles. Video lectures, many hours in all: watch on Wi-Fi where you can.
(opens nptel.ac.in in a new tab)
NPTEL, NPTEL: The Joy of Computing using Python (IIT Ropar, whole course), CC BY-NC-SA 4.0 · licence
Watch
-
if-elif-else | Intro to CS - Python | Khan Academy
5 min · ≈ 23 MB · about ₦13
(opens youtube.com in a new tab)
-
If statements in Python are easy (if, elif, else) 🤔
8 min · ≈ 36 MB · about ₦20
(opens youtube.com in a new tab)
-
Control Flow in Python - If Elif Else Statements
16 min · ≈ 72 MB · about ₦40
(opens youtube.com in a new tab)
-
The if-elif-else Statement in Python
13 min · ≈ 59 MB · about ₦33
(opens youtube.com in a new tab)
We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.