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 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.
Python also supports simple math:
>>> 3 + 2
5
>>> 3 - 2
1
>>> 3 * 2
6
>>> 3 / 2
1.5
>>> Comments are text that does nothing. Everything after a # is ignored.
Like this:
>>> 1 + 2 # can you guess what the result is?
3
>>>Comments are useful for explaining something to people reading the code.
The interactive prompt does this if you enter a comment on a line with nothing else on it:
>>> # hello there
...
>>>I have no idea why this happens and I think it would be better if the
prompt would just stay as >>>. Press Enter twice to get the prompt
back to >>>.
Strings are small pieces of text that we can use in our programs. We can create strings like this:
>>> 'hello'
'hello'
>>> 'this is a test'
'this is a test'
>>> 'string with a # in it'
'string with a # in it'
>>> 'test'
'test'
>>> "test"
'test'It doesn't matter if you use 'single quotes' or "double quotes". Python
prefers single quotes when displaying strings to us, but it understands
both quote styles. Double quotes are useful for strings that contain
single quotes, like "it's weird".
You can also do this:
>>> "hello" + "world"
'helloworld'
>>> "hello" * 3
'hellohellohello'
>>> - 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 and pieces of text in quotes are strings. - You can use single quotes and double quotes however you want.
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.