Python from scratch

13 lessons for people who have never programmed. The code runs right on this page: click “Run”, change it and run it again. Making mistakes is not scary, nothing will break.

On the first run the browser loads Python, which takes a few seconds. Unfamiliar words are in the glossary (in Russian for now).

0 of 17 lessons and projects done

Old engraving: eight snakes, including a reticulated python
  1. Lesson 0. How this course works

    Why. Before writing code, it helps to understand three things: what a program is, why we learn Python and how to study on this page.

    What a program is. A program is an instruction for a computer. It is like a recipe: a cook follows the steps in order, first chopping, then cooking. A computer also runs commands one by one, from top to bottom.

    Why Python. Python code reads almost like ordinary English text, with few extra symbols. People use Python to build websites, process data and automate boring work. That is why it is often chosen as a first language.

    How to study. Every lesson has an explanation, examples and exercises. Click “Run” under an example, and Python will run the code right here, in the browser. You can change the code however you like: the computer won't break.

    Exercises with checking. Write a solution and click “Check”. If it doesn't work out, open the hints one by one. There is a ready solution too, but try on your own first.

    Progress. The “Lesson done” tick is saved in this browser. Look up unfamiliar words in the glossary.

    Try it: run both examples of this lesson. They are already real programs.

    Common question: do I need to install anything?

    No. Python loads into the browser by itself the first time you run code, it takes a few seconds. You will need to install Python on your computer later, for your own project, and there is a separate lesson about it.

    Common question: do I need to be good at maths and English?

    No. School arithmetic is enough. Python has few English words, and we explain what they mean in the lessons.

    In short:

    • a program is commands that the computer runs one by one, from top to bottom;
    • code here runs right in the browser, nothing to install;
    • making mistakes is not scary: try, change and run again.
    Example 1. A recipe step by step
    print("Step 1: I take a cup")
    print("Step 2: I pour the tea")
    print("Step 3: I drink the tea")
    Example 2. Python can count
    print(2 + 2)
    print("2 + 2")

    In the second example the first line calculates, and the second prints the text as is: Python doesn't calculate anything in quotes.

  2. Lesson 1. Your first program

    Why. Any program, even a big website, is made of simple commands. Let's start with the most important one — the command that shows a result.

    The print command shows on the screen what is in the brackets. Text is written in quotes: print("Hello!"). Numbers can be written without quotes: print(2026).

    Order. Each command is written on a new line. Python runs them from top to bottom: what is higher gets printed earlier.

    Several values. One print can list several values separated by commas. Python puts spaces between them itself: print("I am", 16, "years old") prints I am 16 years old.

    An empty line. print() with nothing in the brackets prints an empty line. It is handy for separating one part of the output from another.

    Comments. Everything after the # sign is a note for people. Python skips it. Comments help explain what the code does.

    Try it: write your name in quotes and click “Run”. Then swap the two lines and see how the output changes.

    Common mistake: a missing quote or bracket

    Quotes and brackets always come in pairs. If you write print("Hello!), Python can't find the end of the text: SyntaxError: unterminated string literal Check that every opening quote and bracket has a closing one.

    Common mistake: Print with a capital letter

    Python tells capital and small letters apart. Print and print are different words for it, and it doesn't know the first one: NameError: name 'Print' is not defined Python commands are written in small letters.

    In short:

    • print(...) shows a result: text in quotes, numbers without quotes;
    • commands run from top to bottom, each on a new line;
    • # starts a comment, and quotes and brackets always come in pairs.
    Example 1. First commands
    # This is a comment: Python will skip it
    print("Hello!")
    print("This is my first program")
    Example 2. Several values
    print("My name is", "Aruzhan")
    print("I am", 16, "years old")
    print()
    print("Learning to program since", 2026)

    Exercise: introduce yourself

    Level: Easy

    Write a program that prints two lines: first Hello!, and under it I am learning Python.

    Show the solution
    print("Hello!")
    print("I am learning Python")

    A harder exercise: find the mistakes

    Level: Medium

    The program has three mistakes, and it doesn't run. Fix them so that the program prints My name is Yerlan and I am 16 years old.

    Show the solution
    print("My name is Yerlan")
    print("I am", 16, "years old")
  3. Lesson 2. Numbers: Python as a calculator

    Why. Websites count all the time: the price in a cart, test scores, how many days are left until an exam. So first let's learn to count.

    Operations. Python adds +, subtracts -, multiplies * and divides /. You can print the result right away: print(2 + 3).

    Whole and decimal numbers. There are whole numbers, for example 7, and decimal ones, for example 7.5. The fractional part is separated with a dot, not a comma. Ordinary division / always gives a decimal number: 10 / 2 equals 5.0.

    Whole division and remainder. Imagine 17 sweets packed 5 to a box. 17 // 5 answers how many full boxes you get: 3. 17 % 5 answers how many sweets are left over: 2.

    Powers and order of operations. 2 ** 3 is 2 cubed, that is 8. The order of operations is as in maths: first multiplication and division, then addition and subtraction. Brackets change the order: (2 + 3) * 4 equals 20.

    Rounding. round(3.14159, 2) keeps two digits after the dot: 3.14.

    A number and text are different things. 2 + 2 gives 4, but "2" + "2" gives "22": that is glued text, not a sum.

    Try it: calculate how many minutes there are in a week. Then check how the remainder works: what does 10 % 3 give?

    Common mistake: a comma instead of a dot

    A decimal number needs a dot. If you write print(2,5), there will be no error, but Python will read it as two numbers and print 2 5. Correct: print(2.5).

    Common mistake: division by zero

    You can't divide by zero in Python either. The command print(5 / 0) stops the program: ZeroDivisionError: division by zero Check that the divisor isn't zero.

    In short:

    • + - * / are the usual operations, the fractional part is separated with a dot;
    • // — how many whole times, % — the remainder, ** — a power;
    • numbers are written without quotes: "2" in quotes is already text.
    Example 1. A calculator
    print(7 + 5)
    print(7 - 5)
    print(7 * 5)
    print(7 / 2)
    print(7 // 2)
    print(7 % 2)
    print(2 ** 10)
    Example 2. Shopping
    print("Three notebooks at 450 tenge:", 3 * 450)
    print("With a pen for 200:", 3 * 450 + 200)
    print("Change from 2000:", 2000 - (3 * 450 + 200))
    print("Average grade:", round((5 + 4 + 5) / 3, 1))

    Exercise: seconds in a day

    Level: Easy

    A day has 24 hours, an hour has 60 minutes, a minute has 60 seconds. Print how many seconds there are in a day. Let Python do the calculation.

    Show the solution
    print(24 * 60 * 60)

    A harder exercise: sweets shared equally

    Level: Medium

    47 sweets are shared equally among 6 children. Print two lines: how many sweets each child gets and how many are left over. You should get Each: 7 and Left over: 5.

    Show the solution
    print("Each:", 47 // 6)
    print("Left over:", 47 % 6)
  4. Lesson 3. Variables: how to remember a value

    Why. A program has to remember data: a student's name, test scores, the total in a cart. That is what variables are for.

    What a variable is. A variable is a labelled box. You put a value in the box and then refer to it by the label. name = "Anya" means: put the text "Anya" in the box labelled name.

    The = sign in Python means “put”, not “equals”. The box name goes on the left, and what goes into it on the right.

    A value can change. score = score + 10 means: take what is in score, add 10 and put it back. The short way to write the same thing is score += 10.

    How to name variables. Use Latin letters, digits and the _ sign. A name can't start with a digit. Choose clear names: user_age is better than a.

    Value types. Every value has a type: str — text, int — a whole number, float — a decimal. type(...) tells you the type. int("16"), str(16) and float("2.5") turn one into another.

    Keyboard input. On a computer input("What is your name? ") asks a person and waits for an answer. The answer always comes as text, so a number is read like this: int(input()). In the browser on this site input() doesn't work, so we write values right in the code. And on websites data comes from forms, which get a lesson of their own.

    Try it: put in your own name and age and run again. Add 5 more to the score.

    Common mistake: a typo in a name

    If you create name and then write nmae, Python won't understand what you mean: NameError: name 'nmae' is not defined Check that the name is spelled the same everywhere, down to the letter.

    Common mistake: a number as text

    "16" + 1 won't work: text on the left, a number on the right. TypeError: can only concatenate str (not "int") to str First turn the text into a number: int("16") + 1 gives 17.

    In short:

    • a variable is a labelled box: name = "Anya", the = sign means “put”;
    • a value can change: score += 10;
    • type() tells the type, int(), str(), float() change it.
    Example 1. Boxes with data
    name = "Anya"
    age = 18
    score = 0
    
    print("My name is", name)
    print("In a year I will be", age + 1)
    
    score = score + 10
    score += 5
    print("Score:", score)
    Example 2. Text or a number
    age_text = "16"
    age = int(age_text)
    
    print(type(age_text))
    print(type(age))
    print("In a year:", age + 1)
    print("Glued text:", age_text + "1")

    Exercise: a piggy bank

    Level: Easy

    There are 500 tenge in a piggy bank. Put in 300 more, then spend 250. Change the value of the money variable, and at the end print how much is left.

    Show the solution
    money = 500
    money += 300
    money -= 250
    print(money)

    A harder exercise: age from a form

    Level: Medium

    A form on a website sent the age as text: "15". Work out how old the person will be in 3 years and print In 3 years you will be 18.

    Show the solution
    age_text = "15"
    age = int(age_text)
    print("In 3 years you will be", age + 3)
  5. Lesson 4. Strings: working with text

    Why. Almost everything on a website is text: a student's name, a heading, a message in a form. To work with text, you need to be able to store it, join it and fix it.

    What a string is. In Python text is called a string. It is letters standing in a row, like beads on a thread. A string is written in quotes: "Hello".

    Quotes are the borders of the text. Without them Python will think that Hello is a variable name and report an error.

    Joining strings is done with the + sign: "Hello, " + "Aruzhan" gives Hello, Aruzhan. Put the space in yourself: Python won't add it.

    An f-string is the handiest way to insert a value into text. Imagine a postcard with a gap for a name. The letter f before the quotes tells Python: “there are gaps here”. The gaps themselves are curly braces: f"Hello, {name}!". Python fills in the value of the variable.

    Methods are commands that every string has. They are written after a dot:

    • .strip() removes extra spaces at the start and at the end;
    • .title() makes the first letter capital: aruzhan → Aruzhan;
    • .upper() makes all letters capital, .lower() — small.

    Length and letters by number. len(name) counts how many characters a string has. And you can take one letter by its number in square brackets. Numbers start from zero: name[0] is the first letter, name[1] is the second.

    Try it: in the first example put in your name with extra spaces — the result won't change. Replace .title() with .upper(). In the second example change the city and the age.

    Common mistake: joining text with a number

    You can't add a string and a number with the + sign. The command print("I am " + 18) stops the program with an error: TypeError: can only concatenate str (not "int") to str Python doesn't understand what you wanted: to add numbers or to join text. Use an f-string: print(f"I am {18}"). It turns the number into text by itself.

    Common mistake: a string doesn't change by itself

    A method doesn't fix the string, it returns a new one. So after name.strip() the old spaces stay in name. You need to save the result: name = name.strip().

    In short:

    • text in quotes is a string, strings are joined with the + sign;
    • an f-string puts values into text: f"Hello, {name}!";
    • the methods .strip(), .title(), .upper() return a new string, len() counts characters, name[0] gives the first letter.
    Example 1. Tidying up a name
    name = "   aruzhan   "
    
    clean = name.strip()
    print(clean.title())
    print("Characters in the name:", len(clean))
    print("First letter:", clean[0].upper())
    print("Hello, " + clean.title() + "!")
    Example 2. A postcard with gaps
    name = "Yerlan"
    city = "Astana"
    age = 16
    
    print(f"Hello, {name}!")
    print(f"You are from {city}, and you are {age}.")
    print(f"In a year you will be {age + 1}.")

    Exercise: a participant's badge

    Level: Easy

    The name and the city are already stored in variables, but with extra spaces. Print a badge: the name in capital letters, then from and the city. You should get ARUZHAN from Almaty.

    Show the solution
    name = "  aruzhan "
    city = " Almaty"
    print(f"{name.strip().upper()} from {city.strip()}")

    A harder exercise: a short name

    Level: Medium

    Registers often write a name briefly: the surname, then the first letter of the first name with a dot. The first name and surname are already stored, but in small letters and with spaces. Print Seitkazy A.

    Show the solution
    first = "  aruzhan"
    last = "seitkazy  "
    first = first.strip()
    last = last.strip()
    print(f"{last.title()} {first[0].upper()}.")

Next: part 2

Conditions, lists and loops: your program will learn to make decisions and repeat actions. The first mini-project, a discount calculator, is there too.