Getting started with Python Series: Step 1

Accessing Python via the terminal

Storytime

When I was first learning to code, one of the most frustrating things at the beginning was actually opening up the place to code and then "bringing in" the dataset! All the tutorials and lectures went through it so quickly that I felt behind before I had even written a single line of code. As a result, step 1 of this blog post series is going to be opening up the terminal and Python on a Mac (sorry Windows users-- I will provide some resources for you at the bottom of this page) and later steps are going to be opening and looking at datasets.

In this blog post, we are going to talk about the terminal and then use the terminal to open up Python.

Navigation using the terminal

In using a computer, there are files that are organized into folders and these folders are organized into other folders. The terminal is an easy way to navigate between all of these folders and files. So let's see what this actually looks like. First, go to the top right hand side of the computer and look at the magnifying glass.

Screen Shot 2022-09-15 at 9.38.03 AM.png

Click on the magnifying glass and type in "terminal". Click on the first option, the word terminal with a little black screen icon. You then might have a screen appear where you have to give terminal permission to access your Desktop or Downloads.

Screen Shot 2022-09-15 at 9.39.14 AM.png

You should now see a place to type in the terminal to the right of a $. This place to type is called the command line.

Screen Shot 2022-09-15 at 9.42.09 AM.png

Navigating in the terminal

We can use the terminal to navigate around. We now want to tell the computer "commands" or bits of code to run. Pwd and ls are 2 useful commands to try. To do so, type the letters so that they are the left of the $ in the terminal and then press "enter" on your keyboard.

Screen Shot 2022-09-15 at 9.50.13 AM.png

pwd (print working directory) tells you were you are located. When you type pwd and press enter, then you should see something like /Users/your_Mac_login_name/. This means that you are currently in the folder (directory) of your_Mac_login_name rather than a "Desktop" or "Directory" folder.

Now, try ls and then type enter:

Screen Shot 2022-09-15 at 10.28.49 AM.png

After typing the command, your screen should look something like this:

Screen Shot 2022-09-15 at 9.54.45 AM.png

ls lists all of the files and directories (folders) that are "below" /Users/your_Mac_login_name/ that you can navigate to. It might list directories like Desktop and Downloads. In a future lesson, we are going to make a new directory (folder) but for right now, we are just going to use the terminal to open Python.

Aside: Sharing code

In the above sections, I took screenshots of the command line and imported them to the article. This system is okay for 1-2 line commands but not as well for longer commands that you might want to copy and paste. Thankfully, the coding community has come up with a way to share code in articles without having to rely on screenshots! Therefore, instead of looking like this:

Screen Shot 2022-09-15 at 9.54.45 AM.png

The command (for either the terminal or Python) will look like this:

ls

Also, it is normally assumed that you will type in the command provided and then press enter. Therefore, when sharing code below and in future lessons, you should type the command and then press "enter" to run that command.

Using the terminal to open up Python

Python is an especially good language for data analytics and is an extremely versatile language. To see if you have Python on your computer and, if so, what version of Python it is, type the following on the command line.

python --version

If you have Python, then it should say the version that you have in the next line. For example, mine says Python 3.6.4 :: Anaconda, Inc.

  • If the version of Python begins with a 3, then you are all set and can skip to the next section, do 2 things in Python.

  • If it begins with a 2 e.g. Python 2.7.1 or if it says that Python does not exist, then you need to download Python. Click here for the Python downloads website.

Do 2 things in Python

Now, we are first going to open Python and type 2 commands! Type the following on the command line (no spaces). Remember from "Sharing code" that you need to type the command and then press "enter" on your keyboard for the command to run:

python3

It should give you information about the Python version in the terminal and then have a line with >>> and a grey box that you can type:

Screen Shot 2022-09-15 at 10.07.50 AM.png

Python use 1: a basic calculator. Type a math problem.

2+2

Python use 2: printing words. Type print and then a word surrounded by open and closed parentheses and apostrophes.

print('My name is Codo Sapiens and I am going to learn coding')

After pressing enter, you should see what you wrote inside the (' ') on the next line.

Exiting Python and final thoughts

Learning about the terminal and typing these two commands in Python is an awesome start! To quit Python and the terminal, you can simply click the red button in the top left hand corner of the terminal. To quite Python but keep the terminal open, then use the following command (remember to then press "enter").

quit()

Resources for Windows users

This website has some ways to open the terminal.

You can install the latest release of Python3 through the Microsoft Store or directly from the Python makers themselves.

Did you find this article valuable?

Support Codo Sapiens by becoming a sponsor. Any amount is appreciated!