Skip to content

Notes: M1: Your first Python program

Writing code feels like it should be hard. It isn't mysterious, a program is just a list of plain instructions a computer follows in order, and Python is a language designed to read almost like English. This module gets you past "I've never coded" to "I wrote a program and I understand every line." Everything in the rest of this course, calling an AI model, building a chatbot, a RAG assistant, an agent, is built from the four ideas below. Master these and the AI half is mostly new vocabulary, not new thinking.

What "programming" actually is

A program is a list of instructions. The computer starts at the top and does them one at a time, in order, until it runs out. That's it. The skill of programming is breaking what you want into small, exact steps, because the computer does exactly what you say, not what you meant.

Python is one programming language (there are many). We chose it because it reads clearly, it's the language most AI tools are built in, and it's forgiving for beginners. When you "run" a Python program, a piece of software called the Python interpreter reads your instructions and carries them out.

flowchart LR
  S["Source code<br/>(the Python you write)"] --> I["Python interpreter<br/>(reads it line by line)"]
  I --> A["Actions<br/>(prints text, does maths, asks questions)"]

Where we run it today: a notebook in the browser

You don't need to install anything yet. We use Google Colab, a free website that runs Python on Google's computers. You type code into a cell, press Run, and the answer appears underneath. A page of these cells is a notebook.

Why start here? Because setup is where beginners quit, wrestling with installers before you've written a single line is demoralizing. Notebooks remove all of that so day one is writing code, not configuring tools. In M3 you'll install Python on your own machine, once you actually need to (to use files and outside libraries). Right up to M2 we stay in the browser.

Notebook vs. a "real" program: a notebook runs your code one cell at a time, which is perfect for learning and experimenting. Later you'll write programs as .py files that run all at once from top to bottom. The code itself is identical, only how you launch it differs.

The four building blocks

Almost every program, including the AI apps later in this course, is built from four basic moves: output, variables, types, and input. Here they are.

1. Output: print()

print() shows something on the screen. Whatever you put inside the brackets gets displayed.

print("Hello, World!")     # shows: Hello, World!
Text you want shown literally goes in quotes: "..." or '...', your choice, as long as they match. The quotes are how Python knows "this is text, show it as-is."

▶ Try it right here — real Python, in your browser, no install. The first run takes a few seconds to wake up.

Prefer more room? Open it as a full browser notebook ↗ (a demo notebook for now).

2. Variables: labelled boxes that remember

A variable is a name that holds a value, so you can use it again later. You create one with =, which means "put the value on the right into the name on the left" (it is not the "equals" of maths).

name = "Ada"       # the box called name now holds the text Ada
print(name)        # shows: Ada
Read name = "Ada" as "let name be Ada." From then on, writing name is the same as writing "Ada". Change the value and everything that uses the variable updates, that's what makes code reusable.

3. Types: what kind of value it is

Every value has a type, which tells Python what it can do with it. Today you meet three:

Type Python name Looks like Example
Text string (str) in quotes "Ada", '42 Main St'
Whole/decimal number integer (int) / float no quotes 36, 42.50
True or false boolean (bool) the words True / False True

The type matters because it decides what's allowed. You can do maths on numbers (42.50 * 2) but not on text. And "36" (text) is not the same as 36 (a number), a trap that bites everyone early. The fix is converting between them:

  • float("42.50") turns text into a decimal number (so you can do maths).
  • int("36") turns text into a whole number.
  • str(36) turns a number into text (so you can glue it into a sentence).

This matters more than it looks: input() always gives you text, even when the person types a number. To do maths on what they typed, you convert it first, exactly what float(input(...)) does in the lab.

4. Input: input()

input() asks the person a question, waits for them to type and press Enter, and hands back what they typed, as text.

your_name = input("What's your name? ")   # waits, then stores their answer in your_name
Input + output together make a program interactive, it has a little conversation with the person, which is exactly what a chatbot does (you'll build one in M4).

Putting values into sentences

Two ways you'll see for combining text and variables:

Gluing with + (concatenation), joins pieces of text end to end:

print("Nice to meet you, " + your_name)
This only works on text. "age: " + 36 is an error, because you can't glue text to a number, you'd need "age: " + str(36).

f-strings (the clean, modern way), put an f before the quotes and drop variables inside { }:

print(f"Hello {your_name}, you are {age}.")
Python replaces each {...} with that variable's value, numbers included, no converting needed. You can even format inside the braces, f"${total:.2f}" rounds a number to 2 decimal places, like money. f-strings are what you'll use everywhere from here on.

Comments: notes for humans

Anything after a # on a line is a comment: Python ignores it completely. Comments are for the people reading the code (including future you) to explain why something is there.

tip = bill * 18 / 100   # 18% of the bill
Good code uses comments to explain intent, not to restate the obvious.

Errors are normal (and they help)

When something's off, Python stops and prints a red error message (a "traceback"). This is not a disaster, it's the single most common thing that happens while coding, even for experts. The last line names the problem. Two you'll meet today:

  • SyntaxError, a typo in the shape of the code: a missing quote, bracket, or colon. Python couldn't even read the line.
  • NameError: name 'x' is not defined, you used a variable before creating it (often a spelling slip, like naem instead of name).

The move is always the same: read the last line, find the small thing, fix it, run again. You are not behind when you see an error; you're programming.

Go deeper (optional, not needed for today's win) - **Why "float"?** Decimal numbers are stored in a format called *floating point*. A surprise to file away: `0.1 + 0.2` prints `0.30000000000000004`, because not every decimal can be stored exactly in binary. It rarely matters for everyday maths, and you round for display with `:.2f`. - **Single vs double quotes** are identical in Python. Use one kind so the *other* can appear inside without fuss: `"it's fine"` or `'she said "hi"'`. - **`=` vs `==`:** one `=` *assigns* (puts a value in a variable); two `==` *compares* (asks "are these equal?", giving `True`/`False`). You'll use `==` for decisions in M2. - **The Python interpreter** is itself a program (written mostly in C). "Running Python" means asking it to read and carry out your instructions, which is why you can run the *same* code in Colab, on your laptop, or on a server, and get the same result.

Check yourself

Lock in today's win, answer each in your head, then reveal.

1. What does a program do, in one sentence?

Show answer

It follows a list of instructions in order, top to bottom, doing exactly what each line says. Programming is breaking a goal into small, exact steps.

2. What's the difference between 42 and "42"?

Show answer

42 is a number (you can do maths on it). "42" is text (a string), the digits four and two as characters. "42" + "1" gives "421" (glued text), while 42 + 1 gives 43. Convert text to a number with int(...) or float(...) before doing maths.

3. Why do we wrap input() in float(...) when we want a number?

Show answer

Because input() always returns text, even if the person types 42.50. float(...) turns that text into a decimal number so maths works. Without it, bill * 18 would either error or repeat the text, not calculate.

4. What will this print, and why?

name = "Sam"
print("name")

Show answer

It prints the literal word name: not Sam. Inside quotes, text is taken exactly as written. To print the variable's value you'd drop the quotes: print(name), or use an f-string: print(f"{name}").

5. You run code and get a red NameError. What's the calm next move?

Show answer

Read the last line of the error, it names the undefined variable. Usually it's a typo (naem vs name) or you used a variable before creating it. Fix the small thing and run again. Errors are normal and they point you straight at the problem.


New words (also in resources/glossary.md): program, Python, interpreter, notebook, cell, print, string, integer, float, boolean, variable, assignment, type, type conversion (cast), input, concatenation, f-string, comment, syntax error.

Source: original, written for this course. Concepts and the worked examples (strings, variables, types, print, f-strings, basic maths) are informed by the instructor's own python_basics.pdf notes in Material_AI_Engineering/. No third-party text or figures; the diagrams are original.