Python is powerful but strict. Watch your indentation and colons!
Python Thirds Reference Sheet
Input & Output
BasicsConcatenation (Text + Text)
# Need space inside quotes!
Text + Numbers (Use str)
# Wrap number in str()
print("Score: " + str(score))
Maths Magic
Python follows BODMAS rules.
| Sym | Action | Example |
|---|---|---|
| + | Add | 5 + 2 |
| - | Subtract | 5 - 2 |
| * | Multiply | 5 * 2 |
| / | Divide | 10 / 2 |
| ** | Power | 3 ** 2 |
input() is text. Wrap it to do maths:age = int(input("Age? "))
Decisions
Computers can choose paths.
if score >= 90:
print("Gold")
elif score >= 50:
print("Pass")
else:
print("Fail")
Loops
For Loop (Counting)
Repeat set number of times.
for i in range(5):
print(i)
While Loop (Condition)
Repeat while true.
while run:
print("Go")
Python Debugging Checklist
Code red? Error message? Check these common traps.
-
The Golden Colon :
if,else,for,whileMUST end with:. -
Indentation Error
Use Tab to indent code inside blocks.
-
Maths with Text?
input()is text. Wrap it:int(input()). -
Case Sensitive
Print❌ vsprint✅.name!=Name. -
Equals: = vs ==
=sets value.==checks value. -
Missing Brackets ()
print "hi"❌print("hi")✅ -
Matching Quotes
Start with
"? End with". -
Infinite Loop!
Ensure
whileloops eventually stop. -
Consistent Names
Did you spell it the same way?
scorevsscroe.
Data Types
Python needs to know what kind of data it is holding.
Characters inside quotes.
x = "Hello"
No decimal point.
x = 5
Number with a dot.
x = 3.14
True or False.
x = True
Lists (Arrays)
Store multiple items in one variable.
print(x)
1. Grade Calc
Logic: Input -> Int -> If/Elif/Else
if score >= 80:
print("A")
elif score >= 50:
print("C")
else:
print("Fail")
2. Password
Logic: While Loop (Trap)
while pw != "123":
pw = input("Pin? ")
if pw != "123":
print("No!")
print("Open")
3. Repeater
Logic: For Loop (Range)
n = int(input("Times? "))
for i in range(n):
print(word)
4. Tip Calc
Logic: Float Maths & Formatting
tip = bill * 0.10
total = bill + tip
print("Pay: " + str(total))
5. Days Alive
Logic: Multiplication
days = age * 365
print("Days: " + str(days))
6. Quiz Bot
Logic: String Checking (==)
if ans == "London":
print("Correct!")
else:
print("Wrong!")
SyntaxError
"I don't understand!"
You broke the grammar rules.
- Missing bracket
()? - Missing quote
""? - Missing colon
:?
IndentationError
"Messy spacing!"
Code is not lined up.
- Check lines after a
: - Press Tab to fix.
- Align everything perfectly.
NameError
"Who is that?"
Using a variable that doesn't exist.
- Check spelling (
scorvsscore). - Did you define it first?
- Check Capital Letters.
TypeError
"Wrong Data Type!"
Maths with words? Numbers with text?
- Can't add Number to String.
- Use
str()to print numbers. - Use
int()for maths input.
ZeroDivisionError
"The Black Hole"
Dividing by 0 is impossible.
- Check your variables.
- Is the divider 0?
- Change the number.
ValueError
"I can't convert that!"
Trying to turn a word into a number.
int("cat")crashes.- User must type a number.
- Check input matches code.
Tip: Python counts from 0, not 1!