Intro to Computer Science

Python Cheat Sheet

If Statements

Run code only when something is true.

Comparing two values

One = stores a value. Two == asks a question. Mixing them up is the most common bug there is.

SymbolWhat it asksExample
==Is it equal to?name == "Becca"
!=Is it not equal to?name != "Becca"
<Is it less than?score < 60
>Is it greater than?score > 90
<=Is it less than or equal to?age <= 12
>=Is it greater than or equal to?age >= 18

Comparing text

Text comparisons are exact. Capital letters matter.

name = "Becca"
if name == "Becca":
    print("Hey, it is Becca.")
else:
    print("Not Becca.")
Output
Hey, it is Becca.

Comparing text without caring about capitals

Use this one

.lower() both sides and "BECCA", "becca", and "Becca" all match. Handy after input(), since you never know how they typed it.

name = input("Enter a name: ")
if name.lower() == "becca":
    print("Hey, it is Becca.")
else:
    print("Not Becca.")
Sample run
Enter a name: BECCA
Hey, it is Becca.

if, else, elif

if on its own

Runs only when the condition is true. Otherwise nothing happens.

age = 27
if age >= 18:
    print("You can vote.")
Output
You can vote.

if with else

else runs whenever the if is false.

age = 15
if age >= 18:
    print("You can vote.")
else:
    print("Not yet.")
Output
Not yet.

if, elif, else

Python checks top to bottom and stops at the first true one.

score = 84
if score >= 90:
    print("A")
elif score >= 80:
    print("B")
elif score >= 70:
    print("C")
else:
    print("Keep working.")
Output
B

elif or separate ifs?

Same numbers, different answers. Neither one is wrong. Pick the one that does what you want.

Separate ifs run every match

Each if is its own question, so two of them can be true.

age = 27
if age < 20:
    print("Under 20.")
if age < 30:
    print("Under 30.")
if age < 100:
    print("Under 100.")
Output
Under 30.
Under 100.

elif stops at the first match

Once one is true, Python skips the rest.

age = 27
if age < 20:
    print("Under 20.")
elif age < 30:
    print("Under 30.")
elif age < 100:
    print("Under 100.")
Output
Under 30.

Checking two things at once

and: both must be true

Both parts are true, so the login works.

username = "Ashlee"
password = "0321"
if username == "Ashlee" and password == "0321":
    print("Welcome, Ashlee.")
else:
    print("Wrong username or password.")
Output
Welcome, Ashlee.

and: one wrong is enough to fail

Same code, wrong password. One bad part fails the whole thing.

username = "Ashlee"
password = "0322"
if username == "Ashlee" and password == "0321":
    print("Welcome, Ashlee.")
else:
    print("Wrong username or password.")
Output
Wrong username or password.

or: just one must be true

It is Saturday, so Python never even checks Sunday.

day = "Saturday"
if day == "Saturday" or day == "Sunday":
    print("It is the weekend.")
else:
    print("It is a school day.")
Output
It is the weekend.

not: flips true and false

Reads as: if it is not raining.

is_raining = False
if not is_raining:
    print("Go outside.")
Output
Go outside.

An if inside an if

Nested if

Lets you tell the two failures apart instead of giving one vague message.

username = "Ashlee"
password = "0322"
if username == "Ashlee":
    if password == "0321":
        print("Welcome, Ashlee.")
    else:
        print("Right username. Wrong password.")
else:
    print("That is not Ashlee.")
Output
Right username. Wrong password.