In this lesson, we’ll take a look at how we can install Python. Before January 1, 2020, there were two Python versions:
- Python 2.x
- Python 3.x
Python 2.0 was released in 2000 and the latest version, 2.7, was released in 2010. Python 3.0 is from 2008. Python 2.x and 3.x are very similar but there are some (syntax) differences. Because Python 2.x is older, there is still a lot of code out there written for Python 2.x.
Since January 1, 2020, Python 2.7 is retired. When you download Python, it will be the latest 3.x version.
Let’s see how we can install Python on different operating systems.
Linux and Mac OS X
Linux and Mac OS are easy because Python is pre-installed on Mac OS X and most Linux distributions. You can check the Python version from the command line. For example, let’s see what Ubuntu 18.04 LTS has pre-installed:
python --version Python 2.7.17
python3 --version Python 3.6.9
Ubuntu 18.04 LTS comes with Python 2.7 and 3.6.9 pre-installed.
If you want the latest version on Mac OS X, you can always grab it from Python.org MAC OS X downloads. For Linux, upgrading Python depends on your Linux distribution.
Microsoft Windows 10 doesn’t come with Python pre-installed, so we’ll have to install it ourselves. Head over to Python.org downloads and download the installation file:
Open the installation file and you’ll see the following screen:
The installation is straightforward but I would suggest clicking on the “Add Python 3.x to PATH” checkbox. This means you can run Python anywhere from the command line, not just from the folder where it is installed. Hit the Install Now button and you only have to wait a minute for the installation to complete:
Once the installation is successful, click on Close and we are done:
In your start menu, you will find some new items:
This completes our installation.
There are two ways to run Python:
- Integrated Development and Learning Environment (IDLE): Python’s built-in Integrated Development Environment (IDE).
- CLI: You can start Python from the command line.
Let’s try both options.
Click on the IDLE icon in your Windows 10 start menu:
You’ll see a white screen with a Python shell:
>>> that you see above, is the prompt of the interactive Python interpreter. This means you can type Python code and the interpreter will execute it right away. Here is a quick example:
>>> print("hello world") hello world
You can also save Python files and execute them later. Click on “New File” in the “File” menu:
You’ll get a blank screen where you can type your Python code:
Click on “Save” or “Save As” in the “File” menu:
Enter your filename. I’ll go for “hello-world.py”:
Now you can click on “Run Module” under the “Run” menu to execute your code:
You will see the output of your code in the Python shell:
That’s how we execute Python code.
You can also start the Python interpreter from the CLI:
C:\>Python Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
Let’s run some code:
>>> print("hello world") hello world
If you want to exit, type
>>> exit() C:\>
You can also run a Python file directly. For example, let’s try to run the “hello-world.py” file we created with IDLE:
C:\>python hello-world.py hello world
That’s how we can use the CLI to run Python code.
With trinket.io we have two options to run our Python code, similar to how you run code on your own computer:
- Start the interactive Python interpreter.
- Save and run .py files.
I use both options in our lessons. Here is an example of the Python interpreter which executes our code automatically:
In the trinket above, you can see the
>_ button. This means we use the Python interpreter. Below is an example where we use a .py file. You need to click on the
> arrow to execute the code:
In the trinket above, the
> button means we execute a .py file. You can move the separation bar between the two panes to see more of the code or the output if needed.
>button to re-run your code.
We learned a few things about Python:
- How to verify your pre-installed Python version on Mac OS X or Linux.
- How to install Python on Microsoft Windows.
- How to run code using Python’s built-in IDE (IDLE).
- How to run code from the CLI with the Python interpreter.
We are now ready to dive further into Python.