Input & Output

Basics
🗣️ PRINT
Computer speaks
👂 INPUT
Computer listens
name = input("Name? ")

Concatenation (Text + Text)

print("Hi " + name)
# Need space inside quotes!

Text + Numbers (Use str)

score = 100
# 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
Maths Tip: input() is text. Wrap it to do maths:
age = int(input("Age? "))

Decisions

Computers can choose paths.

score = 85

if score >= 90:
  print("Gold")
elif score >= 50:
  print("Pass")
else:
  print("Fail")
a == b
a != b
a > b
a < b
a >= b
a <= b
⚠ Don't forget the colon (:)!

Loops

For Loop (Counting)

Repeat set number of times.

# 0, 1, 2, 3, 4
for i in range(5):
  print(i)

While Loop (Condition)

Repeat while true.

run = True
while run:
  print("Go")

Python Debugging Checklist

Code red? Error message? Check these common traps.

  • The Golden Colon :

    if, else, for, while MUST end with :.

  • Indentation Error

    Use Tab to indent code inside blocks.

  • Maths with Text?

    input() is text. Wrap it: int(input()).

  • Case Sensitive

    Print ❌ vs print ✅. name != Name.

  • Equals: = vs ==

    = sets value. == checks value.

  • Missing Brackets ()

    print "hi"
    print("hi")

  • Matching Quotes

    Start with "? End with ".

  • Infinite Loop!

    Ensure while loops eventually stop.

  • Consistent Names

    Did you spell it the same way? score vs scroe.

Data Types

Python needs to know what kind of data it is holding.

String (str) Text

Characters inside quotes.

x = "Hello"
Integer (int) Whole Number

No decimal point.

x = 5
Float (float) Decimal

Number with a dot.

x = 3.14
Boolean (bool) Logic

True or False.

x = True

Lists (Arrays)

Store multiple items in one variable.

Create
items = ["Apple", "Banana"]
Access
print(items[0]) # Apple
Append
items.append("Cherry")
Traverse
for x in items:
  print(x)

1. Grade Calc

Logic: Input -> Int -> If/Elif/Else

score = int(input("Score? "))
if score >= 80:
  print("A")
elif score >= 50:
  print("C")
else:
  print("Fail")

2. Password

Logic: While Loop (Trap)

pw = ""
while pw != "123":
  pw = input("Pin? ")
  if pw != "123":
    print("No!")
print("Open")

3. Repeater

Logic: For Loop (Range)

word = input("Word? ")
n = int(input("Times? "))

for i in range(n):
  print(word)

4. Tip Calc

Logic: Float Maths & Formatting

bill = float(input("Bill? "))
tip = bill * 0.10
total = bill + tip

print("Pay: " + str(total))

5. Days Alive

Logic: Multiplication

age = int(input("Age? "))
days = age * 365

print("Days: " + str(days))

6. Quiz Bot

Logic: String Checking (==)

ans = input("Capital? ")
if ans == "London":
  print("Correct!")
else:
  print("Wrong!")

SyntaxError

"I don't understand!"

You broke the grammar rules.

Fix:
  • Missing bracket ()?
  • Missing quote ""?
  • Missing colon :?

IndentationError

"Messy spacing!"

Code is not lined up.

Fix:
  • Check lines after a :
  • Press Tab to fix.
  • Align everything perfectly.

NameError

"Who is that?"

Using a variable that doesn't exist.

Fix:
  • Check spelling (scor vs score).
  • Did you define it first?
  • Check Capital Letters.

TypeError

"Wrong Data Type!"

Maths with words? Numbers with text?

Fix:
  • 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.

Fix:
  • Check your variables.
  • Is the divider 0?
  • Change the number.

ValueError

"I can't convert that!"

Trying to turn a word into a number.

Fix:
  • int("cat") crashes.
  • User must type a number.
  • Check input matches code.