1. General¶
This section covers all general things about Python and things, which do not belong to any of the other categories.
1.1. Installation¶
I generally recommend installing Python via binary from python.org. However, since Microsoft allows you to install Python via the Windows Store, it’s highly recommended for Windows users to install Python this way. There are a lot advantages of this approach.
Note: As a Mac or Linux user, you may want to add the following alias to your
.bashrc
or.zshrc
as most systems come with Python 2 installed by default andpython
points to the Python 2 installation:alias python='python3'
1.2. Read-Eval-Print Loop (REPL)¶
The Read-Eval-Print Loop (REPL) ships with your Python installation.
In fact, if you enter python
in your console, the REPL should start.
The idea behind the REPL is, that you get immediate feedback about the commands you’ve entered.
Whenever you see >>>
in the code examples, you can be sure, that the code is executed in the REPL.
Otherwise the code will be part of a script.
Example:
>>> 5
5
1.3. Execution¶
If you want to run a Python file via command-line, simply run:
$ python <filename>
E.g.:
$ python my_file.py
1.4. Naming Conventions¶
PEP 8 is the Style Guide for Python Code. It not only specifies code formatting and programming recommondations, but also naming conventions. You should definitely check it out.
Note: As described in the previous paragraph, PEP 8 does also specify code formatting guidelines. However, if you want to use a code formatter, I can highly recommend Black.
1.5. Comments¶
To write a comment, just put a #
symbol in front of it.
Everything after the #
will be ignored (until the end of the line).
To write a multi-line comment, you can use Python’s triple-quoted strings: """
.
# Here's a single line comment
x = 5 # A single line comment after code
"""
This is
a multi-line comment
x = 3
"""
b = 9