The >>>
means that Python is ready and we can enter a command. The
basic idea is really simple: we enter a command, press Enter, enter
another command, press Enter and keep going.
You probably don't know any Python commands yet. Let's see what happens if we just write something and press Enter.
>>> hello
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'hello' is not defined
>>>
Oops! That didn't work. But like I wrote in the introduction, error messages are our friends. This error message tells us what's wrong and where, and we'll learn what "name 'hello' is not defined" means later.
Maybe we can press Enter without typing anything?
>>>
>>>
>>>
>>>
That worked. How about numbers?
>>> 123
123
>>> -123
-123
>>> 3.14
3.14
>>> -12.3
-12.3
>>>
There we go, it echoes them back.
In some countries, decimal numbers are written with a comma, like 3,14
instead of 3.14
. Maybe Python knows that?
>>> 3,14
(3, 14)
>>>
We didn't get an error... but (3, 14)
is not at all what we expected!
So from now on, let's use a dot with decimal numbers, because 3.14
worked just fine. Later we'll learn what (3, 14)
is.
-WARNING: This part contains boring math. Be careful!
Let's type some math stuff into Python and see what it does.
>>> 17 + 3
20
>>> 17 - 3
14
>>> 17 * 3
51
>>> 17 / 3
5.666666666666667
>>>
It's working, Python just calculates the result and echoes it back.
I added a space on both sides of +
, -
, *
and /
. Everything would
work without those spaces too:
>>> 4 + 2 + 1
7
>>> 4+2+1
7
>>>
However, I recommend always adding the spaces because they make the code easier to read.
Things are calculated in the same order as in math. The parentheses (
and )
also work the same way.
>>> 1 + 2 * 3 # 2 * 3 is calculated first
7
>>> (1 + 2) * 3 # 1 + 2 is calculated first
9
>>>
Python also supports many other kinds of calculations, but most of the time you don't need them. Actually you don't need even these calculations most of the time, but these calculations are probably enough when you need to calculate something.
We can also type a #
and then whatever we want after that. These bits
of text are known as comments, and we'll find uses for them later.
>>> 1 + 2 # can you guess what the result is?
3
>>>
Again, I put a space after the #
and multiple spaces before it just to
make things easier to read.
If we write comment on a line with no code on it, the prompt changes
from >>>
to ...
. To be honest, I have no idea why it does that and I
think it would be better if it would just stay as >>>
. The prompt goes
back to >>>
when we press Enter again.
>>> # hello there
...
>>>
- Error messages are our friends.
- We can enter any Python commands to the interactive
>>>
prompt, and it will echo back the result. +
,-
,*
and/
work in Python just like in math.- Pieces of text starting with a
#
are comments.
If you have trouble with this tutorial please tell me about it and I'll make this tutorial better. If you like this tutorial, please give it a star.
You may use this tutorial freely at your own risk. See LICENSE.