Module 1 of 6 · 40 minutes

Getting started with Python

No installation, no jargon. By the end of this module you'll be writing and running Python in your browser.

By · Last updated

Learning objectives

  • Explain why Python dominates AI work
  • Run and edit code in the live code cells
  • Use variables of different types
  • Format text with f-strings and string methods
  • Read and fix simple errors
  • Explain why reading code still matters when AI assistants write it
  • Round and format numbers for display
  • Debug with a routine: read, locate, print, change one thing

1.1 Why Python for AI?

Python is the most widely used language in AI and data science. The Stanford AI Index 2026 (using Lightcast data) reports Python as the most in-demand specialised skill in AI job postings. Reasons:

  • Readable — code looks close to plain English.
  • Libraries — pandas for data, scikit-learn for machine learning, PyTorch for deep learning, and official SDKs from Anthropic, OpenAI, Google and others.
  • Notebooks — run code a cell at a time and see results immediately.
  • Community — almost every AI tutorial and course uses it.

1.2 Run code right here

You don't need to install anything. Every code block in this course is a live code cell: press Run and Python runs inside your browser. Change the code and run it again — experimenting is the fastest way to learn. Reset brings back the original.

  • The first run on a page takes a few seconds while Python loads. After that it's quick.
  • Cells on a page share one memory, like a notebook: a variable created in one cell can be used in the cells below it.
  • Your code stays in your browser. Nothing you type is sent to a server.

For your own projects you'll want a notebook. Google Colab is free and browser-based: sign in with a Google account, click New notebook, type code in a cell and press Shift+Enter. (Alternatives: Jupyter on your computer, or VS Code with the Python extension.)

1.3 Your first lines of code

Python · press Run, then try changing itprint("Hello, AI!")

# A comment: Python ignores anything after #
2 + 3 * 4          # the value of the last line is shown: 14

1.4 Variables and types

A variable is a name for a value. Python works out the type for you.

product = "Bamboo board"     # str  (text)
price = 24.99                # float (decimal number)
stock = 120                  # int   (whole number)
in_stock = stock > 0         # bool  (True / False)

print(type(price))           # <class 'float'>
print(round(price * 1.2, 2)) # 29.99
namevaluetypeproductrefers to"Bamboo board"strpricerefers to24.99floatstockrefers to120intin_stockrefers toTruebool
A variable is a name pointing at a value. Python works out each value's type for you.

1.5 Strings and f-strings

name = "Rey"
course = "Python for AI"
message = f"Welcome, {name}! You're taking {course}."
print(message)

review = "  The board is GREAT but arrived late.  "
print(review.strip().lower())          # "the board is great but arrived late."
print("late" in review.lower())        # True
print(len(review.strip()))             # number of characters

f-strings (note the f before the quote) are how you'll build prompts for AI models later.

1.6 Reading error messages

Errors are normal. Run this cell and read the last line of the message first — it names the error type and the problem.

print(price + " pounds")     # this fails: you can't add a number to text

The fix is to put the number inside an f-string:

print(f"{price} pounds")

1.7 Coding with AI assistants

A lot of today's code is drafted by AI. Chat assistants explain code and write snippets. Coding agents (tools such as Claude Code, GitHub Copilot and Codex) go further: you describe a task in plain English and they write, run and fix code across a whole project.

That makes basic Python more useful, not less. Someone still has to:

  • Say clearly what's wanted — which takes the vocabulary you're learning here: variables, functions, DataFrames.
  • Read and check the result — AI-written code can look right and be wrong.
  • Own it — you are responsible for what the code does with data, money and people's information.
Use AI to learn — wisely

AI assistants are excellent at explaining error messages and code. Paste the error and ask "explain this like I'm new to Python", or ask for a line-by-line explanation of any code an AI wrote before you run it. But type the fix yourself and make sure you understand it — and never paste API keys or private data.

1.8 Numbers that look wrong

Computers store decimal numbers in binary, and most decimals can't be stored exactly. The result surprises everyone the first time:

print(0.1 + 0.2)                 # not quite 0.3
print(24.99 * 1.2)               # not quite 29.988

total = 24.99 * 1.2
print(round(total, 2))           # round for calculations you'll reuse
print(f"£{total:.2f}")           # format for display: always two decimal places
print(f"{1234567.891:,.0f}")     # commas for thousands, no decimals

This isn't a Python bug; every language does it. The habit to build: do the arithmetic at full precision, and round or format only when you show the result. For money in real systems, people often work in whole pence to avoid the problem altogether.

1.9 Debugging: a method, not a talent

Everyone's code breaks. What separates beginners from experienced programmers is mostly a routine for finding out why:

  1. Read the last line of the error for the type and the message.
  2. Find the line it points to — the arrow in the message marks the spot.
  3. Print what you have just before that line. Most bugs are a variable holding something you didn't expect.
  4. Change one thing and run again. If you change three, you won't know which one fixed it.

The cell below has two bugs. Run it, read the error, fix it, and run again; then deal with the second.

Python · two bugs to find (run it, read the error, fix, repeat)item = "Bamboo board"
quantity = "3"                      # this came from a web form, so it is text
unit_price = 24.99

total = quantity * unit_price
print(f"{quantity} x {item} = £{totl:.2f}")

The first error is a TypeError: quantity is the text "3", not the number 3, and text can't be multiplied by a decimal. (The message calls text a "sequence", meaning a sequence of characters. Error messages use Python's own vocabulary, and part of learning to debug is learning to translate it.) Wrap it as int(quantity). The second is a NameError: totl is a typo for total. Python even suggests the name it thinks you meant.

Some bugs raise no error at all: the code runs and gives a wrong answer. Those are found the same way — print the values at each step and compare them with what you expected — which is why step 3 is the one to practise.

Exercise: price calculator

Create variables for a product name, a price excluding VAT and a VAT rate of 0.2. Print a sentence like "Bamboo board costs £29.99 including VAT", rounded to 2 decimal places.

Your turn# Write your code here, then press Run
Show a solution
name = "Bamboo board"
price_ex_vat = 24.99
vat_rate = 0.2
price_inc = round(price_ex_vat * (1 + vat_rate), 2)
print(f"{name} costs £{price_inc:.2f} including VAT")
Exercise: find the silent bug

This code runs without an error, but the average is wrong (it should be 4.0). Add print lines to find out why, then fix it.

Your turnratings = [5, 3, 4, 4]

total = 0
for r in ratings:
    total = r

average = total / len(ratings)
print("Average rating:", average)      # prints 1.0, but should be 4.0
Show a solution
ratings = [5, 3, 4, 4]

total = 0
for r in ratings:
    total = total + r          # the bug: this line said  total = r
    print("after", r, "total is", total)

average = total / len(ratings)
print("Average rating:", average)
# Printing inside the loop showed total being replaced each time instead of added to.

Key takeaways

  • Python is the leading language for AI and data.
  • The code cells on these pages run Python in your browser; Colab does the same for your own projects.
  • Variables hold strings, numbers and booleans.
  • f-strings insert values into text — you'll use them to build prompts.
  • Read the last line of an error first.
  • AI assistants can write code, but you still need to read, check and own it.
  • Calculate at full precision; round or format only when showing a result.
  • Debugging is a routine: read the last line, find the line, print the values, change one thing.