1: Introduction to Python


What is Python?

Python is a high-level programming language first released 20 years ago in 1990. In this case, and in programming in general, "high-level" signifies that smaller, intensive tasks are automatically handled for users.  Python is also considered "high-level" due to the use of actual English words such as printforwhileifelse, and continue. Thus, Python makes coding easier than perhaps other coding languages due to its level of abstraction provided to its users.

 

How is Python used?

Keep in mind that Python is not only powerful because of its legible syntax; once you get the basics, you can find Python packages that easily add functionality to your programs without much work and in most cases, free of cost. For example, instead of creating a program to graph data, many programmers have already created data visualization packages that you can use for free. As you learn, you'll realize how much time you can save installing amazing packages that enable you to focus on creating a product ready for deployment.

To get started we'll install Python and JupyterLab in order to go over some basic coding examples.  JupyterLab is a package that allows users to quickly edit and run multiple snippets of Python code in one file, called a Jupyter notebook.








2: Installing Python


Go to Python's official website

Download PythonGIF

To install Python, download the package from the official Python website https://www.python.org/. Go to the Downloads dropdown menu and click on your operating system. Make sure you choose the correct version for your operating system since the version in the image above is for Windows.

 

Check if Python is installed

macOS Terminal

User-Macbook:desktop user$ python3
Python 3.6.8 (default, Oct  7 2019, 12:59:55) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Windows Command Prompt

C:\Users\Owner\desktop>py
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

If Python is installed correctly, you should have access to the programming language from the command line.

On macOS, enter python3 to launch the Python shell. On Windows, enter py to launch the Python shell. The Python shell allows you to write lines of code in Python.  We will not be using it to practice coding given that it is not a full-fledged code editor.  Instead, we are just checking to see if Python is installed correctly. 

 

Exit the Python shell

macOS Terminal

User-Macbook:desktop user$ python3
...
>>> exit()
User-Macbook:desktop user$

Windows Command Prompt

C:\Users\Owner\desktop>py
...
>>> exit()
C:\Users\Owner\desktop>py

Type exit() to exit the shell on both macOS and Window CLIs.








3: Setting up a virtual environment


Next, we will setup a virtual environment that will contain the JupyterLab package.  We setup JupyterLab and its corresponding notebooks in their own virtual environment to prevent conflicting package requirements.

 

Install the virtual environment module

macOS Terminal

User-Macbook:code user$ pip install venv

Windows Command Prompt

C:\Users\Owner\Desktop\code>pip install venv

To setup a virtual environment we will need to install the venv module, which stands for virtual environment. The prompt in the Terminal or Command Prompt will disappear during downloading only to reappear when the download is complete. Once venv is installed, we will never need to repeat this step when creating future environments.  To install Python packages we use pip, a Python package manager that comes installed with Python.  In later lessons we will install Django with a pip command.  

 

Create the virtual environment

macOS Terminal

C:\Users\Owner\Desktop\code>python3 -m venv jup
C:\Users\Owner\Desktop\code> cd jup

Windows Command Prompt

C:\Users\Owner\Desktop\code>py -m venv jup
C:\Users\Owner\Desktop\code> cd jup

This command will create a folder called jup that is also a virtual environment.  Once the command line returns and the environment is created, cd into jup.  

 

Activate the virtual environment

macOS Terminal

User-Macbook:jup user$ source bin/activate
(jup) User-Macbook:jup user$

Windows Command Prompt

C:\Users\Owner\Desktop\code\jup>Scripts\activate
(jup) C:\Users\Owner\Desktop\code\jup>

We need to activate our virtual environment to ensure that the packages we will install are only available in the virtual environment.  Make sure the virtual environment is always activated when working in the folder, otherwise you will not have access to packages that will be installed.  You can tell the virtual environment is activated when the folder name is in parentheses at the start of the command line. 

 

(For Future Use) Deactivate the virtual environment

macOS Terminal

(jup)User-Macbook:jup user$ deactivate
User-Macbook:jup user$

Windows Command Prompt

(jup)C:\Users\Owner\cesktop\code\jup>deactivate
C:\Users\Owner\desktop\code\jup>

Deactivating the virtual environment is important if you are are moving between projects in the command line. The command to deactivate the environment is simply deactivate. Do not deactivate the environment if you are continuing directly to the next step, but it's good to know for future use. There will no longer be parentheses surrounding the virtual environment name if the environment is successfully deactivated.








4: Installing JupyterLab


Now that we have a designated virtual environment for JupyterLab, it is time to install the program using pip and launch the interface. 

 

Install JupyterLab

macOS Terminal

(jup) User-Macbook:jup user$ pip install jupyterlab

Windows Command Prompt

(jup) C:\Users\Owner\Desktop\code\jup>pip install jupyterlab

Make sure the jup virtual environment is activated and run the command above. Wait for JupyterLab to install on your device. This may take a few minutes. When it is done installing, you will see the prompt reappear and be able to type in more commands.

 

Launch JupyterLab

macOS Terminal

(jup) User-Macbook:jup user$ jupyter notebook

Windows Command Prompt

(jup) C:\Users\Owner\Desktop\code\jup>jupyter notebook

The jupyter notebook command will open your web browser to the address http://localhost:8888/tree and present the JupyterLab interface.  This page is only available on your local device and currently contains no notebooks. In the next lesson you will upload an existing Jupyter Notebook that we provide and begin learning Python.

JupyterLab interface

 

(For future use) Close JupyterLab from the CLI

macOS Terminal

(jup) User-Macbook:jup user$ jupyter notebook
...
Ctrl+C

Windows Command Prompt

(jup) C:\Users\Owner\Desktop\code\jup>jupyter notebook
...
Ctrl+C

Press the keyboard command Ctrl + C for Windows or command + C for macOS, to stop the Jupyter Notebook server and shut down all of the running notebooks or kernels. Do not run this command if you are continuing to the next lesson.






Quiz Questions


1. Python is a high-level programming language. In this context, what does "high-level" signify?


2. Which of the following is a package manager for Python?


Next lesson


Check out the comments and debug buttons if you get stuck.