Skip to content

cat posts/en/python-lernen-01-setup-erste-zeile.md

Learning Python (Part 1): Installation and your first program

Install Python, try out the interactive REPL, and run your very first script: the start of our little text adventure.

Medieval illuminated manuscript: A knight in a red cloak sits in the grass typing on a laptop, while across from him a blue snake with a yellow belly and a golden crown rears up.

This is the start of a series in which you learn Python from scratch. It's explicitly aimed at people with no programming experience. I don't assume you know any other programming language. All you need is to be comfortable with files, a text editor, and the terminal. Everything else comes step by step.

A small text adventure ties the series together: bit by bit, you program a dungeon. Each part adds a new language concept until it turns into a small, playable game.

Today we get Python running, try out the interactive shell, and write our first program.

Installing Python

Python is available for Windows, macOS, and Linux. Install a current stable version if you can. For this series, you need at least Python 3.12. You don't need pre-release versions such as alphas, betas, or release candidates.

Windows: Install Python with the Python Install Manager. After that, you can usually start Python with python. The Install Manager also provides the py command.

macOS: If there's a /usr/bin/python3 on your system, it belongs to Apple's developer tools, and you shouldn't change or delete it. Instead, install a current Python version with the installer from python.org.

Alternatively, you can install Python with Homebrew:

brew install python

On macOS, you usually run Python as python3.

Linux: Many distributions already ship with Python 3. Check the installed version first:

python3 --version

If Python is missing, install it with your distribution's package manager if possible. If the installed version is older than 3.12 and the repositories don't offer a newer one, don't just replace the system Python. Many Linux components depend on exactly that version. In that case, install a newer Python version alongside it.

Checking that it worked

Open a terminal and run the matching command:

Operating system Typical command
Windows python --version or py --version
macOS python3 --version
Linux python3 --version

With a current installation, the output looks something like this:

Python 3.14.7

Remember which command works for you. In this series, I use python for simplicity. If your system uses python3 or py instead, substitute that.

The REPL: Python as a calculator

If you start Python without a file name, you end up in the REPL. The abbreviation stands for read-eval-print loop: Python reads your input, evaluates it, prints the result, and waits for the next input.

You can recognize the REPL by its >>> prompt:

>>> 2 + 2
4
>>> "Hello" * 3
'HelloHelloHello'

Python displays the >>> itself. You only type what comes after it.

The REPL is handy for trying out small expressions and language constructs without creating a file every time. You can leave it with exit() on every system. On macOS and Linux, Ctrl+D also works; on Windows, it's Ctrl+Z followed by Enter.

Creating a project directory

Now create a folder for the files in this series and change into it:

mkdir python-lernen
cd python-lernen

Save the files from the following steps in this directory. That way, you can conveniently start them from the terminal later.

Your first script

If you want to save code and run it again later, you write it to a file. A plain text editor is enough for that. A code editor such as Visual Studio Code is more comfortable.

In your project directory, create a file named hallo.py:

# The first Python program
print("Hello, dungeon.")

The first line is a comment. Python ignores everything from the hash sign (#) to the end of the line. Comments help you explain code, or understand more quickly later on why you wrote something a certain way.

print(...) outputs whatever is between the parentheses. In this case, that's a piece of text, which Python calls a string.

Run the file in the terminal:

python hallo.py

This appears on the screen:

Hello, dungeon.

You've now written and run your first Python program.

Reading input with input

Programs that only print fixed text get boring fast. With input(...), you can ask the user something. The function displays a prompt, waits for input, and returns what was typed as soon as the user presses Enter.

We can store the input in a variable:

name = input("What's your name? ")
print("Hello,", name)

name = gives the input a name we can use to refer to it later. The second line prints it.

Here, print receives two values: the string "Hello," and the variable name. By default, print puts a space between multiple values.

input(...) always returns the input as a string, even if the user types a number. We'll cover how to turn that into an actual number in the next part.

The dungeon, step 1

Enough theory—let's build our game. To start, we need nothing more than print, input, and a variable.

Create a file named dungeon.py:

# Dungeon—Part 1: Greeting
print("You stand before the rusty gate of a forgotten dungeon.")
print("A cold draft blows toward you.")

name = input("What's your name, adventurer? ")

print("Welcome, " + name + "! May your courage be greater than your caution.")

What's new is the plus sign in the last line:

"Welcome, " + name + "!"

You can use + to join strings together. That lets us drop the name we stored earlier straight into the greeting.

A more convenient way to do this is with f-strings. We'll get to know them in the next part.

Run the program:

python dungeon.py

The output looks something like this:

You stand before the rusty gate of a forgotten dungeon.
A cold draft blows toward you.
What's your name, adventurer? Karl
Welcome, Karl! May your courage be greater than your caution.

That's the complete code after part 1. Save the file. We'll build on it in the next part.

Pitfalls

  • python isn't found. On Windows, try py instead; on macOS or Linux, try python3. Then use that same command to run your scripts as well.

  • Forgetting the quotation marks. Without quotation marks, print(Hello) treats Hello as the name of a variable. Since it doesn't exist, the program ends with a NameError. Text goes in quotation marks: print("Hello").

  • The file is in the wrong directory. python dungeon.py only works if your terminal is in the file's directory or you give the path to it. Otherwise, Python reports that it can't find the file.

  • The file is accidentally named dungeon.py.txt. Windows in particular can hide known file extensions. Check that the file really ends in .py.

  • >>> got copied into the file. The >>> characters belong only to the REPL. Don't write them into a Python file.

Exercises

1. Ask for a favorite weapon

After the name, have the dungeon also ask for the player's favorite weapon, and then print both in one sentence.

Solution
print("You stand before the rusty gate of a forgotten dungeon.")

name = input("What's your name, adventurer? ")
waffe = input("What do you fight with? ")

print(name + " enters the dungeon, armed with " + waffe + ".")
`waffe` is German for weapon.

2. Repeating text

What does the following command print?

print("=" * 30)

What could output like that be used for in a text adventure?

Solution The command produces a line of 30 equals signs:
==============================
With strings, the `*` operator repeats the given text. A line like this could, for example, separate rooms, sections, or headings from each other.

Going deeper

Get more out of the interactive shell:

REPL power: IPython and ptpython

What technically happens when a script starts:

What happens when you run python script.py?

Next up

So far, our dungeon only remembers a name. In part 2, we cover values and variables: different data types, numbers, calculating with them, and f-strings.

That gives our adventurer hit points and a proper status line.

0 comments

No comments yet. Be the first!