This post is part of the Python Crash Course series. The chronological order on how to read the articles is to be found on the agenda.
In the previous post (see python-as-a-program) we have installed python. It’s now time to play around with it.
First, make sure python is installed:
zsh> python --version
Python 3.11.4 # your version may differ
Launch the program:
zsh> python
>>>
Note: see how the terminal prompt has changed. This shows you are now within the python program.
It’s all fun and games
We are now free to play around the way we like:
>>> 2+2
4
>>> my_string = "hello world!"
>>> print(my_string)
hello world!
>>> a = 4
>>> b = 5
>>> a*b
20
>>> my_number = 42
>>> my_number += 1
>>> print(my_number)
43
When you have had enough, you can simply write quit()
and then hit Enter
to call the exiting method:
>>> quit()
Note: you have to press Enter
for your inline-command to be executed. Outputs are displayed on the next lines before the terminal handovers the process back to you.
Until you get stuck
It might happen you sometime get stuck with your program endlessly looping, performing never ending computations in the background.
This is the case if you have a while
loop with no exit conditions:
>>> while True:
... print("foo")
...
foo
foo
foo
foo
^C
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
KeyboardInterrupt
You can interrupt such never-ending states by sending an exit signal. This is done by pressing Control + C.
Where to go now
By now, you have Python installed and you can run it in your terminal.
It’s a good start but not really convenient so far: after you have exited the program, all your instructions are gone.
It might be ok if you just want to play around. However, we want to be able to save our instructions so we can start building up on it next time we resume back to work.
What you want is to start writing your code into files so your code can actually be saved and retrieved after the session has ended.
This is what we gonna learn in the next chapter: python-files.