Appendix C — Python tutorial#

Click the binder button Binder or this link bit.ly/pytut3 to run this notebooks interactively.

Abstract: In this tutorial, I’ll introduce you to the basics of the Python programming language. Don’t worry, learning Python is not complicated. You’re not going to become a programmer or anything like that, I’m just going to show you how to use Python as a calculator. We’ll start by setting up a Python coding environment on your computer (JupyterLab Desktop). We’ll then cover basic building blocks like expressions, variables, functions, lists, for-loops, etc. By the end of this tutorial, you’ll be familiar with the essential parts of the Python syntax needed to use the most kick-ass, powerful calculator that ever existed.

Introduction#

Python is a fancy calculator#

Python commands are similar to the commands you give to a calculator when you want to compute something. The same way a calculator has different buttons for the various arithmetic operations, the Python language has a number of commands you can “run” or “execute.” Knowing how to use Python gives you access to hundreds of useful libraries and thousands of functions, which are like calculator buttons specialized for different domains: math, science, statistics, machine learning, etc.

Why you should learn#

Here are some things you can do using Python:

  • You can use Python as a basic calculator for doing arithmetic calculations using math operations like +, -, *, /, and other math functions.

  • Python is also a scientific calculator since it provides functions like sin, cos, log, etc.

  • You can use Python as a graphical calculator to plot functions and visualize data.

  • Python is an extensible, programmable calculator that allows you to define your own functions and operations.

  • Python provides powerful libraries for numerical computing (numpy), scientific computing (scipy), and symbolic math calculations (sympy).

  • Python has libraries for data management (pandas), data visualization (seaborn), and statistics (statsmodels).

If any of these seems useful to you, then read on to get started learning Python!

How to learn Python#

The fastest way to learn Python is through hands-on experimentation in an interactive coding environment like a Jupyter notebook. By running various Python commands and seeing the results they produce, you can quickly learn what Python functions (calculator buttons) are available, and what they do.

This tutorial is intended for absolute beginners with no prior programming experience. I’ll guide you step-by-step through the code examples, and explain what the code does in plain English. You can then explore the Python code examples, run them on your own, or “play” with them by changing the commands and running them with different inputs to see how the outputs change. I’ve also prepared some exercises for you, that will allow you to practice what you’ve been learning.

Getting started#

Follow the six steps outlined below to setup a computational learning environment on your computer that will allow you to run this tutorial interactively.

Installing JupyterLab Desktop#

JupyterLab is a computing platform that makes it easy to run Python code. JupyterLab provides a notebook interface that allows you to run and edit Python commands interactively, which is the learning environment I want you to have while reading the rest of this tutorial. JupyterLab Desktop is a convenient all-in-one application that you can install on your computer to run JupyterLab.

STEP 1: Download JupyterLab Desktop#

Visit the web page github.com/jupyterlab/jupyterlab-desktop and choose the download link for your operating system. Complete the installation steps.

STEP 2: Start JuputerLab Desktop#

Launch the JuputerLab Desktop application and choose the New session… option from the menu. You might be prompted to install a “bundled Python environment,” which you should accept. After this is done, you should see a window similar to the one shown below.

Screenshot of the JupyterLab user interface showing File Browser in the left sidebar and the main launcher window open. The button called Python 3 (ipykernel) is used to create a new Python 3 notebook.

STEP 3: Create a new notebook#

Click the Python 3 (ipykernel) button to create a new Jupyter notebook.

Notebooks are interactive documents#

A Jupyter notebook consists of a sequence of cells, similar to how a text document consists of a series of paragraphs. The notebook you created in STEP 3 has a single empty code cell, which is ready to accept Python commands.

STEP 4: Try a numerical calculation#

Enter the expression 2 + 3 into the code cell, then press SHIFT+ENTER to run the code. You should see the result of the calculation displayed on a new line immediately below your input. The cursor will automatically move to the next code cell, as shown in the screenshot below.

Screenshot showing the contents of Jupyter notebook with labels for the input and output of the first code cell. The screenshot also highlights the notebook execution control buttons: run, stop, restart, and run all.

Notebook execution controls#

The menu bar at the top of the notebook contains the notebook execution control buttons. The play button executes the code in the current cell, and is equivalent to pressing SHIFT+ENTER. The stop and restart buttons can be used to interrupt a computation that is stuck or taking too long. The run all button is useful when you want to rerun all the cells in the notebook from start to finish.

Code cells contain Python commands#

Each code cell in a notebook is a command prompt that allows you to enter Python commands and “run” them by pressing SHIFT+ENTER, or by clicking the play button in the toolbar. Let’s see the example commands from screenshot again. We can make Python compute the sum of two numbers by entering 2+3 in a code cell, then pressing SHIFT+ENTER.

2 + 3
5

When you run a code cell, you’re telling the computer to evaluate the Python command it contains. Python then displays the result of the computation immediately below. In the above example, the input is the expression 2 + 3, and the output is the number 5.

Let’s now compute a more complicated math expression \((1+4)^2 - 3\). The Python equivalent of this math expression is shown below.

(1+4)**2 - 3
22

Note the Python syntax for math operations is similar to the notation we use in math: addition is +, subtraction is -, multiplication is *, division is /, and we use the parentheses ( and ) to tell Python to compute 1+4 first. The syntax for exponents is a little unusual, we use two multiplication signs (asterisks) to denote exponentiation: \(a^n\) = a**n.

Running a code cell is similar to using the EQUALS button on a calculator: whatever math expression you entered, the calculator will compute its value and display its result. The interaction with the Python calculator is similar: you input some Python code, press SHIFT+ENTER and Python will run your code and display the result.

STEP 5: Your turn to try!#

Type in some expression involving numbers and operations in a new code cell, then run it by pressing SHIFT+ENTER or by using the play button in the toolbar.

STEP 6: Download the tutorial notebook#

Click this link bit.ly/pytut3nb to download the notebook python_tutorial.ipynb and save it to a permanent location on your computer (e.g. create a new folder Documents/LearningPython/ and place the tutorial notebook in that folder). Switch back to the JupyterLab window and use the File Browser panel to navigate to the location on your computer where you saved the notebook python_tutorial.ipynb, then open it.

If you are reading this in print or as a static document (web page or PDF), I expect you to follow along with the notebook on your computer for the remainder of this tutorial, so that you can play with the code examples interactively. The tutorial notebook contains many pre-filled code cells with examples of Python commands for you to run. I encourage you to edit the contents of the code cells and re-run them to see how the output changes. Remember that trial-and-error is the best way to learn Python!

Cell operations menu#

In the top-right corner of each code cell, you’ll find a useful set of cell operations.

Cell operation menu. Use these buttons to move the current cell, duplicate it, or add a new cell above or below the current cell.

You can use insert new cell below button to create a blank code cell at any place in the notebook and experiment with some variation of the code you are learning. Try running the code with different numbers, or come up with your own examples. Think of this notebook as your personal playground for exploring Python commands. Don’t worry you can’t break anything!

Learning on hard mode#

If you are feeling particularly ambitious, you can try manually typing-out all the code examples into a new notebook. In general, I recommend that you type things out by yourself rather than relying on copy-paste. It’s good to practice the speed of your typing skills and develop the “muscle memory” for entering special characters like [, ], {, }, >, <, #, etc.

Expressions and variables#

Most Python commands involve computing the value of some Python expression and storing the resulting value in a variable. If our goal is to learn Python, then we better start by explaining what Python expressions are and how to storing values into variables works.

Expressions#

A Python expression is an arbitrary combination of values, operators, and function calls. We write expressions and ask Python to compute their value. Here is an example of an expression involving several math operations, like the ones we saw earlier.

5**2 - 3*7
4

Let’s now look at a fancier expression that shows other aspects of the Python syntax like lists and function calls. The following expression computes the sum of a list of three numbers using the function sum.

sum( [1,2,3] )
6

In Python, we define lists using the square brackets [ and ]. The code [1,2,3] defines a list of three elements: 1, 2, and 3. The function sum computes the sum of the list of numbers. The parentheses ( and ) are used to delimit the input to the function. We’ll have a lot more to say about lists and functions later on in this tutorial. For now, I just wanted to show you an expression that involves something other than arithmetic operations, as a preview of what is to come.

Python comments#

Comments are annotations in code cells intended for human readers. We write comments in Python by prefixing them with the character #. Python will ignore the # character and any text that comes after it on that line.

# this is a comment

The text “# this is a comment” is ignored by the Python interpreter. As far as Python is concerned, the above code cell is completely empty. I’ll use comments in this tutorial to provide an additional “narrative” for each line of code.

At first I’ll use comments at the literal level to explain how to read the code (What). I’ll also use comments to explain what special syntax does (How). Towards the end of the tutorial, the comments will switch to explaining the intent of each command (Why).

Variables#

A variable is a name we use to refer to some value. This is similar to how variables are used in math. Variables are used to represent constants, function inputs, function outputs, and all kinds of intermediate values in calculations. We use the assignment operator = to store values into variables.

The assignment operator#

In all the code examples above, we computed various Python expressions, but we didn’t do anything with the results. The more common pattern in Python is to store the result of an expression evaluation into a variable.

For example, here is the code that computes the expression 2+3 and stored the result in a variable named x.

x = 2+3   # calculate 2+3 and store the result into x

The result of this expression is that the value 5 gets saved in the variable x. Python evaluates this command from right-to-left: Python first looks at the expression 2+3 and computes its result 5, then it sees the remainder of the statement x =, which means we want to store the result into the variable x. The assignment statement doesn’t have any output, so running this code cell doesn’t display anything.

To display the contents of the variable x, we can write its name in a code cell.

x   # print x
5

Note the information about the variable x is available in this code cell, even though we defined it in the previous code cell. This is the whole point of the computational notebook environment: every code cell runs in a “context” that includes the results from all the previous code cells. When you use the run all button in the toolbar, and read the notebook from top-to-bottom, it’s as if you’re telling a story, which define different characters (variables) and show them in action (computations).

Note also we didn’t need to call any special function to display the contents of the variable. This is because, in a notebook interface, the value of the last expression in each code block gets displayed automatically. Later in the tutorial, we’ll learn about the function print that you can use to display values in the middle of a code cell or to customize how values are displayed.

We can combine the commands “assign the result of 2+3 to x” and “display x” into a single code cell, as shown below.

x = 2+3   # store 2+3 into x
x         # print x
5

The first line in this code block computes the expression 2+3 and assigns the result to the variable x. The second line evaluates the variable x to display its contents.

Exercise 1: using the assignment operator#

Imitate the above code block, to create another variable y that contains the value of the expression \((1+4)^2 - 3\), then print the contents of the variable y to show the result.

# Instructions: put your answer in this code cell

Click for solution.

Reading the assignment operator#

The meaning of the assignment operator = is not the same as the meaning of the operator \(=\) used for writing math equations, so you should not read x = 5 as “x equals 5.” The assignment statement x = 5 tells Python to store the value 5 (the right-hand side) into the variable x (the left-hand side), so the correct way to read this statement is, “set x to 5”, “Put 5 into x”, or “record 5 under the name x”. The symbol = is not a passive statement of equality, but an active verb telling Python to store the result of an expression into a particular variable.

To summarize, the syntax of an assignment statement is as follows:

<place> = <some expression>

The assignment operator “=”is used to store the value of the expression <some expression> into the memory location <place>, which is usually a variable name. Later in this tutorial, we’ll learn how to store values in other places (inside containers like lists and dictionaries).

Using multi-line expressions for step-by-step calculations#

Okay so how does storing values into variables help us with real-world practical calculations? Let me show you an example of a complicated calculation that we can do easily in Python.

Example 1: number of seconds in one week#

Let’s say we need to calculate how many seconds there are in one week. We know there are \(60\) seconds in one minute, \(60\) minutes in one hour, \(24\) hours in one day, and \(7\) days in one week. We can calculate the number of seconds in one week step by step, where in each step we perform a very simple calculation. We’ll define several intermediate variables and give the variable descriptive names to help us keep track of what is going on.

one_min = 60              # number of seconds in one minute
one_hour = one_min * 60   # calc. number of sec in one hour 
one_day = one_hour * 24   # calc. number of sec in one day 
one_week = one_day * 7    # calc. number of sec in one week 
one_week                  # print the value of one_week
604800

Note we can use the underscore _ as part of variable names. This is a common pattern for variable names in Python code, because the name some_name is easier to read than somename.

Okay, let’s see if you can do this now. Here are some real-world calculations you might want to perform. Let’s see if you can use what we have learned about Python variables and expressions to solve these exercises on your own.

Exercise 2: weight conversion#

Johnny weights 107 kg, and wants to know his weight in pounds. One kilogram is equivalent to 2.2 lbs. Can you write the Python expression for computing Johnny’s weight in pounds?

I have already created a code cell where you can write your answer, and even started the code for you by defining the variable weight_in_kg which contains the weight in kilograms. To complete this exercise, I want you to replace the ... placeholder with a Python expression that uses the variable weight_in_kg.

# Instructions: replace ... with your answer

weight_in_kg = 107  # store 107 into weight_in_kg
weight_in_lb = ...

Click for solution.

Exercise 3: tax calculation#

You’re buying an item at the store and the price is \(\$50\) dollars. The government imposes a \(10\%\) tax on your purchase. Calculate the total you’ll have to pay, including the \(10\%\) tax.

# Instructions: replace the ...s with the appropriate calculations

price = 50.0
taxes = ...
total = ...

Click for solution.

Exercise 4: temperature conversion#

The formula for converting temperatures from Celsius to Fahrenheit is \(F = \tfrac{9}{5} \cdot C + 32\). Given the variable C that contains the current temperature in Celsius, write the expression that calculates the current temperature in Fahrenheit and store the answer in a new variable named F.

# Instructions: replace the ... with a Python expression

C = 20
F = ...

Check your formula works by changing the value of C, and re-running the code cell. When C = 100, the temperature in Fahrenheit should be 212.0.

Click for solution.

Variable types#

Every variable in Python has a type, which tells you what kind of data it contains, and what kind of operations you can do with it. There are two types of variables for storing numbers, and another type for storing text. There are also several types of “container variables” like lists and dictionaries.

Here is a list of the most common types of variables in Python:

  • Integers (int): used to store whole numbers like 42, 65, 78, -4, -200, etc. Python integers are roughly equivalent to math set of integers \(\mathbb{Z}\).

  • Floating point numbers (float): used to store decimals like 4.6,78.5, 1000.0 = 1e3, 0.001 = 1e-3, 123.456 = 1.23456e2, etc.

  • Lists (list): ordered container for other values. For example, the list [61, 79, 98, 72] contains four integers. The beginning and the end of the list are denoted by the square brackets [ and ], and its elements are separated by commas.

  • Strings (str): used to store text like "Hello", "Hello everyone". Strings are denoted using either double quotes "Hi" or single quotes 'Hi'.

  • Boolean values (bool): logic variables with only two possible values, True or False.

Let’s look at some examples of the different types of variables.

score = 98                  # an int
average = 77.5              # a float
scores = [61, 79, 98, 72]   # a list
message = "Hello everyone"  # a str
above_the_average = True    # a bool

To see the contents of the variable score, we simply type its name.

score
98

What type of variable is the variable score? To see the type of a variable, we can call the function type like this:

type(score)
int

The output tells us that score is of type int (an integer).

Exercise 5: values and types of variables#

Print the contents and the type of the variables average, scores, message, and above_the_average that were defined above.

# Instructions: Use this code cell to show the contents of each variable
#               then use the function `type` to print its type.

Click for solution.

Technical jargon: objects and methods#

The technical term object is used to describe the fundamental building blocks we use to store data when programming. We store different types of data into different types of objects, and the integers, floating point numbers, lists, strings, and boolean values are examples of different types of objects. Calling the function type on the object obj, type(obj), tells us the type of the object obj.

Objects have certain functions “attached” to them, which we call methods. Different types of objects have different kinds of methods, which make it easy to manipulate the data stored in the object. Earlier, we defined the Python list object scores = [61,79,98,72], which is a container that holds four numbers. The list scores has methods like .append, .insert, .extend, .reverse, .sort, etc. These methods allow us to perform operations on the underlying data in the list. For example, if we want to sort the values in the list in increasing order, we can call the method scores.sort() and the list will become [61,72,79,98].

Every value in Python is an object. Learning Python requires being familiar with the different object types and their method. Later on in this tutorial, we’ll have a whole section on lists where we’ll describe their methods and use cases. There are also sections that cover boolean values, strings, and dictionaries. The key point to remember is that Python objects are like different types of LEGOs available for you to play with, and remember that you can use the function type to find out what kind of object you’re working with.

Functions#

Functions are the essential building blocks of Python programs. Functions allow us to encapsulate any sequence of operations as a reusable piece of functionality. You can think of a function as a chunk of code that you define once, then use multiple times by “calling” it from other places your code.

Calling functions#

The Python syntax for calling the function named “fun” with the input “arg” is to wrap the input in parentheses: fun(arg). The syntax for calling functions in Python is borrowed from the math notation \(f(a)\), which is used to describe evaluating the function \(f\) on the input \(a\). A Python function takes certain variable(s) as input and produces a certain variable as output. In programming, function inputs are called arguments and the output of the function is called its return value.

Python built-in functions#

Python functions are like the different calculator buttons you can press to perform computations. Here is a list of the most common Python functions:

  • type(obj): tells us the type of the object obj.

  • sum(mylist): calculates the sum of the values in the list mylist.

  • print(obj1,obj2,...): displays the objects obj1, obj2, etc. in the output cell.

  • len(mylist): returns the length of the list object mylist.

  • help(obj): displays help information about the object obj.

  • range(a,b): creates the list of numbers [a,a+1,...,b-1].

  • str(obj): converts the object obj into text (str).

You’ll learn to use all these functions (and many others) in later sections of this tutorial.

Multiple arguments#

Some functions can accept multiple arguments, and we use commas to separate these arguments: fun(arg1,arg2,arg3). For example, we can call the function print with several arguments to display them together on a single line.

print("The average is", average, "percent.")
The average is 77.5 percent.

Keyword arguments#

Some functions accept optional keyword arguments (also called options) that modify the function’s behaviour. For example, the print function accepts the keyword argument sep (separator) that controls what text is used to separate the different values you want to print. The default value for the option sep is a single space " ", which is why the number 77.5 appears separated by spaces from the text in the output above.

We can specify a different value for the sep keyword argument, if we want to use a different separator, as shown below.

print("average", average, sep="=")
average=77.5

Python syntax for defining new functions#

Python makes it easy to define functions, which is like adding new buttons to the calculator. To define a new function called fun, we use the following syntax:

def fun(arg):              # define the function `fun` that takes `arg` as input
    <calculation step 1>   # first line of calculations
    <calculation step 2>   # second line of calculations 
    return <result>        # specify the output value of the function

You can think of this function definition as adding the new button fun to the Python calculator. Let’s go over the code example above line-by-line to explain all the new elements of syntax. We start with the Python keyword def, then give the name of the function we want to define, which is fun in this case. Next, we specify the arguments that the function expects inside parentheses. In this example, the function fun takes a single input called arg. The colon : indicates the beginning of the function body. The function body is an indented code block (each line begins with four spaces) that specifies the calculations that the function is supposed to perform on the input arg. The last line in the function body is a return statement that tells us the output of the function (its return value).

Once we have defined the function fun using the def-syntax above, we can now call the function in code cells below this one. For example, calling fun(5) will perform the calculations steps 1 and 2 on the value 5, then return the value output calculated from this input. In other words, anywhere you see the function call expression fun(5), you can mentally replace it with the output value computed by fun when arg is 5. I know this may seem complicated as hell at first sight, but I assure you that it will make sense after we look at some examples.

Example 2: adding the double button to the calculator#

Let’s define a new Python function called double, which takes a number as input and multiplies the number by two.

def double(x):  # define the function `double` with input `x`
    y = 2*x     # compute 2 times `x` and store the result in `y`
    return y    # return the value of `y` as output

Note the body of the function (the second and third lines) is indented by four spaces. In Python, we use indentation to delimit when a code block starts and when it ends. In the above example, function’s body contains two lines of code, but in general, Python functions can contain dozens or even hundreds of lines of code: any sequence of operations you might want to perform on the input x to calculate the output y.

To call the function double, we use the function name followed by the function’s input argument in parentheses, as we saw earlier.

double(6)  # call the function `double` on the input `6`
12

Let’s narrate what happened in the above code cell from Python’s perspective. When Python sees the expression double(6) it recognizes it as a function call. It then looks for the definition of the function double and finds the def statement in the previous code cell, which tells it what steps need to be performed on the input. It then performs the steps in the function body with the value 6 stored in the variable x, which results in 12 as the return value. Visit the link tinyurl.com/bdzr69s9 to see a visualization of what happens when we call the function double with different inputs.

Example 3: tax calculating function#

Let’s add a function that adds 10% tax to a given purchase price and returns the total of the price plus taxes.

def add_taxes(price):       # define the function `add_taxes` with input `price`
    taxes = 0.10 * price    # compute 10% of price and store in taxes
    total = price + taxes   # compute the total
    return total            # return the value in `total`

We can now use the function add_taxes to calculate total cost of purchasing different kinds of items.

add_taxes(50)   # calculate the total cost of purchasing a $50 item
55.0
add_taxes(100)  # calculate the total cost of purchasing a $100 item
110.0

Try to narrate what happened when Python evaluated the above function call expressions. You can imitate the narrative from the point of view of Python like the one for the function call double(6), but applied to the function calls add_taxes(50) and add_taxes(100). This is an important exercise that I highly recommend so you’ll become comfortable with the semantics of function calls. Click this link tinyurl.com/muufc5cn to see a visualization of the add_taxes function calls.

Example 4: calculating the mean of a list of numbers#

The formula for computing the mean (average value) of a list of numbers \([x_1, x_2, \ldots, x_n]\) is \(\overline{\mathbf{x}} = \left( x_1 + x_2 + \cdots + x_n \right) / n\). In words, the average value is the sum of the values, divided by the length of the list. Let’s write the function mean that computes the mean of a list values.

def mean(values):           # define the function `mean` with input `values`
    n = len(values)         # compute the length of the list
    avg = sum(values) / n   # compute the average using the formula
    return avg              # return the value in `avg`

The calculations this function performs are the same as the math formula. Let’s try the function on a list of numbers.

mean([1,2,3,4])   # calculate the mean of the values [1,2,3,4]
2.5

Indeed, \((1+2+3+4)/4 = 10/4 = 2.5\), so the function seems to be working as expected.

Exercise 6: temperature conversion function#

Define a Python function called temp_C_to_F that converts temperatures from Celsius C to Fahrenheit F. The formula for converting a temperature in Celsius to a temperature in Fahrenheit is \(F = \tfrac{9}{5} \cdot C + 32\).

Hint: You can reuse the code from Exercise 4.

# Instructions: replace ... with your answer

def temp_C_to_F(C):
    ...

Click for solution.

There is a lot more to learn about Python functions, but there are also other topics I want to show you, so we’ll have to move on now.

Python lists and for-loops#

Python lists allow us to easily manipulate thousands or millions of values. A for-loop is a programming construct used to repeat some operation multiple times. We often use for-loops to perform a calculation for each element in a list. We’ll now describe how Python lists work, then describe for-loops in the next section.

Lists#

The Python syntax for creating a list starts with an opening square bracket [, followed by the list elements separated by commas ,, and ends with a closing bracket ]. For example, here is how to define the list scores that contains four integers:

scores = [61, 79, 98, 72]   # define a list of four numbers
scores
[61, 79, 98, 72]

A list has a length, which you can obtain by calling the function len on it.

len(scores)   # compute the length of the list `scores`
4

We use the in operator to check if a list contains a certain element.

98 in scores   # Is the number 98 contained in list `scores`?
True

The result is the boolean value True, which means the answer is: “Yes, the number 98 is contained in the list scores.”

Accessing elements of a list#

We access the individual elements of the list scores using the square brackets syntax scores[<idx>], where <idx> is the 0-based index of the element we want to access. You can think of indices as analogous to the street numbers used to address different locations on a street.

Visualization of the Python list  that shows the indices 0, 1, 2, 3 and the values 61, 79, 98, 72 of the four elements

The visualization above shows the different indices and values of the elements in the list scores.

The first element of the list has index 0, the second element has index 1, and so on. The last element has an index equal to the length of the list minus one.

scores[0]   # first element in the list `scores`
61
scores[1]   # second element in the list `scores`
79
scores[3]   # last element in the list `scores`
72

Note the semantics used to access list elements is completely separate from the semantics used to define lists, even though both are based on the square brackets syntax [ and ]. The [ and ] in the expression [1,2,3] means “create a list,” while the [ and ] in mylist[0] means “get the first element of mylist.”

List slicing#

You can extract a subset of a list using the “slice” syntax a:b, which corresponds to the range of indices a, a+1, …, b-1. For example, if you want to extract the first three elements in the list scores, use the slice 0:3, which is equivalent to the indices 0, 1, and 2.

scores[0:3]   # first three elements of the list `scores`
[61, 79, 98]

The result of selecting a slice from a list is another list.

List methods#

List objects can be modified using their methods: .sort(), .append(), .pop(), and .reverse(). Remember a method is a function “attached” to a given object that we use to perform certain operations on that object.

We’ll now look at some examples that illustrate how to use the dot-syntax for calling list methods.

To sort the list of scores, you can call its .sort() method.

scores.sort()   # sort the list in increasing order
scores          # print `scores` to show it is now sorted
[61, 72, 79, 98]

Use the .append() method to add a new element to the list.

scores.append(22)   # add `22` to the end of the list `scores`
scores              # print `scores` to show `22` was added
[61, 72, 79, 98, 22]

The method .pop() extracts the last element of the list:

scores.pop()  # pull out the last element of the list `scores`
22

You can think of .pop() as the “undo operation” of the append operation.

To reverse the order of elements in the list, call its .reverse() method:

scores.reverse()   # reverse the order of elements in the list
scores             # print `scores` to show it is now reversed
[98, 79, 72, 61]

Other useful list methods include .insert(idx,newel) and .remove(el), which allow you to insert new elements or to remove elements at arbitrary places in the list.

For loops#

The for-loop is a programming concept that allows us to repeat some operation or calculation multiple times. The most common use case of a for-loop is to perform some calculation for each element in a list.

The syntax of a Python for-loop looks like this:

for <element> in <container>:
    <operation 1 using `element`>
    <operation 2 using `element`>
    <operation 3 using `element`>

This code tells Python to repeat the operations 1, 2, and 3 for each element <element> in the list <container>. The operations we want to repeat are indented by four spaces, which indicates they are part of the body of the for-loop. Recall we saw indented code blocks previously for the function body when defining a function. The syntax is the same here: all the lines at the same indentation level are part of the code block.

Example 5: print all the scores#

We start with a basic example of a for-loop that simply prints the value of each element.

scores = [61, 79, 98, 72]   # define a list of four numbers

for score in scores:        # repeat for each score in the list scores:
    print(score)            #   print the value of `score`
61
79
98
72

A for-loop describes a certain action (or actions) that you want Python to repeat. The for-loop shown in the code cell above instructs Python to repeat the action print(score) four times, once for each score in the list scores. The operation we want to repeat is indented by four spaces and hence “inside” the for loop.

Example 6: compute the average score#

The math formula for computing the mean (average value) of a list of numbers \([x_1, x_2, \ldots, x_n]\) is: \(\overline{\mathbf{x}} = \left( x_1 + x_2 + \cdots + x_n \right) / n\). We previously computed the average using the functions sum and len: avg = sum(grades)/len(grades), but suppose we don’t have access to the function sum for some reason, and we want to compute the sum of the grades using a for-loop. Here is the code for computing the average score.

total = 0                   # variable used to store cumulative sums
for score in scores:        # repeat for each score in the list scores:
    total = total + score   #   add score to the running total

# At this point, total contains the sum of the scores.

avg = total / len(scores)   # compute the average and store in `avg`
avg                         # print `avg`
77.5

On the first line, we define the temporary variable total (initially set to 0), which we’ll use to store the intermediate values of the sum after each iteration of the for-loop. Next, the for-loop tells Python to go through the list scores. For each score in the list, we perform the operation total = total + score. After the for loop is finished, the sum of all the scores is stored in the variable total, as if we calculated total = sum(scores). To obtain the average, we divide total by the length of the list.

Visit this link tinyurl.com/3bpx887v to see a step-by-step visualization of the four steps (iterations) of the above for-loop execution, which shows how the variable total grows after each iteration. See also this blog post for more background on for-loops.

Loop variable names are arbitrary#

The name of the variable used for the for-loop is totally up to you, but you should choose a logical name that accurately describes the list elements. For example, if the list is called objs, it makes sense to use obj as the name of the loop variable: “for obj in objs:”. Given a list profiles, we would write the for-loop as “for profile in profiles:”. Given a list nodes, we would use for-loop like “for node in nodes:”, etc.

List comprehension (bonus topic)#

We often need to transform a list of values, by applying the same transformation to each value in the list. Using the standard for-loop syntax to apply the function fun to all the elements in a list requires four lines of code:

newvalues = []                  # storage space for the transformed values
for value in values:            # repeat for each value in the list values
    newvalue = fun(value)       #   compute `fun(value)` and store in `newvalue`
    newvalues.append(newvalue)  #   add transformed value to the end of `newvalues`

Python provides the list comprehension syntax, which is shorthand for describing a for loop as a single line of code.

newvalues = [fun(value) for value in values]
# apply `fun` to all `values` and store the result in `newvalues`

Here is an example of using the list comprehension syntax to apply the function double on all the numbers in the list numbers, and store the result in a new list called doubles.

numbers = [1, 2, 3, 4, 5]      
doubles = [double(number) for number in numbers]
doubles
[2, 4, 6, 8, 10]

List comprehension syntax the preferred way to express simple list transformations, since it easier to read and fits on a single line.

Boolean variables and conditional statements#

Boolean variables#

Boolean variables represent logical conditions that are either True or False. We obtain boolean values when we perform numerical comparisons using the operators like <, >=, <=, == (equal to), != (not equal to).

x = 3   # store 3 in the variable `x`
x > 2   # Is `x` greater than 2?
True

Another context where boolean values come up is when we use the in operator to check if an object is part of a list (or other container).

3 in [1,2,3,4]   # Is the number 3 contained in the list [1,2,3,4]?
True

Conditional statements#

Boolean values are used in conditional statements, which are blocks of Python code that may or may not be executed, depending on the value of a boolean value. The Python keywords if, elif, and else are used to create conditional statements.

Let’s start with some examples of if statements.

if True:
    print("this code will run")

if False:
    print("this code will not run")
this code will run

The indented code block inside an if statement is executed only when the if condition is True. We can extend an if statement with an else clause, which allows us to specify a different code block that will run if the condition is False.

x = 3

if x > 2:
    print("x is greater than 2")
else:
    print("x is less than or equal to 2")
x is greater than 2

We can check multiple conditions by using additional elif statements after the initial if, as shown in the next example.

temp = 8

if temp > 22:
    print("It's hot.")
elif temp < 10:
    print("It's cold.")
else:
    print("It's OK.")
It's cold.

When Python sees this if-elif-else statement, it checks all the if and elif conditions one-by-one, and if it finds a condition that is True, it will execute the corresponding code block. If none of the if and elif conditions are True, then the else code block will be run. At the end of this if-elif-else statement, exactly one of the print commands will be executed.

Exercise 7: temperature assessment#

Add another condition to the temperature code to print It's very hot! if the temperature is above 30.

temp = 33

# Instructions: edit the code below to insert the new condition

if temp > 22:
    print("It's hot.")
elif temp < 10:
    print("It's cold.")
else:
    print("It's OK.")
It's hot.

Click for solution.

Boolean expressions#

You can use the logical operations and, or, and not to combine individual boolean values into larger boolean expressions that check multiple conditions. The result of an and operator (logical conjunction) is True only if both operands are true, and False otherwise, as shown in the following table.

Boolean expression

Value

True and True

True

True and False

False

False and True

False

False and False

False

The or operator (logical disjunction) will result in True as long as at least one operand is True:

Boolean expression

Value

True or True

True

True or False

True

False or True

True

False or False

False

The not operator performs the negation of a boolean value.

Boolean expression

Value

not True

False

not False

True

Here is an example of using the and operator to check two conditions simultaneously.

x = 3
x >= 0 and x <= 10   # Is `x` between 0 and 10?
True

Try changing the value of x to make the boolean expression False.

Exercise 8: water phases#

The phase of water depends on temperature. The three possible phases of water are "gas" (water vapour), "liquid" (water), and "solid" (ice). The table below shows the phase of water depending on the temperature temp, expresses as math inequalities.

Temperature range

Phase

temp >= 100

gas

0 <= temp < 100

liquid

temp < 0

solid

Fill in the if-elif-else statement below to print the correct phase depending on the variable temp.

temp = 90   # the water temperature

# Instructions: Fill-in the code of the if-elif-else statement by replacing
#               the ...s with the appropriate inequality conditions
#               and water phases (either "gas", "liquid", or "solid").

if ...:
    print(...)
elif ...:
    print(...)
else:
    print(...)
Ellipsis

Click for solution.

Exercise 9: assigning letter grades#

Teacher Joelle has computed the final scores of the students as a percentage (a score out of 100). The school where she teaches requires her to convert each student’s score to a letter grade according to the following grading scale:

Grade

Numerical score interval

A

90% - 100%

B

70% - 89.999…%

C

50% - 69.999…%

F

0% - 49.999…%

Write an if-elif-elif-else statement that takes the score variable (a number between 0 and 100), and prints the appropriate letter grade for that score.

score = 90   # student score

# Instructions: Fill-in the code of the if-elif-elif-else statement by replacing
#               the ...s with the appropriate conditions and letter grades.

if ...:
    print(...)
elif ...:
    print(...)
elif ...:
    print(...)
else:
    print(...)
Ellipsis

Click for solution.

Other data structures#

We already discussed lists, which are the most common data structure (container for data) in Python. In this section, we’ll briefly introduce some other Python data structures you might encounter.

Strings#

In Python, strings (type str) are the containers we use for storing text. We can create a string by enclosing text in single quotes ' or double quotes ".

message = "Hello everyone"
type(message)
str

String concatenation#

We can use string the + operator to concatenate (combine) two strings.

name = "Julie"
message = "Hello " + name + "!"
message
'Hello Julie!'

Strings behave like lists of characters#

You can think of the string "abc" as a list of three characters ["a", "b", "c"]. We can use list syntax to access the individual characters in the list. To illustrate this list-like behaviour of strings, let’s define a string of length 26 that contains all the lowercase Latin letters.

letters = "abcdefghijklmnopqrstuvwxyz"
letters
'abcdefghijklmnopqrstuvwxyz'
len(letters)  # length of the string
26

We can access the individual characters within the using the square brackets. For example, the index of the first letter in the string is 0:

letters[0]
'a'

The index of the letter "b" in the string letters is 1:

letters[1]
'b'

The last element in list of 26 letters has index 25

letters[25]
'z'

We can use the slice syntax to extract a substring that spans a certain range of indices. For example, the first four letters of the alphabet are:

letters[0:4]
'abcd'

The syntax 0:4 is a shorthand for the expression slice(0,4), which corresponds to the range of indices from 0 (inclusive) to 4 (noninclusive): [0,1,2,3].

Tuples#

A tuple is similar to a list, but with fewer features. The syntax for defining a tuples is base don the parentheses, and the elements of the tuple are separated by commas.

(1, 2, 3)
(1, 2, 3)

Actually, the parentheses are optional: we can define a tuple by using writing a bunch of commas-separated values.

1, 2, 3
(1, 2, 3)

I want you to know about this comma syntax, because it is very convenient for printing several values at the end of a code cell in a notebook environment. For example, here is how we can display the first, second, and last characters from the string letters on a single line.

letters[0], letters[1], letters[25]
('a', 'b', 'z')

Dictionaries#

One of the most useful data structure when programming is the associative array, which is called a dictionary (dict) in Python. Associative arrays are sometimes called a hash-tables or lookup tables. A dictionary is a container of key-value pairs. The syntax for creating a dictionary is based on "key": value pairs, separated by commas, wrapped in curly braces { and }. For example, the code below defines the dictionary profile that contains three key-value pairs.

profile = {"name":"Julie", "age":31, "score":98}
profile
{'name': 'Julie', 'age': 31, 'score': 98}

A dictionary is a mapping from keys to values, as illustrated below.

Visualization of the dictionary profile that shows its keys and its values side by side.

We use the keys to access the different values in the dictionary. The keys of the profile dictionary are "name", "age", and "score".

profile.keys()
dict_keys(['name', 'age', 'score'])

The values associated with these keys are "Julie" (a string), 31, and 98 (integers).

profile.values()
dict_values(['Julie', 31, 98])

We access the value in the dictionary using the square brackets syntax. For example, to get the value associated with key "score", we use:

profile["score"]
98

Recall, we used the square bracket syntax earlier for accessing the values within a list. Indeed, lists and dictionaries are both containers objects, and we use square brackets syntax to access elements within them.

You can change the value associated with a key by assigning a new value to it, as follows:

profile["score"] = 77
profile
{'name': 'Julie', 'age': 31, 'score': 77}

You can also add a new key-value pair to the dictionary by assigning a value to a key that doesn’t exist yet:

profile["email"] = "julie@site.org"
profile
{'name': 'Julie', 'age': 31, 'score': 77, 'email': 'julie@site.org'}

Note the profile dictionary now has a new key "email" and the value "julie@site.org" stored under that key.

Exercise 10: creating a new profile dictionary#

Create a new dictionary called profile2 with the same keys as dictionary profile for the user "Alex" who has age 42 and score 65.

profile2 = ...  # replace ... with your answer

Click for solution.

Type conversions#

We sometimes need to convert between variables of different types. The functions for conversing types have the same name as the object types:

  • int : converts any expression into an int

  • float: converts any expression into a float

  • str: converts any expression into a str

For example, suppose you’re given the number "42.5" as a string.

type("42.5")
str

To convert this string to a floating point number, we can call the function float.

f = float("42.5")
f
42.5
type(f)
float

Now that we have converted the string "42.5" to the float 42.5, we can do numerical operation like +, -, and * with this number.

Example 7: compute the sum of two numbers#

Suppose we’re given two numbers \(m\) and \(n\), and we want to compute their sum \(m+n\). However, the numbers \(m\) and \(n\) are given to us as strings, so we don’t get the expected result when we add them together.

mstr = "2.1"
nstr = "3.4"
mstr + nstr
'2.13.4'

This is because the addition operator + for strings means concatenate, not add. We have to manually convert the strings to numbers, if we want to add them together as numbers.

mfloat = float(mstr)
nfloat = float(nstr)
mfloat + nfloat
5.5

Exercise 11: type conversion and sum#

Write the Python code that converts values in the list prices to floating point numbers and adds them together.

Hint: use a for-loop or the list comprehension syntax.

prices = ["22.2", "10.1", "33.3"]

# write the code to compute the total price

Click for solution.

Python packages and modules#

All the code examples we showed above were using the Python built-in functions and data types, but that is only a small part of the functionality available in Python. There are hundreds of Python packages and modules that provide additional functions and data types for all kinds of applications. There are Python modules for processing different data files, making web requests, doing efficient numerical computing, calculating statistics, etc. The list is almost endless!

Import statements#

We use the import keyword to load a Python module and make it available in the current notebook environment. The command to import the module <module> in the current context is:

import <module>

After this import statement, we can use the all functions from the module <module> by calling them using the prefix <module>., which is called the “dot notation” for accessing names within the <module> namespace. For example, here is how we can import the statistics module and use the function statistics.mean to compute the mean of a list of numbers.

import statistics
statistics.mean([1,2,3,4])
2.5

Alias import statements#

A very common trick you’ll see in many Python notebooks, is to import python modules under an “alias” name, which is usually a shorter name that is faster to type. The alias-import statement looks like this:

import <module> as <mod>

For example, importing the statistics module under the alias stats looks like this.

import statistics as stats
stats.mean([1,2,3,4])
2.5

The alias-import statement allows us to use the shorter names stats.mean and stats.median instead of typing out the full module name each time, like statistics.mean and statistics.median. In data science, it is very common to alias-import the numpy module as np, the pandas module as pd, and the seaborn module as sns.

Selective import statements#

It is also possible to import only a specific function from a module using the following syntax.

from <module> import <function>

This allows you to use <function> directly in your code, without the <module>. prefix.

from statistics import mean
mean([1,2,3,4])
2.5

The Python standard library#

The Python standard library refers to the hundreds of Python modules that come bundled with every Python installation. Here are some of the most commonly used modules from the standard library:

  • math: math functions like sqrt, sin, cos, etc.

  • random: random number generation

  • statistics: descriptive statistics functions

  • datetime: manipulate dates and times

  • urllib: tools to manipulate URLs

  • json: read and write JSON files

  • csv: read and write CSV files

  • sys: access information about the current process

  • os: interface with the operating system and file system paths

  • re: regular expressions patterns for text processing

The easy of access to all these modules with thousands of predefined functions is a big reason why Python is popular today. Python is often described as a language that comes with “batteries included” because of all the functionality available in the standard library.

Installing packages using pip#

In addition to the standard library, the Python ecosystem includes a vast ecosystem of third-party modules, which you can install using the pip command-line tool. Normally, using pip requires some familiarity with command-line interface tools, but when working in a JupyterLab environment, you can use the magic command %pip right within a notebook. To install Python package named <pkgname>, run the command %pip install <pkname> in a notebook code cell. For example, here is the %pip command for installing the sympy package.

%pip install sympy
Requirement already satisfied: sympy in /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages (1.12.1)
Requirement already satisfied: mpmath<1.4.0,>=1.1.0 in /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages (from sympy) (1.3.0)
[notice] A new release of pip is available: 23.0.1 -> 24.0
[notice] To update, run: pip install --upgrade pip
Note: you may need to restart the kernel to use updated packages.

In this case, the message “Requirement already satisfied: sympy” tells us that the sympy package is already installed, so there is nothing to do. If the sympy was not present, however, the %pip command search for the sympy package published on the Python Package Index website (see https://pypi.org/project/sympy/), download the latest version, and install it so that it is available for use (e.g. import sympy etc.).

The breadth and depth of the modules in the Python ecosystem is staggering. Whether you’re an astrophysicist analyzing data from deep space, a neuroscientist who is trying to decode neural activity, or an office worker trying to process some obscure file format, there is a good chance that someone has come before you and written code you can reuse for your task. A detailed discussion of the Python third-party modules ecosystem is outside the score of this introductory tutorial, but I’d like to mention a few of the all-star, heavy-hitter libraries for scientific computing and data science.

Scientific computing libraries#

Python was adopted by the scientific community very early on, because it is an easy language to learn. Scientific computing usually involves large-scale numerical calculations on vectors and matrices, which led to the development of the following packages for high-performance computing:

  • Numerical Python (numpy) is a library that provides array and matrix objects that make mathematical operations run very fast.

  • Scientific Python (scipy) is a library provides many functions used by scientists and engineers. The module scipy.stats contains all the probability models we use in statistics.

  • Symbolic Python (sympy) is a library for symbolic math calculations. Using SymPy you can work with math expressions just like when solving math problems using pen and paper. See the sympy_tutorial.pdf to learn more about SymPy.

Data science libraries#

Another community where Python is very popular is among statisticians and data analysts, which is thanks to the following libraries for data handling and visualization:

  • Pandas (pandas) is a library for tabular data management. Pandas is the Swiss army knife of data manipulations, and provides functions for loading data from various formats, and doing data processing, data cleaning, and calculating descriptive statistics. See the pandas_tutorial.ipynb notebook to learn more about Pandas.

  • Seaborn (seaborn) is a high-level library for statistical plots. Seaborn allows you to generate beautiful data visualizations like strip plots, scatter plots, histograms, bar plots, box plots, etc. See the seaborn_tutorial.ipynb notebook to learn more about Seaborn.

  • Statsmodels (statsmodels) is a statistical modelling library that includes advanced statistical models and functions for performing statistical tests.

TODO: closing sentence

Getting comfortable with Python#

JupyterLab provides lots of tools for making learning Python easy for beginners, including documentation (help menus) and interactive ways to explore variable properties.

Showing the help info#

Every Python object has a “docstring” (documentation string) associated with it, which provides the help info about this object. There are three equivalent ways to view the docstring of any Python object obj (value, variable, function, module, etc.):

  • help(obj): shows the docstring for the object obj

  • obj?: shortcut for help(obj)

  • press SHIFT+TAB: while the cursor is on top of a variable or function inside a code cell.

There are also other methods for getting more detailed info about an object like obj??, %psource obj, %pdef obj, but we won’t need these for now.

Example 8: learning about the print function#

Let’s say want to learn more about the print function. Put the cursor in the middle of function and press SHIFT+TAB.

print
<function print>

You know this function accepts one or more arguments and displays them, but the help menu shows complete information about other keywords arguments (options) you can use when calling this function. You can also use the help function to see the same information.

help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

The ... in the doc string of the print function tells us it accepts multiple values. The keyword argument sep (separator) dictates the text that is inserted between values when they are printed, The keyword end controls what is printed at the end of the line (\n means newline). Note we could have obtained the doc string using print? or using print(print.__doc__).

Exercise 12: getting help#

Display the doc-string of the function len.

len
<function len(obj, /)>

Click for solution.

Inspecting Python objects#

Suppose you have an unknown Python object obj. How can you find out what it is and learn what you can do with it?

Displaying the object#

There are several ways to display information about the object obj.

  • print(obj): converts the object to str and prints it

  • type(obj): tells you what type of object it is

  • repr(obj): similar to print, but shows the complete text representation (including quotes). The output of repr(obj) usually contains all the information needed to reconstruct the object obj.

Auto-complete object’s methods#

JupyterLab code cells provide an “autocomplete” feature that allows us to interactively exploring what methods are available on a given object. To use the autocomplete functionality, start by writing the name of the object followed by a dot obj., then press TAB button. You’ll be presented with a list of all the object’s methods. Try this out for yourself.

message = "Hello everyone"
# message.    # place cursor after the dot and press the TAB button

This autocomplete feature is used very often by programmers to find the names of the methods they need. Examples of useful methods on the message object include message.upper(), message.lower(), message.split(), message.replace(...), etc.

Python error messages help you fix mistakes#

Sometimes your code will cause an error and Python will display an error message describing the problem it encountered. You need to be mentally prepared for these errors, since they happen a lot and can be very discouraging to see. Examples of errors include SyntaxError, NameError, TypeError, etc. Error messages look scary, but really they are there to help you—if you read what the error message is telling you, you’ll know exactly what you need to fix in your code. The error message literally describes the problem!

Let’s look at an example expression that causes a Python exception. Suppose you’re trying to compute the difference 5-3, but you forgot the minus sign.

5 3
  Cell In[84], line 1
    5 3
      ^
SyntaxError: invalid syntax

The code cell above shows an example of a SyntaxError that occurs because Python expects some operator or separator between the two numbers. Python doesn’t know what to do when you just put two numbers side-by-side, and it doesn’t want to guess what your intention was (53? 5.3?).

You’ll see these a threatening looking red message like this any time Python encounters an error while trying to run the commands you specified. We say the code “raises” or “encountered” an exception. This is nothing to be alarmed by. It usually means you made a typo, forgot a required syntax element, or tried to compute do something impossible. The way to read these red messages is to focus on the explanation message that gets printed on the last line. The error message usually tells you what you need to fix. The solution will be obvious when you have made a typo or a syntax error, but in more complicated situations, you might need to search for the error message online to find what the problem is. In the above example, the fix is simple: we just need to add the missing minus sign: 5-3.

The three most common Python error messages are:

  • SyntaxError: you typed in something wrong (usually missing ,, ", or ] or some other punctuation).

  • NameError: raised when a variable is not found in the current context.

  • TypeError: raised when a function or operation is applied to an object of incorrect type.

Later on you might run into: KeyError when a key is not found in a dictionary, ValueError when a function gets an argument of correct type but improper value, ImportError when importing modules, and AttributeError when trying to access an object attribute that doesn’t exist.

Exercise 13: let’s break Python!#

Try typing in Python commands that causes each of these exceptions:

  1. SyntaxError (hint: write an incomplete list expression)

  2. NameError (hint: try referring to a variable that doesn’t exist)

  3. TypeError (hint: try to sum something that is not a list)

Click for solution.

Python documentation is very good#

The official Python documentation website is https://docs.python.org. This website provides tons of excellent learning resources and reference information for learning Python. Here are some useful links to essential topics for Python beginners:

I encourage you to browse the site to familiarize yourself with the high quality information that is available in the official docs pages.

When searching online, the official Python docs might now show up on the first page of results. Because of Python’s popularity, there are hundreds of spammy websites that provide inferior information, wrapped in tons of advertisements and popups. These websites that are much inferior to the official documentation. When learning something new, you should always prefer the official docs, even if they don’t appear first in the list of results on the page. Stack overflow discussions can be a good place to find answers to common Python questions. ChatGPT is also pretty good with Python code, so you can ask it to give you code examples or provide feedback on code you’ve written.

Final review#

Let’s do a quick review of all the Python concepts we introduced in this tutorial.

Python grammar and syntax review#

Learning Python is like learning a new language:

  • nouns: variables and values of different types

  • verbs: functions and methods, including basic operators like +, -, etc.

  • grammar: rules about how to use nouns and verbs together

Python keywords#

Here is a complete list of keywords in the Python language:

False      class      finally    is         return
None       continue   for        lambda     try
True       def        from       nonlocal   while
and        del        global     not        with
as         elif       if         or         yield
assert     else       import     pass
break      except     in         raise

As you can see from the above list, there are some keywords that we didn’t cover in this tutorial, because they are not essential to know at first. Here is the shortlist of the keywords you need to remember:

  • def used to define a new function and the return statement that defines the output of the function

  • in to check if element is part of container

  • for used in for-loops and list-comprehension syntax

  • the boolean values True and False

  • or, and, and not to create boolean expressions

  • if, elif, else used in conditional statements

  • None is a special keyword that represents the absence of a value

  • import ... and from ... import ... statements to import Python modules

Python data types#

Here is a reminder of the Python built-in data types:

  • int: integers

  • float: decimals

  • list: list of objects [obj1, obj2, obj3, ...]

  • bool: boolean values (either True or False)

  • str: text strings

  • dict: associative array between keys and values

  • tuple: similar to a list, but cannot be modified

Python functions#

Here are the essential functions you need to remember:

  • print(arg1,arg2, ...): print str(arg1), str(arg2), ... to the screen

  • type(obj): tells you what kind of object

  • len(obj): length of the object (only for: str, list, dict objs)

  • range(a,b): creates a list of numbers [a,a+1,...,b-1]

  • help(obj): display info about the object, function, or method

Here is another set of build-in functions used for lists, and list-like objects:

  • len(mylist): length of the list mylist

  • sum(mylist): sum of the values in the list of numbers mylist

  • all(mylist): True if all values in the list mylist are True

  • any(mylist): True if any of the values in the list mylist are True

  • enumerate(mylist): convert list of values to mylist to list of tuples (i,mylist[i]). Used to know the index in for-loop, e.g. for i, item in enumerate(items): ....

  • zip(list1,list2): joint iteration over two lists

  • Low-level iterator functions: iter() and next()

The function for input/output (I/O) operations are:

  • input: prompt the user for text input

  • print(arg1,arg2, ...): print str(arg1), str(arg2), ... to the screen or file

  • open(filepath,mode): open the file at filepath for reading or writing. Use mode="r" for reading or mode="w" for writing.

Here are some functions you can use when “looking around” to find more information about objects:

  • str(obj): display the string representation of the object.

  • repr(obj): display the Python representation of the object. Usually, you can copy-paste the output of repr(obj) into a Python shell to re-create the object.

  • help(obj): display info about the object, function, or method. This is equivalent to printing object’s docstring obj.__doc__.

  • dir(obj): show complete list of attributes and methods of the object obj

  • globals(): display all variables in the Python global namespace

  • locals(): display local variables (within current scope)

Advanced functions:

  • Functional stuff: map(), eval(), exec()

  • Meta-programming: hasattr(), getattr(), setattr()

  • Object oriented programming: isinstance(), issubclass(), super()

Python punctuation#

The most difficult part of learning Python is the use of non-word punctuation characters, which have very specific meaning and take some time to get used to. Let’s review and summarize how the symbols =([{*"'#,.: are used in Python.

  • The hash symbol # is used for comments.


  • Asterisk * is the multiplication operator, while the double asterisk ** is the exponent operator.


  • Equal sign = is used for:

    • assignment statements: x = 5

    • pass keyword arguments to functions: print("ans",3,sep=":")

    • (advanced) specify default keyword argument in function definitions


  • The comparison operators <, >=, <=, == (equal to), and != (not equal to) are used compare relative size of numerical values: x > 2.


  • Parentheses () are used for:

    • function definitions: def fun(x): ...)

    • calling functions: print(3)

    • enforcing order of operations: (x + y) * z

    • defining tuples: (1, 2, 3)


  • Comma , is used as:

    • element separator in lists, tuples, sets, etc.

    • separator between key:value pairs when defining a dictionary

    • separate function arguments in function definitions

    • separate function arguments when calling functions


  • Curly-brackets (accolades) {} are used to

    • define dictionaries: mydict = {"key":"value", "k2":"v2"}

    • define sets: {1,2,3}


  • Square brackets [] are used for:

    • defining lists: mylist = [1, 2, 3]

    • accessing list elements: mylist[2]

    • list slicing: mylist[0:2] = first two items in mylist

    • accessing dictionary values by key: mydict["key"]


  • Quotes " or ' are used to define string literals. There are several variations you can use:

    • raw strings r"..." are be used for reduce escape characters

    • f-strings f"..." are used to include variables

    • triple quotes """ or ''' are used to define entire paragraphs


  • Colon : is used for:

    • starting an indented code block in statements like if, elif, else, for, etc.

    • key: value separator in dict literals

    • slice notation: 0:2 == slice(0,2) == [0,1]


  • Period . is used as:

    • decimal separator for floating point literals: 4.2

    • access object attributes and methods: msg.upper()

    • access names within a namespace: statistics.mean


  • Semicolon ; can be used to put multiple commands on single line.
    We sometimes use ; at the end of a line to suppress the output of a command in a notebook environment.

Applications#

I want to tell you about some of the cool Python applications you can look forward to if you choose to develop your Python skills further. Python is not just a calculator, but a general-purpose programming language, so it can be used for many applications. We already mentioned Python uses in scientific computing and data science. Here are some other areas where Python programming is popular.

  • Command line scripts: you can put Python commands into a script, then run them on the command line (terminal in UNIX or cmd.exe in Windows). For example, you can write a simple script that downloads music videos from YouTube and saves them as .mp3 files you can listen to offline.

  • Graphical user interface (GUI) programs: many desktop applications are written in Python. An example of a graphical, point-and-click application written in Python is Calibre, which is a powerful eBook management library, eBook reader, and eBook converter that supports every imaginable eBook format.

  • Web applications: the Django and Flask frameworks are often used to build web applications. Many of the websites you access every day have as server component written in Python.

  • Machine learning: most of the current machine learning (a.k.a. artificial intelligence) research and development is done using Python code.

  • Glue code: whenever you have some process that needs to take data from one place and bring it into another program, Python is a good choice to automate this process.

  • Cloud automation: you can use Python scripts to automate the entire IT infrastructure of a company.

I mention these examples so you’ll know the other possibilities enabled by Python, beyond the basic “use Python interactively like a calculator” code examples that we saw in this tutorial. We’re at the end of this tutorial, but just the beginning of your journey to discover all the interesting things you can do with Python.