Skip to main content
Techlogia — AI and Web Development Berlin

Practise this module for free on a real VM

Sign up for free
 All courses
Free to read – no sign-up

Python Basics

First steps with Python: print, variables, if/else, loop, function. Write 5 short scripts as .py files.

Duration: 30 minLevel: BeginnerExercises: 5

Your first Python scripts

Python basics: your first scripts

Python is one of the most-used programming languages — easy to read and ideal to start with. A script is a text file ending in .py that you run with python3 file.py.

Key terms

  • print(): outputs something to the screen.
  • Variable: a named storage slot for a value, e.g. name = "Max".
  • input(): reads input from the keyboard — the program waits until you press Enter.
  • if/else: makes decisions depending on a condition.
  • for loop: repeats something for each element.
  • Function (def): a named, reusable block of code.

Work in the folder /home/student/python/. Edit files with nano, run with python3 file.py.

How the checker works

It really runs your script — with its own test inputs — and compares the output. A script that looks good but contains an error does not pass; every script may run for at most 5 seconds. So always test it yourself with python3 before you click "Check".

Your goal

You write five small scripts and learn the most important building blocks of any programming language.

Exercises

  1. 1. Hello world with print

    Concept: print(). The function print() displays text. Write a script that outputs exactly Hello World.

    mkdir -p /home/student/python
    echo 'print("Hello World")' > /home/student/python/hallo.py
    python3 /home/student/python/hallo.py

    Check: python3 /home/student/python/hallo.py prints Hello World. The checker runs your script the same way.

  2. 2. Variable + input

    Concept: variable. A variable stores a value under a name. With input() your program gets the value from the keyboard — so it greets everyone by their own name.

    nano /home/student/python/variable.py:

    name = input()
    print("Hello " + name)

    Test it: python3 /home/student/python/variable.py, type Mia and press Enter.

    Check: If you enter Mia, it prints Hello Mia. The checker tests with two different names — so a hard-coded greeting does not pass.

  3. 3. A decision with if/else

    Concept: if/else. With if you test a condition; else is the case when it does not hold. Important: the code inside if/else is indented (4 spaces). int(...) turns the input into a number so you can compare it.

    nano /home/student/python/entscheidung.py:

    age = int(input())
    if age >= 18:
        print("adult")
    else:
        print("minor")

    Test with python3 /home/student/python/entscheidung.py and the inputs 17 and 18.

    Check: For 17 it prints minor, for 18 it prints adult. The checker tests exactly this boundary.

  4. 4. A for loop

    Concept: for loop. A for loop repeats a block for each element of a sequence. range(1, n + 1) produces the numbers 1 to n.

    nano /home/student/python/schleife.py:

    n = int(input())
    for i in range(1, n + 1):
        print(i)

    Test with the input 3 — it prints 1, 2, 3 one below the other.

    Check: The script counts from 1 to the entered number, one number per line. The checker tests with 3 and with 6.

  5. 5. Your own function

    Concept: function (def). A function bundles code under a name so you can reuse it. It takes parameters and hands back a result with return.

    nano /home/student/python/funktion.py:

    def square(number):
        return number * number
    
    x = int(input())
    print(square(x))

    Test with the input 4 — it prints 16.

    Check: funktion.py defines a function with def and a parameter, and the script prints the square of the entered number. The checker tests with 4 and 7.

Knowledge check: Python basics

No VM, about 3 minutes. You recall what you practised in this module – wrong answers come back once more at the end.

Related courses

Now practice it yourself

Reading is good – doing is better. Practise this module on a real Linux VM, right in your browser. A free account is all it takes.

Start for free

Lab content under CC BY 4.0 – free to use with attribution (© TechLogia).

How do you like this page?