Introduction to Python

Try me

Open In ColabBinder

Python is a high level programming language that is particularly suited for data science and operations research. Python has three main versions labelled 1, 2, and 3. Within each version, there are several minor releases, like 2.2 and 3.3. Therefore, there are many Python versions labelled 1.x, 2.x, 3.x, where the x is the minor version number. This course covers 3.x, but it is not difficult to change from one version to another. This notebook provides a quick introduction to Python in Notebooks. If you are not familiar with this environment, check the partner book on computer programming in this link

Your first code cell

Let us start easy with our first code block. In this cell, we will assign variables and use a function. We will use the Python function print() that prints a message in the output of the code cell. The message is passed as a string variable. Let us introduce briefly these two concepts in the context of Jupyter Notebooks.

Basic variables

Variables allow us to store a value and use it in our Notebook through a name. To assign value to a variable, we first type the name, then the equal sign (=) and then the value:

[1]:
x=1
y=0.4
msg='Hello world'

There are different types of variables. In the example above, x and y are two numeric variables. We will talk about numeric variables later in this Notebook. The variable msg is a string variable (text). You can either use quotes or double quotes to assign a string variable.

Basic functions

In Python, a function takes variables as arguments and return a value. The standard Python library provides many convenient functions. Let us start with a simple example.

[2]:
# This is your first Python cell
print(msg)
print('Hello again!')
Hello world
Hello again!

The first line in the example above is a comment. Comments help us humans read the code and start with a ‘#’. Therefore, the code cell above has only one comment and two lines of code. The first line of code calls the function print with the string variable msg. Note that we can use variables in separate code cells of a Notebook. Note also that variables are declared when we Run a code cell. Cells should be run in the order they are presented in a Notebook. The second line of code takes a string value as argument, because we do not want to store this value to use it later. You can play around with the print function to print a different message or several messages, just give it a try! Do not be afraid to change the values of variables. Variables are mutable, meaning that you can change the value of a variable in your code. But before you do, note that Python documentation normally includes references to the group comedy Monty Python and the words “spam” and “eggs” are normally used as fill-in variables. The example below reassigns the value of the variable msg to print the word “spam”. You can try to add a new line of code to print “spam” and “and eggs!” in two separate lines of the output in the next cell code.

[3]:
msg="spam"
print(msg) #Tip: I can also add a comment to a line of code
# Add a new line to print "spam" and "and eggs!" to the output of this code cell
spam

Numeric Variables

In Python, you can declare variables and do simple operations with them. We are going to do mathematical operations with variables, so you need to get familiar with the operands and the results, depending on the type of arguments used. Let us start with numerical variables. Python uses standard variable notations, so the operands +, -, *, / and ** represent sum, rest, multiply, divide and power operations. You can also use parenthesis to control the order of operations and declare negative or decimal variables using the - sign or a . (eg -4 or -4.5). In the following block of code you can find some examples.

[29]:
x = 2 #x is a numeric variable, equal to 2.
y = x+4 #y is a numeric variable, equal to x+4 = 6.
z = (x+3)*(y**2)/4 #z is a numeric variable equal to (2+3)*(6**2)/4 = 5
print(x)
print(y)
print(z)
2
6
45.0

Notice that z is different from x and y. While x and y are integers, z is a floating variable. One of the main characteristics of Python is that it is a weakly typed programming language. Roughly, this means that you can do operations with variables of different types and Python will make the operation to its best effort to avoid a compilation error. That is why Python generated a floating variable from the division operation. Also, as in math, the order of the operands is important, and we can use parenthesis to control it.

Errors

We cannot use any combination of variables. For instance, adding a numeric variable to a string could result in a compilation error. Another important piece of information is that in Jupyter, you can use variables declared in different code cells as long as you run them first. If you have run the previous cell, you can test the following cell.

[7]:
ms = 'x is ' + x
print(ms)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-3ada229f534a> in <module>()
----> 1 ms = 'x is ' + x
      2 print(ms)

TypeError: can only concatenate str (not "int") to str

Notice that the output of the cell now shows an error message. Do not panic. The message gives you information to interpret and correct the error. While you program, specially when you are a beginner, it is normal to get errors and very important to get familiar with the error messages. In this message, Python warns you of a TypeError and the reason why (you cannot concatenate an int to a string). There are other things that you are not allowed. For instance, you cannot divide by zero. The only person that can divide by zero is Chuck Norris. If you are not Chuck Norris, you will get a different error if you try the following cell:

[8]:
z = y/0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-8-d81c8fc02de0> in <module>()
----> 1 z = y/0

ZeroDivisionError: division by zero

In this case, the error message is different. In both cases, you get enough context information to identify the line of code that gave error and the reason why that line resulted in an error. ## Printing results to output Now some tips, if you want to print a msg with values, you can pass more variables to the print function as comma separated values. As we saw in the previous example, the print function can also take numeric values, so this is a way to get the expected result. We could also use the function str to convert a number to a string and use the + operator to concatenate the strings on a message. We can also use the format function to a string with two parameters to tell the function print how do you want to see them:

[14]:
print('x is ', x , ', y is ', y, ', z is ', z)
print('x is ' + str(x) + ', y is ' + str(y) + ', z is ' + str(z))
print('x is {x:d}, y is {y:d} and z is {z:.1f}'.format(x=x,y=y,z=z))
x is  2 , y is  6 , z is  5.0
x is 2, y is 6, z is 5.0
x is 2, y is 6 and z is 5.0

In Python, there are many ways to achieve the same result, so it is very important to read the docs and experiment to understand the different trade-offs!.

Lists

We will work quite a lot with lists in this course. Lists are used to store an indexed list of objects. To assign a list variable, we use brackets, surrounding the values of the objects we want to store. Indexed means that we can get the value of a specific item in the list using its position in the list. The index is specified again using brackets. Let us see a basic example:

[5]:
words = ["Hello!", "spam", "eggs"]
print(words[0])
print(words[1])
print(words[2])
Hello!
spam
eggs

Boolean variables

Another important type in Python is the Boolean type. Boolean variables can take two values, False or True.

[8]:
b = True

Boolean logic

Normally, in Python we used logical operands to do operations with boolean variables and build logic into our code. Basic boolean operators are logic and (and), logic or (or) and negate (not):

[15]:
c = not b
print(c)
print(c & b)
print(c and b)
print(c | b)
print(c or b)
False
False
False
True
True

Comparisons are also important operators that return boolean values. Comparators are less (<), less or equal (<=), exactly equal (==), greater or equal (>=), and greater (>). Normally, they take numerical values:

[21]:
x = 5
y = 6
z = 6
print(x < y)
print(y <= z)
print(y == z)
print(y >= z)
print(y > x)
True
True
True
True
True

The in operator is important in the context of lists. It allows us to check if any element of a list contains a specific value. It can be combined with the not operator if we want the opposite logic.

[25]:
print("tomato" in words)
print("spam" in words)
print("tomato" not in words)
False
True
True

Control Structures

Control structures allow us to implement complex logic into our programs, creating different execution pathways or repeating code blocks to make our code more efficient. The next subsections describe basic selection structures (if else clauses) and repetition structures (for loops) in Python.

If and else clauses

If clauses allow us to execute some code if a condition is true. This is the notation of the if clause:

if expression:
    sentence(s)

The sentences inside the if clause are only executed if the expression is true. On the other hand, else clauses execute sentences if the previous clause does not terminate normally. In general, we will use else clauses after if clauses to execute other clauses when the expression is not true. Therefore, an if clause combined with an else clause looks something like this:

if expression:
    sentence(s)
else:
    other_sentence(s)

Let us see an example:

[20]:
if b:
    print("b is:")
    print(c)
else:
    print("False!")

print("This message is always shown")
b is:
False
This message is always shown

Note that there is an indent (a space from the left margin) in the sentences inside the if and else clauses. This is very important because it is the way we have to tell Python which lines of codes are inside a clause and which are not.

For loops

For loops iterate over iterable variables like lists (we will see other iterable types later in the course). For loops allow us to declare a variable and use it inside it to control the sentences it contains. For loops are primarily used to write very efficient code with few lines. The structure of a for loop is

for var in vars:
    sentence(s)

The for loop declares a (var) variable that can only be used inside the sentences it contains. It will run the sentence(s) it contains as many times as elements are contained in the (vars) list used in the clause.

Take as example the code we used when we defined lists. We could rewrite it as:

[22]:
for word in words:
    print(word)
Hello!
spam
eggs

List comprehension

One of the main features of Python lists we will use in the course is list comprehension. List comprehension allows us to assign list variables very efficiently using for loops inside a bracket. Let us see how it works by example:

[32]:
base = [0, 1, 2, 3]
squares = [i**2 for i in base]
print(squares)
variable_names = ['x_'+str(i) for i in base]
print(variable_names)
[0, 1, 4, 9]
['x_0', 'x_1', 'x_2', 'x_3']

Libraries

In Python, we can import libraries using the keyword import. Libraries encapsulate functions and declare variables that provide additional functionalities and type declarations to provide a specific functionality. In next tutorials, we are going to introduce two libraries, numpy and matplotlib, but before that, you need to know how to import a library. Imports work like this:

import library as lib

in the code snippet above, we use the keyword import to ask python to import the library and use the alias lib. Later on our code, we will be able to use the functions in the library using the alias. Python and Conda allow you to import many useful libraries through import commands, but not every available library. For some libraries, first we will need to install the library in our environment, normally using Conda (but we could also use other tools like pip). This is however everything you need to know for now. You can now check the following tutorials to learn more about numpy and matplotlib.