A Beginner’s Guide to Python

Melissa Mullen
simple CS
Published in
7 min readAug 25, 2020

Computer programming is at the heart of the modern world. All technology is powered by computer programs. Apps you download to your iPhone, websites you frequent on the internet, and even Netflix, all rely on humans telling machines what to do. If your work is not directly related to technology development, you might be tempted to leave this machine communication to the experts. If you do this, you will be missing out on obtaining a power to make your life easier. What’s more, you will be missing out unnecessarily. One of the greatest inhibitors to learning to code is a fear that it will be too difficult. Computer programming does not have to be complicated. You can learn it quickly and efficiently, and it will improve your life.

Applications

If you have a Mac OS, you are likely familiar with the Applications tab in the Finder window. Every application on your computer was created from a folder (directory) containing text files on a computer. These files can be opened in any text editor (e.g. TextEdit or Microsoft Word). It is written in a computer programming language that is human-readable. That is, if you were to open the file, you would recognize English words on the page. Programming languages allow humans to communicate with computers. They allow the human to give the computer a list of instructions to execute. The computer is capable of understanding this human-readable code because it has a handy English-to-machine translator called a compiler.

Applications on a Mac.

There are hundreds of programming languages. You do not need to learn all of them. This is because they are all (for the most part) capable of accomplishing the same things.

Programming languages can be divided into two categories: high-level and low-level. High level programming languages are easier to learn and use. Python is an example of a high-level language.

Low-level programming languages are the opposite — harder to learn and use. So why would anyone learn and use them? High-level programming languages are themselves written in low-level languages.

Python

Basics

Python is the first language you should learn. It will allow you to quickly understand what sort of tasks your computer is capable of completing.

If you have a Mac OS, you likely already have Python installed. If you don’t, you can download and install it here.

Once you have Python installed, open the Terminal Application (Mac OS) or MobaXterm (Windows). If you are unfamiliar with the command line, I strongly suggest reading this article. Run the following command to create a new Python file.

touch file.py

Now, open your file using the following command.

vim file.py

Press “i” to insert and type the following in the file. You can put any word you want within the quotation marks. I have chosen the word “Hi”.

print(“Hi”)

This is Python for “print the word Hi to the screen”. Press Esc:wq to save and quit. Run the following command to run your code.

python file.py
Running a simple Python file called file.py that prints the word “Hi” to the terminal.

Congratulations! You have run your first computer program. You will notice the word “Hi” printed in the terminal, which is what you asked the computer to do (in Python).

Jupyter Notebook

While learning to code on the command line like this is useful, I find that most people learn best using an Integrated Development Environment, or IDE. An IDE is an application (just like Google Chrome or Microsoft PowerPoint) with a text editor (similar to TextEdit or Microsoft Word) made explicitly for programmers. My favorite for coding in Python is Jupyter Notebook.

Run the following command to download and install Jupyter Notebook.

pip install notebook

Next, run the following command to open Jupyter Notebook.

jupyter notebook

Jupyter Notebook will open in a new tab in your default browser. Select New > Python 3. At this point your screen should look something like this.

An empty Jupyter Notebook.

Try typing the same Python command you typed in file.py on the command line. Then, instead of exiting and running your file on the command line, press Shift+Enter.

A line of Python code in a Jupyter Notebook to print the word “Hi” to the screen.

You will notice that your computer program has been executed, and “Hi” is printed to the notebook, just as it was printed to the terminal when you ran it there.

You can give the computer other instructions too. For example, your computer can perform mathematical operations.

Two lines of Python code in a Jupyter Notebook to add 2 + 3 and print the result to the screen.

Functions

Functions are my favorite part of coding. They allow you to organize code into logical chunks for future re-use. For example, say I want to add 2 numbers together. I can write a function that will do this for me. I can call the function whatever I want (in this case, I will call it add), but Python requires a specific format for function definitions. The function add would will look like this.

A basic function to add 2 numbers (x and y) together and return the result.

In this function, x and y are the input arguments, and z is the return value. In English, this Python function is saying, “Add x and y, and give me the result”. We can use the function like so.

Using the user-defined function add().

Focus on the second to last line of code: result = add(1,2). This line of Python is telling the computer, “Find the function called add. Within the function add, let x = 1 and y = 2. Assign the return value to the variable result”. This is an important point. Whenever you use a function with a return value (in this case, z, the sum of x and y), you will want to set it equal to a new variable (in this case result). That new variable will then be equal to the return value of the function. That is, the variable result will be equal to z, the sum of 1 and 2.

Note that a variable in computer programming is just like a variable in algebra — the only major difference is that in computer programming, variable names are typically longer and more descriptive than letters of the alphabet.

Modules, Packages, and Libraries

Python actually already includes a function that will add numbers together. It is called sum and it can be used like so.

Using the built-in function sum() to add 2 numbers.

The sum function even allows you to add more than 2 numbers together.

Using the built-in function sum() to add 5 numbers.

Both print() and sum() are defined in the Python standard library, which is the code that you installed when you installed Python. These commands that exist in the Python standard library are called built-in functions.

Python also offers additional functionality in the standard library that is more difficult to access. This functionality is contained in Python files called modules. A module is a Python file containing function definitions. Although these modules exist in the Python standard library, you need to explicitly import them into your code at the top of your file. In order to access the functions within a module, you must type the name of the module, followed by a dot, followed by the function name. For example, a very useful Python module is the os module. A very useful function within the os module is the getcwd() function, which gets (returns) your current working directory. This module and function can be imported and used in a Python script like this.

Importing a Python module called “os”, and using a function within the module called “getcwd()”.

There are millions of people around the world who are writing additional Python libraries that we can also use in our code. A library is a collection of packages. A package is a directory of Python modules. We can access these libraries by downloading and installing them using “pip”, Python’s built-in package installer. For example, a very useful Python library is numpy, which can be installed by running the following command.

pip install numpy

Once these libraries are installed, you need to import them into your code at the top of the file if you want to use functions within them. It is possible to assign aliases to the libraries when you import them, so that you can avoid typing out the full library name every time you need to access one of its functions. The standard alias for the numpy library is np. You can import it into your file using this alias like so.

Importing a Python library (to use alongside the Python standard library) called “numpy” using the alias “np”, and using a function within the library called “arange”.

For the moment, ignore the printed array. Simply note that np can be used as an alias for numpy to access the function arange within the numpy library.

Other Libraries

We have only brushed the surface of Python libraries. There are hundreds of thousands of Python libraries available for you to download and install using pip, and import and use in your code. Here are a few of my favorites.

  • numpy : a useful library for creating and manipulating lists (arrays)
  • pandas : an excellent library for working with data from Excel
  • matplotlib : a library for creating plots
  • scipy : an essential library for any mathematician
  • PySimpleGUI : a library for creating graphical user interfaces (GUIs)

Next Steps

The previous section was a basic introduction to Python computer programming. There is much more to know, but as with learning any new skill, it is important not to bombard yourself with too much too soon. For now, play around on Jupyter Notebook, assigning numbers to variables and creating small functions. It is possible to create a wealth of functionality with what you now already know.

--

--