4.1. Basics#

4.1.1. First Steps#

We are ready to begin writing code!

In this section, we will teach you some basic concepts of programming and where to search for help.

4.1.1.1. Variable Assignment#

The first thing we will learn is the idea of variable assignment.

Variable assignment associates a value to a variable.

Below, we assign the value “Hello World” to the variable x

x = "Hello World"

Once we have assigned a value to a variable, Python will remember this variable as long as the current session of Python is still running.

Notice how writing x into the prompt below outputs the value “Hello World”.

x
'Hello World'

However, Python returns an error if we ask it about variables that have not yet been created.

# uncomment (delete the # and the space) the line below and run
# y

It is also useful to understand the order in which operations happen.

First, the right side of the equal sign is computed.

Then, that computed value is stored as the variable to the left of the equal sign.

Keep in mind that the variable binds a name to something stored in memory.

The name can even be bound to a value of a completely different type.

x = 2
print(x)
x = "something else"
print(x)
2
something else

4.1.1.2. Code Comments#

Comments are short notes that you leave for yourself and for others who read your code.

They should be used to explain what the code does.

A comment is made with the #. Python ignores everything in a line that follows a #.

Let’s practice making some comments.

i = 1  # Assign the value 1 to variable i
j = 2  # Assign the value 2 to variable j

# We add i and j below this line
i + j
3

4.1.2. Functions#

Functions are processes that take an input (or inputs) and produce an output.

If we had a function called f that took two arguments x and y, we would write f(x, y) to use the function.

For example, the function print simply prints whatever it is given. Recall the variable we created called x.

print(x)
something else

4.1.2.1. Getting Help#

We can figure out what a function does by asking for help.

In Jupyter notebooks, this is done by placing a ? after the function name (without using parenthesis) and evaluating the cell.

For example, we can ask for help on the print function by writing print?.

Depending on how you launched Jupyter, this will either launch

  • JupyterLab: display the help in text below the cell.

  • Classic Jupyter Notebooks: display a new panel at the bottom of your screen. You can exit this panel by hitting the escape key or clicking the x at the top right of the panel.

# print? # remove the comment and <Shift-Enter>

JupyterLab also has a “Contextual Help” (previously called “Inspector”) window. To use,

  • Go to the Commands and choose Contextual Help (or Inspector), or select <Ctrl-I> (<Cmd-I> for OSX users).

  • Drag the new inspector pain to dock in the screen next to your code.

  • Then, type print or any other function into a cell and see the help.

# len # remove the comment and <Shift-Enter>

We will learn much more about functions, including how to write our own, in a future lecture.

4.1.3. Objects and Types#

Everything in Python is an object.

Objects are “things” that contain 1) data and 2) functions that can operate on the data.

Sometimes we refer to the functions inside an object as methods.

We can investigate what data is inside an object and which methods it supports by typing . after that particular variable, then hitting TAB.

You can scroll through this list by using the up and down arrows.

We often refer to this as “tab completion” or “introspection”.

Let’s do this together below. Keep going down until you find the method split.

# Type a period after `x` and then press TAB.
x
'something else'

Once you have found the method split, you can use the method by adding parenthesis after it.

Let’s call the split method, which doesn’t have any other required parameters. (Quiz: how would we check that?)

x.split()
['something', 'else']

We often want to identify what kind of object some value is– called its “type”.

A “type” is an abstraction which defines a set of behavior for any “instance” of that type i.e. 2.0 and 3.0 are instances of float, where float has a set of particular common behaviors.

In particular, the type determines:

  • the available data for any “instance” of the type (where each instance may have different values of the data).

  • the methods that can be applied on the object and its data.

We can figure this out by using the type function.

The type function takes a single argument and outputs the type of that argument.

type(3)
int
type("Hello World")
str
type([1, 2, 3])
list

We will learn more about each of these types (and others!) and how to use them soon, so stay tuned!

4.1.4. Modules#

Python takes a modular approach to tools.

By this we mean that sets of related tools are bundled together into packages. (You may also hear the term modules to describe the same thing.)

For example:

  • pandas is a package that implements the tools necessary to do scalable data analysis.

  • matplotlib is a package that implements visualization tools.

  • requests and urllib are packages that allow Python to interface with the internet.

As we move further into the class, being able to access these packages will become very important.

We can bring a package’s functionality into our current Python session by writing

import package

Once we have done this, any function or object from that package can be accessed by using package.name.

Here’s an example.

import sys   # for dealing with your computer's system
sys.version  # information about the Python version in use
'3.8.5 (default, Sep  3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)]'

4.1.4.1. Module Aliases#

Some packages have long names (see matplotlib, for example) which makes accessing the package functionality somewhat inconvenient.

To ease this burden, Python allows us to give aliases or “nicknames” to packages.

For example we can write:

import package as p

This statement allows us to access the packages functionality as p.function_name rather than package.function_name.

Some common aliases for packages are

  • import pandas as pd

  • import numpy as np

  • import matplotlib as mpl

  • import datetime as dt

While you can choose any name for an alias, we suggest that you stick to the common ones.

You will learn what these common ones are over time.

4.1.5. Good Code Habits#

A common saying in the software engineering world is:

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. Code for readability.

This might be a dramatic take, but the most important feature of your code after correctness is readability.

We encourage you to do everything in your power to make your code as readable as possible.

Here are some suggestions for how to do so:

  • Comment frequently. Leaving short notes not only will help others who use your code, but will also help you interpret your code after some time has passed.

  • Anytime you use a comma, place a space immediately afterwards.

  • Whitespace is your friend. Don’t write line after line of code – use blank lines to break it up.

  • Don’t let your lines run too long. Some people reading your code will be on a laptop, so you want to ensure that they don’t need to scroll horizontally and right to read your code. We recommend no more than 80 characters per line.

4.1.6. Numbers#

Python has two types of numbers.

  1. Integer (int): These can only take the values of the integers i.e. \( \{\dots, -2, -1, 0, 1, 2, \dots\} \)

  2. Floating Point Number (float): Think of these as any real number such as \( 1.0 \), \( 3.1415 \), or \( -100.022358923223 \)

The easiest way to differentiate these types of numbers is to find a decimal place after the number.

A float will have a decimal place, but an integer will not.

Below, we assign integers to the variables xi and zi and assign floating point numbers to the variables xf and zf.

xi = 1
xf = 1.0
zi = 123
zf = 1230.5  # Notice -- There are no commas!
zf2 = 1_230.5  # If needed, we use `_` to separate numbers for readability

4.1.6.1. Python as a Calculator#

You can use Python to perform mathematical calculations.

a = 4
b = 2

print("a + b is", a + b)
print("a - b is", a - b)
print("a * b is", a * b)
print("a / b is", a / b)
print("a ** b is", a**b)
print("a ^ b is", a^b)
a + b is 6
a - b is 2
a * b is 8
a / b is 2.0
a ** b is 16
a ^ b is 6

You likely could have guessed all except the last two.

Warning: Python uses **, not ^, for exponentiation (raising a number to a power)!

Notice also that above +, - and ** all returned an integer type, but / converted the result to a float.

When possible, operations between integers return an integer type.

All operations involving a float will result in a float.

a = 4
b = 2.0

print("a + b is", a + b)
print("a - b is", a - b)
print("a * b is", a * b)
print("a / b is", a / b)
print("a ** b is", a**b)
a + b is 6.0
a - b is 2.0
a * b is 8.0
a / b is 2.0
a ** b is 16.0

We can also chain together operations.

When doing this, Python follows the standard order of operations — parenthesis, exponents, multiplication and division, followed by addition and subtraction.

For example,

x = 2.0
y = 3.0
z1 = x + y * x
z2 = (x + y) * x

What do you think z1 is?

How about z2?

4.1.6.2. Other Math Functions#

We often want to use other math functions on our numbers. Let’s try to calculate sin(2.5).

#sin(2.5)

If you comment the code above you will see that Python complains that sin isn’t defined.

The problem here is that the sin function – as well as many other standard math functions – are contained in the math package.

We must begin by importing the math package.

import math

Now, we can use math.[TAB] to see what functions are available to us.

# uncomment, add a period (`.`) and pres TAB
# math
# found math.sin!
math.sin(2.5)
0.5984721441039564

4.1.7. Strings#

Textual information is stored in a data type called a string.

To denote that you would like something to be stored as a string, you place it inside of quotation marks.

For example,

"this is a string"  # Notice the quotation marks
'this is a string'  # Notice the quotation marks
this is not a string  # No quotation marks

You can use either " or ' to create a string. Just make sure that you start and end the string with the same one!

Notice that if we ask Python to tell us the type of a string, it abbreviates its answer to str.

type("this is a string")
str

4.1.7.1. String Operations#

Some of the arithmetic operators we saw in the numbers lecture also work on strings:

  • Put two strings together: x + y.

  • Repeat the string x a total of n times: n * x (or x * n).

x = "Hello"
y = "World"
x + y
'HelloWorld'
3 * x
'HelloHelloHello'

What happens if we try * with two strings, or - or /?

The best way to find out is to try it!

a = "1"
b = "2"
#a * b  # uncomment this to see what happens
#a - b  # uncomment this to see what happens

4.1.7.2. String Methods#

We can use many methods to manipulate strings.

We will not be able to cover all of them here, but let’s take a look at some of the most useful ones.

x
'Hello'
x.lower()  # Makes all letters lower case
'hello'
x.upper()  # Makes all letters upper case
'HELLO'
x.count("l")  # Counts number of a particular string
2
x.count("ll")
1

4.1.7.3. String Formatting#

Sometimes we’d like to reuse some portion of a string repeatedly, but still make some relatively small changes at each usage.

We can do this with string formatting, which done by using {} as a placeholder where we’d like to change the string, with a variable name or expression.

Let’s look at an example.

country = "Vietnam"
GDP = 223.9
year = 2017
my_string = f"{country} had ${GDP} billion GDP in {year}"
print(my_string)
Vietnam had $223.9 billion GDP in 2017

Rather than just substituting a variable name, you can use a calculation or expression.

print(f"{5}**2 = {5**2}")
5**2 = 25

Or, using our previous example

my_string = f"{country} had ${GDP * 1_000_000} GDP in {year}"
print(my_string)
Vietnam had $223900000.0 GDP in 2017

In these cases, the f in front of the string causes Python interpolate any valid expression within the {} braces.

Alternatively, to reuse a formatted string, you can call the format method (noting that you do not put f in front).

gdp_string = "{country} had ${GDP} billion in {year}"

gdp_string.format(country = "Vietnam", GDP = 223.9, year = 2017)
'Vietnam had $223.9 billion in 2017'

For more information on what you can do with string formatting (there is a lot that can be done…), see the official Python documentation on the subject.

4.1.8. Booleans#

A boolean is a type that denotes true or false.

As you will soon see in the control flow chapter, using boolean values allows you to perform or skip operations depending on whether or not a condition is met.

Let’s start by creating some booleans and looking at them.

x = True
y = False

type(x)
bool
x
True
y
False

4.1.8.1. Comparison Operators#

Rather than directly write True or False, you will usually create booleans by making a comparison.

For example, you might want to evaluate whether the price of a particular asset is greater than or less than some price.

For two variables x and y, we can do the following comparisons:

  • Greater than: x > y

  • Less than: x < y

  • Equal to: ==

  • Greater than or equal to: x >= y

  • Less than or equal to: x <= y

We demonstrate these below.

a = 4
b = 2

print("a > b", "is", a > b)
print("a < b", "is", a < b)
print("a == b", "is", a == b)
print("a >= b", "is", a >= b)
print("a <= b", "is", a <= b)
a > b is True
a < b is False
a == b is False
a >= b is True
a <= b is False

4.1.8.2. Negation#

Occasionally, determining whether a statement is “not true” or “not false” is more convenient than simply “true” or “false”.

This is known as negating a statement.

In Python, we can negate a boolean using the word not.

not False
True
not True
False

4.1.8.3. Multiple Comparisons (and/or)#

Sometimes we need to evaluate multiple comparisons at once.

This is done by using the words and and or.

However, these are the “mathematical” ands and ors – so they don’t carry the same meaning as you’d use them in colloquial English.

  • a and b is true only when both a and b are true.

  • a or b is true whenever at least one of a or b is true.

For example

  • The statement “I will accept the new job if the salary is higher and I receive more vacation days” means that you would only accept the new job if you both receive a higher salary and are given more vacation days.

  • The statement “I will accept the new job if the salary is higher or I receive more vacation days” means that you would accept the job if (1) they raised your salary, (2) you are given more vacation days, or (3) they raise your salary and give you more vacation days.

Let’s see some examples.

True and False
False
True and True
True
True or False
True
False or False
False
# Can chain multiple comparisons together.
True and (False or True)
True

4.1.8.4. all and any#

We have seen how we can use the words and and or to process two booleans at a time.

The functions all and any allow us to process an unlimited number of booleans at once.

all(bools) will return True if and only if all the booleans in bools is True and returns False otherwise.

any(bools) returns True whenever one or more of bools is True.

What do you expect to get with

all([True,True,False])

all([True,True,True])


any([True,True,False])

any([True,True,True])