Skip to content
AI-grafen
CBuilderProgramming· about 45 min· fundamentals that rarely change· verified 2026-09-20· EN

Python — the basics

Be able to write and run a short Python program with variables, if statements, a function and printing, and to read an error message and understand where the fault is.

Prerequisites

Intuition

Python is the language almost all AI code is written in. It reads almost like pseudocode:

score = 12
if score > 10:
    print("Pass")
else:
    print("Try again")

Indentation (four spaces) is not ornament — it decides what belongs to the if.

Code

A function packages code so it can be reused:

def vat(price):
    return price * 0.25

print(vat(200))   # 50.0

Error messages are read from the bottom up. NameError: name 'price' is not defined means you have used a name that does not exist — often a typo, or the variable is created later in the code. TypeError means you have mixed kinds, for instance text + number.

Formal

The types you need first: int (whole numbers), float (decimals), str (text), bool (True/False). type(x) shows the type. int("12") turns text into a number; str(12) does the opposite. input() always gives a str — do not forget to convert.

Mastery means

  • Writes a function with a parameter and a return value
  • Interprets a common error message (NameError, TypeError)

Sign in to do the exercises and build your mastery up.

Sources

All the sources and licences