Video
Use this if you want an external lecture version of the same material.
Lesson assets
Keep the notes in-page. Use this column for your own PDFs first: recitations, labs, homework, or an optional downloadable handout.
video link
CS50P Lecture 1: Conditionals
lecture notes
Logic notes PDF
Example downloadable handout slot for the formal notes behind the lesson.
recitation
Recitation 2 PDF
Use this spot for a shorter guided sheet that students work through after lecture.
hw
Homework 2 PDF
Sample homework slot using one of your existing local PDFs.
From statements to branches
In mathematics, a statement is something that is either true or false.
Examples:
- is true.
- is even is true.
- is not always true; it depends on the value of .
In programming, these become Boolean expressions.
3 < 5
x % 2 == 0
score >= 50
Each of these evaluates to either True or False.
That is exactly why conditionals matter: a program can choose a branch based on the truth value of a predicate.
The shape of an if
age = int(input("Age: "))
if age >= 18:
print("adult")
else:
print("minor")
This is not philosophically complicated. It is just a decision tree written in a strict syntax.
The mathematical version is the piecewise rule
Python lets us execute that rule.
Combining predicates
You will constantly use and, or, and not.
For example:
if score >= 50 and attendance >= 0.8:
print("pass")
This means both conditions must be satisfied.
Mathematically, if
then the branch runs exactly when is true.
Example: sign of a number
def sign(x: float) -> str:
if x > 0:
return "positive"
elif x < 0:
return "negative"
else:
return "zero"
This looks trivial, but it is already a model of disciplined casework.
Whenever a mathematical problem splits into disjoint cases, code often mirrors that structure directly.
Guardrails for beginners
A few mistakes show up constantly:
- Writing
=when you meant==. - Forgetting indentation.
- Writing overlapping conditions in the wrong order.
For example:
score = 95
if score >= 50:
print("pass")
elif score >= 90:
print("distinction")
This never prints "distinction" because the first branch already captures every score above 90.
The fix is to order conditions from most restrictive to least restrictive.
Short exercise set
Write functions for the following:
is_even(n)returningTrueiff is even.max_of_two(a, b)returning the larger input.grade(mark)returning"A","B","C", or"F"using sensible cutoffs.
Then explain the logic of each one in plain English before you run it.
That last step matters. If you cannot explain the branch structure, you usually do not understand it yet.
Good code branches only work when the reasoning behind them is explicit.