Practise this module for free on a real VM
Sign up for freePython Basics
First steps with Python: print, variables, if/else, loop, function. Write 5 short scripts as .py files.
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.forloop: 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. Hello world with print
Concept:
print(). The functionprint()displays text. Write a script that outputs exactlyHello World.mkdir -p /home/student/python echo 'print("Hello World")' > /home/student/python/hallo.py python3 /home/student/python/hallo.pyCheck:
python3 /home/student/python/hallo.pyprintsHello World. The checker runs your script the same way.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, typeMiaand press Enter.Check: If you enter
Mia, it printsHello Mia. The checker tests with two different names — so a hard-coded greeting does not pass.3. A decision with if/else
Concept:
if/else. Withifyou test a condition;elseis the case when it does not hold. Important: the code insideif/elseis 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.pyand the inputs17and18.Check: For
17it printsminor, for18it printsadult. The checker tests exactly this boundary.4. A for loop
Concept:
forloop. Aforloop 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 prints1,2,3one below the other.Check: The script counts from 1 to the entered number, one number per line. The checker tests with
3and with6.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 withreturn.nano /home/student/python/funktion.py:def square(number): return number * number x = int(input()) print(square(x))Test with the input
4— it prints16.Check:
funktion.pydefines a function withdefand a parameter, and the script prints the square of the entered number. The checker tests with4and7.
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 freeLab content under CC BY 4.0 – free to use with attribution (© TechLogia).
