Python for Business Analytics

Python Essentials


This notebook is a concise introduction to the very basics of the Python language that we will use.

Content:

Getting started

To get started, you can use your Notebook as a calculator. For example:

In [1]:
2 + 3
Out[1]:
5
In [2]:
10/4
Out[2]:
2.5

The following statement assigns a value to the variable x. Because the variable does not yet exist, the assignment statements creates the variable.

In [3]:
x = 5

The print function allows us to display information.

In [4]:
print('For truth is always strange; stranger than fiction.') # Lord Byron (the # starts a comment)
For truth is always strange; stranger than fiction.

We can also use the print function to output values.

In [5]:
x = 10
print(x)
10

Modules

The Python language by design has a small core. Most of the fuctionality that we need is in modules or packages that we need to explicity load into our session. There are two ways to do this: either by loading the entire package (or a subset of it) or a specific function.

In [6]:
import numpy as np # the np is an alias
np.sqrt(4)
Out[6]:
2.0
In [7]:
from numpy import sqrt
sqrt(4)
Out[7]:
2.0

Boolean and numeric variables

The most basic data type is a Boolean variable, which can be either True or False.

In [8]:
x = False
print(x)
False
In [9]:
x = 2 > 0
print(x)
True
In [10]:
type(x)
Out[10]:
bool

In expressions involving numbers, a False is automatically converted to zero and a True is converted to one.

In [11]:
y = 2*x
print(y)
2

The basic numerical data types are integers and floats.

In [12]:
x = 2
type(x)
Out[12]:
int
In [13]:
x = 2.0
type(x)
Out[13]:
float

Strings

String variables represent text data.

In [14]:
sentence = 'For truth is always strange; stranger than fiction.' 
type(sentence)
Out[14]:
str

We now take strings as an example to explore some of the basic functionality available in Python.

In [15]:
sentence.lower()
Out[15]:
'for truth is always strange; stranger than fiction.'

Above, lower is a method available for the string object. A method is a function that operates on the associated object.

In [16]:
'truth' in sentence # checks whether the substring 'truth' appears in the sentence
Out[16]:
True
In [17]:
sentence = 'It is sunny today'
print(sentence)

new_sentence = sentence.replace('sunny', 'raining')
print(new_sentence)
It is sunny today
It is raining today
In [18]:
len(sentence) # how many characters in the string variable?
Out[18]:
17
In [19]:
sentence[:11] # selects the first 11 characters of the string
Out[19]:
'It is sunny'

Data structures

Lists

A list is a sequence of values. The individual elements or items of a list can be of any type (including a list!).

In [20]:
x =[1, 'Science', True]
type(x)
Out[20]:
list
In [21]:
print(x)
[1, 'Science', True]
In [22]:
felines = []
print(felines)
[]
In [23]:
felines.append('lion')
felines.append('leopard')
print(felines)
['lion', 'leopard']
In [24]:
big_five = felines + ['elephant','rhino', 'buffalo']
print(big_five)
['lion', 'leopard', 'elephant', 'rhino', 'buffalo']
In [25]:
marine = ['shark', 'salmon', 'dolphin']
In [26]:
animals = [big_five, marine]
print(animals)
[['lion', 'leopard', 'elephant', 'rhino', 'buffalo'], ['shark', 'salmon', 'dolphin']]

We can access the elements of the list by indexing and slicing. Python uses zero-based indexing, such that the first element is at index zero.

In [27]:
big_five[0] # first element
Out[27]:
'lion'
In [28]:
big_five[3] # fourth element
Out[28]:
'rhino'
In [29]:
big_five[-1] # last element
Out[29]:
'buffalo'
In [30]:
animals[1] # the second element of animals is a list
Out[30]:
['shark', 'salmon', 'dolphin']
In [31]:
animals[1][1] # get the second of element of animals, then the second element of the result
Out[31]:
'salmon'
In [32]:
big_five[:2] # first two elements
Out[32]:
['lion', 'leopard']

Note that when slicing, the element at the endpoint is not included, which may initially feel counterintuitive if you are not used to zero based indexing.

In [33]:
big_five[1:2]
Out[33]:
['leopard']
In [34]:
big_five[-2:] # last two elements
Out[34]:
['rhino', 'buffalo']

We can delete elements from a list in different ways.

In [35]:
big_five.remove('buffalo')
big_five
Out[35]:
['lion', 'leopard', 'elephant', 'rhino']
In [36]:
del(big_five[-1])
big_five
Out[36]:
['lion', 'leopard', 'elephant']

Dictionaries

A dictionary is like a list, but instead of being indexed by integers, the indices can be of almost any type. The indices of a dictionary are called keys. More formally, a dictionary is a mapping from keys to values.

In [37]:
# 2016 populations
population = {'Sydney' : 5005358, 'Melbourne' : 4641636} # Here, Sydney and Melbourne are the keys
type(population)
Out[37]:
dict
In [38]:
population['Sydney']
Out[38]:
5005358
In [39]:
population['Melbourne']
Out[39]:
4641636
In [40]:
population['Brisbane'] = 2349699
print(population)
{'Sydney': 5005358, 'Melbourne': 4641636, 'Brisbane': 2349699}

Iteration

Iteration allows us to run a block of code repeatedly.

In [41]:
for animal in big_five:
    print(animal)
lion
leopard
elephant
In [42]:
for city in population:
    print(city)
Sydney
Melbourne
Brisbane
In [43]:
for city in population:
    print(population[city])
5005358
4641636
2349699
In [44]:
for i in range(5):
    print(i)
0
1
2
3
4

Here is a more complex example.

In [46]:
numbers = np.arange(0,12,2)
print(numbers)
[ 0  2  4  6  8 10]
In [47]:
total = 0
for number in numbers:
    print(number)
    total += number  # sums the number to the total, equivalent to total = total + number

print('Total = {0}'.format(total))    
0
2
4
6
8
10
Total = 30

Defining functions

Defining functions makes the coding process more efficient, prevents errors, and improves readability.

In [48]:
import math
In [49]:
def area(radius):
    return math.pi*radius**2 # the double asterisk is a power

area(1)
Out[49]:
3.141592653589793
In [50]:
area(2)
Out[50]:
12.566370614359172