[Learning Python: From Zero to One] -Installing Python -(1)
Python is cross-platform, it runs on Windows, Mac and various Linux/Unix systems. Writing Python programs on Windows and putting them on Linux is also capable of running.
To start learning Python programming, you first have to install Python on your computer. Once installed, you get the Python interpreter (which is responsible for running Python programs), a command line interactive environment, and a simple integrated development environment.
Install Python 3.11
Currently, there are two versions of Python, version 2.x and version 3.x, which are incompatible. Since version 3.x is becoming more and more popular, our tutorial will be based on the latest Python version 3.11. Please make sure that the version of Python installed on your computer is the latest 3.11.x so that you can learn this tutorial painlessly.
Installing Python on a Mac
If you are using a Mac with OS X >= 10.9, then the version of Python that comes with the system is 2.7. There are two ways to install the latest Python 3.11:
Method 1: Download the Python 3.11 installer from the Python website, download it, double-click it to run and install it.
Method 2: If Homebrew is installed, install it directly via the command brew install python3
.
Installing Python on Linux
If you are using Linux, then I can assume that you have Linux system administration experience and should have no problem installing Python 3 on your own, otherwise, switch back to Windows.
For the large number of students who are currently still using Windows, if you don’t plan to switch to a Mac in the short term, keep reading below.
Installing Python on Windows
First, download the corresponding 64-bit installer or 32-bit installer for Python 3.11 from the official Python website depending on your Windows version (64-bit or 32-bit), and then, run the downloaded exe installer:
In particular, check Add python.exe to PATH
and click “Install Now” to complete the installation.
Running Python
After successful installation, when you open a command prompt window and type python, two scenarios will appear:
Scenario 1:
Seeing the screen above means that Python was installed successfully!
The fact that you see the prompt >>>
means that we are already in the Python interactive environment and can type any Python code, and you will get the result of the execution immediately after Enter. Now, type exit()
and enter to exit the Python interactive environment (just closing the command line window will also work).
Scenario 2: Getting an error
This is because Windows looks for python.exe
based on the path set by a Path
environment variable, and reports an error if it doesn’t find it. If you missed the Add python.exe to PATH
checkbox during installation, you will have to manually add the path where python.exe
is located to the Path.
If you don’t know how to change the environment variables, we recommend running the Python installer again, making sure to remember to check Add python.exe to PATH.
Python Interpreter
When we write Python code, we get a text file with a .py
extension containing Python code. To run the code, we need the Python interpreter to execute the .py
file.
Since the entire Python language is open source, from the specification to the interpreter, theoretically, anyone who is good enough can write a Python interpreter to execute Python code (with great difficulty, of course). In fact, multiple Python interpreters do exist.
CPython
When we download and install Python 3.x from the official Python website, we get an official version of the interpreter directly: CPython, which is developed in the C language, hence the name CPython. running python
from the command line starts the CPython interpreter.
CPython is the most widely used Python interpreter. All the code of the tutorial is also executed under CPython.
IPython
IPython is an interactive interpreter based on top of CPython, that is to say, IPython is only enhanced in the way of interaction, but the function of executing Python code is exactly the same as CPython. It’s as if many domestic browsers have different appearances, but the kernel actually calls IE.
CPython uses >>>
as a prompt, while IPython uses In[serial number]:
as a prompt.
PyPy
PyPy is another Python interpreter that targets execution speed.PyPy uses JIT technology to dynamically compile (not interpret) Python code, so it can dramatically improve the execution speed of Python code.
The vast majority of Python code can be run under PyPy, but there are some differences between PyPy and CPython, which leads to the fact that the same Python code may have different results when executed under both interpreters. If your code is going to be put under PyPy for execution, you need to understand the differences between PyPy and CPython.
Jython
Jython is a Python interpreter running on the Java platform , you can directly compile Python code into Java bytecode execution.
IronPython
IronPython is similar to Jython, except that IronPython is a Python interpreter running on the Microsoft.
Summary
1. Learn how to install Python into your computer and become proficient at opening and exiting the Python interactive environment.
To run Python on Windows, start the command line and then run python.
To run Python on Mac and Linux, open a terminal and then run python3.
2. There are many Python interpreters, but the most widely used is CPython. if you want to interact with Java or .Net platforms, the best way to do so is not to use Jython or IronPython, but rather to interact with them through network calls, ensuring independence between the programs.
All code in this tutorial is only guaranteed to run under CPython version 3.x. Be sure to install CPython locally (installer downloaded from the official Python website).