Python Guideline for Beginner

TRung Nguyen
The Startup
Published in
6 min readJul 3, 2020

Currently (July 2020) Python is one of the most popular programming languages. According to the PYPL Popularity of Programming Language Index, a search for a tutorial looks for a Python tutorial in 31.17% of the cases [PYPL][1]

Python is a scripting language, whereby each line of the script is read and executed by the Python interpreter. Python scripts may import other Python scripts in the form of packages. The collection of packages used in a Python program should t together and constitutes a Python environment. Building an environment and choosing the right versions of Python packages for a task is best left to a package manager.

Anaconda is a free and open-source tool for Python package management and deployment. Anaconda is platform-independent and will help to manage your python libraries on Windows, Linux, and Mac OS. This means that it will handle module dependencies and provides you with a Python environment. The virtual environment manager allows you to install an independent development environment for each project. You can switch between environments like with other tools (e.g. virtualenv). This tutorial will guide the starter on how to set up the environment from scratch, starting with the installation. If you have already installed Anaconda, skip the Software Installation and start immediately by going to the GettingStarted section.

Software Installation

Installation of the Anaconda package manager can be done in two different ways:

Installation of Miniconda, the console version. Download from Miniconda

Installation of Anaconda Navigator, the GUI version. Download from Anaconda.

In both cases, the package manager itself is named: conda.

This document covers the scope of installing the software on Linux. However, installation on Windows and MacOS is very similar (consult Google on how to install Miniconda or Anaconda Navigator for these operating systems).

For Miniconda, the console version, select the Linux installer (Python 3.x, 64-bit) and download. Install “Just for meand a “~/Miniconda3” folder is created in your home directory. Note: your username should NOT contain spaces. Choose default options for all possible installer choices.

Once installed, open a terminal window and type: “conda -V and see if the installation has been successful. If so, you see the current version of the conda package manager and the (base) prompt, to indicate you are in the base environment, which you will, however, not use very often.

Miniconda

For the installation of Anaconda Navigator (GUI), select the Linux installer (Python 3.x, 64-bit) and download. The GUI is automatically installed when you install Anaconda version 4.0.0 or higher. Install “Just for me” and a “~/Anaconda3” folder is created in your home directory. Note: your username should NOT contain spaces. Choose default options for all possible installer choices.

Note: If you have Miniconda or an older version of Anaconda installed you can install Navigator from an Anaconda Prompt (console) by running the command:” conda install anaconda-navigator “.

The Anaconda Navigator user interface looks like the picture below:

Anaconda navigator
Anaconda navigator

Environments

The initial Miniconda installation comes with a single base environment, which is visible in the terminal prompt.

The command: (base) (pwd) conda list shows all installed packages in the base environment. (pwd) stands for the present folder, included in the terminal prompt as usual. Other conda commands can be given in the terminal to achieve full control over the Python environments and packages. A good practice is, to download a conda cheat-sheet (Google with keyword conda cheat sheet). In such a document, you can nd how to create a virtual environment and activate it.

A new environment is created with: (base) (pwd) conda create -n (env name) python=3.x.

This creates an environment with the basic packages.

An important step is that you have to move to your new environment with:(base) (pwd) activate (env name).

This changes the prompt into: (new name) (pwd) activate (env name). And thus signals you the transfer to your newly created environment. Type conda list to see the basic packages of your new environment.

An alternative option is using the Anaconda Navigator graphical user interface. The following link provides information on environments in Anaconda and the purpose of using them: Environments.

The Anaconda “Environments” window is shown in the figure below. Have a look at the packages installed in the base environment. Create a new environment with the “Create” button in the lower-left of the window. Have a look at the packages installed in your new environment. These steps are identical to the steps executed in Miniconda: under the hood, the GUI types, and carries out the terminal commands for you!

Create an environment

In general, upon creating a new environment, a few (Miniconda) or quite a lot (Anaconda Navigator) standard packages will be installed in the new environment. However, in both cases, most of the useful packages (libraries) need to be installed manually.

There are multiple ways to install missing packages. Search for the missing packages and install them with anaconda-navigator. Look at the instruction on pictures below:

Install packages with Conda Navigator

Installing packages with Console. Keep in mind that the virtual environment needs to be activated before installing the missing packages.

Open terminal
Activate Environment

Find the missing packages from: Anaconda repo.

Copy one of the command in the red box and paste to the terminal.

Install package

For some users, Miniconda will seem a more obvious option for installing new packages in an environment. The activate and conda install -c (channel)(package) commands can just be entered in the terminal window.

Note on using environments: It is customary to create a tailor-made environment for each Python project. Therefore, the information on installed packages needs to be shared among the project contributors. We’ll see later in this document how that is done.

Getting started

Now you can install Jupyter Notebook and Sypder for working with Python. The easiest way to get started is by using a Jupyter notebook. “The Jupyter Note-book is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations, and narrative text. Uses include data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more.” [2].

There are several alternative online Jupyter notebook available for free such as Google Colab, Kaggle,…etc. The instruction on how to set up these online platforms will be discussed in the next tutorial.

Open Jupyter notebook by click “Launch”

Open Jupyter notebook
Create a new python 3 project

Now try to write something with Jupyter notebook and click Run button or “Shift-Enter”

Simple example

Downloading Notebook example. Click execute on Binder to try it online on your browser.

Online example

Github example folder.

Link to relevant information

Anaconda user guide cheatsheet.

Navigator docs.

References

[1] http://pypl.github.io/PYPL.html.

[2] https://jupyter.org/.

--

--