Python
What is Python?
Python is an interpreted object-oriented programming language developed by Guido van Rossum in 1991. Among scripting languages, it is the most popular—so much so that most major software libraries are available in this language. Its popularity is due to its simple syntax, dynamic typing, ease of creating packages, and especially the ability to install them from the command line using the PIP installer through the Python Package Index (PyPI).
This simple guide is useful for those who want to learn Python from scratch and have never had the chance to do so. If I find the time, I will also make tutorials on some common libraries, such as NumPy, and Deep Learning libraries like Keras. This guide will focus on Python 3, as versions 1 and 2 are now obsolete.
Installation and Creating a Virtual Environment
Like all interpreted languages, the written code needs an interpreter. That interpreter is Python itself—an executable that can be downloaded directly from the official website along with related tools. However, it is recommended to use a general-purpose, language-independent package manager to install potential dependencies outside of Python easily and efficiently. For this purpose, Anaconda is the most widely used package manager.
To install the full version of Anaconda, visit the official website's installation page or simply click the link. As an alternative to the full version, I recommend installing a lightweight and minimal version of Anaconda, which takes less time to install and includes fewer potentially unnecessary applications: this version is called Miniconda.
Once Anaconda is installed, you can simply type conda
in the command line. Alternatively (if you're on Windows 10 or later), you can search for the Anaconda terminal called Anaconda Prompt from the taskbar.
Once you open the Anaconda prompt, you should see a screen similar to the following (make sure the text base
appears in parentheses before the current folder path):
The word in parentheses indicates the name of the virtual environment, which by default is base
. At this point, it's recommended to create a new virtual environment. The reason is that if something goes wrong, resetting or deleting the base environment can be tricky, whereas other environments can always be easily deleted and recreated. The command to create a new virtual environment with Python already installed is:
conda create -n myenv python
where myenv
is a name you choose—it could be the name of the project you're planning to develop, a random name, or even your girlfriend's name. Get creative.
Once the virtual environment is created, you can activate it simply with:
conda activate myenv
If you create an environment by mistake or have an old environment you no longer need, you can always delete it—after making sure you've deactivated it—using:
conda deactivate toxicenvironment
conda remove -n toxicenvironment --all
The --all
option removes all installed packages as well (which is what you want if you're trying to free up disk space). Now you're ready to use Python. If you want, you can open the Python interpreter from the command line and enter instructions directly by typing:
python
As we'll see, it's often more convenient to use a file editor (I recommend Notepad++ on Windows) and run the file with the interpreter using:
python filename.py
For convenience, I also recommend changing the current directory to the one containing your script, so you don't have to type the full path. To do this, simply type:
cd path/to/file
And with that, we conclude this first introductory lesson. In the next ones, we'll start writing our first programs, introducing functions, variables, and basic operators.
Comments