Welcome | Get started | Dive | Contribute | Topics | Reference | Changes | More

Lesson 1 – Your first date with Python

Open a terminal on your computer:

  • on Linux hit Ctrl+Alt+T,

  • on Windows hit the 🪟 Win key, type “cmd” and hit ENTER

A terminal looks like this:

luc@frodo:~ $ _

Or like this:

C:\> _

This is called the prompt. The prompt indicates that the computer waits for you to type a command.

In case you are reading this on your phone, I encourage you to get up now and find a computer with a keyboard before reading on. One of the primary joys of programming, maybe the ultimate reason why people become programmers, is the discovery that your computer actually obeys you. You won’t get this feeling by reading a text.

In a terminal you type commands, and when you hit ENTER the computer executes them. Every command is one line of text. This way of talking with a computer is also called the command line.

In your terminal, type the command python and hit ENTER. It will say something like:

$ python
Python 3.12.3 (main, Jan 17 2025, 18:03:48) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

The “>>>” prompt indicates that your computer has switched to the Python language and expects you to speak in Python now.

Let’s start with some small talk. Type:

123+456

and then hit ENTER. She will answer instantly:

579

And again she waits for you to say something. Talking with a computer is always like this: you say something, she tries to understand it and then executes your command, and then asks for your next command.

Next command. Try the following:

x=123

Python doesn’t seem to answer anything, but actually has stored a value (123) in a variable (named “x”). You have now defined a variable.

Computers are very good in remembering things. From now on you can say:

x+456

And she will tell you again 579. That’s because she now knows that when you say “x”, you mean 123.

In case you forgot what “x” means, she can remind you. Just type the name of the variable:

x

Now try to say y instead of x

y+456

You’ll get a longer answer:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'y' is not defined

The first lines are a bit strange (imagine she got angry and therefore talked rubbish), but the last line makes sense, does it? Yes, indeed you never told her what “y” means, so of course she doesn’t understand you. Python is nit-picky regarding how to call things.

Okay, let’s tell her what “y” means to you:

y = 456

And then try again:

x+y

Now she answers 567 because yes, when x is 123 and y is 456, then their sum is 579.

Python is very good in computing. Try the following ones as well:

x*y
x-y
x/y

And a few more:

x*x+5
x*(x+5)
x*x*x

You now know the four basic operators of the Python language: +, -, * and /.

A last thing before we stop for this session:

x=2
y=3
x*y

Yes, Python is not like a fundamental traditionalist, she is perfectly okay with changing your mind about what the names x or y mean. You just redefine them. That’s by the way why they are called variables.

How to end a session with Python? Try to say “exit” and she will tell you. On my Linux computer she says:

>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit

On Windows computers it’s different. If you wonder why, then ask me when we sit together in a comfortable place.

Keep in mind

variable

A name that you define so that it gets a “meaning” for Python. This “meaning” is usually called the value of that variable.

value

The “meaning” or the “content” of a variable. For example 123 or 456 are values.

operator

One of the symbols +, -, * and /, which represent a mathematical operation on two values.