5.
Python Essentials
.1. Overview
e have covered a lot of material quite quickly, with a focus on examples.
ow letʼs cover some core features of Python in a more systematic way.
his approach is less exciting but helps clear up some details.
.2. Data Types
omputer programs typically keep track of a range of data types.
or example, 1.5 is a floating point number, while 1 is an integer.
rograms need to distinguish between these two types for various reasons.
ne is that they are stored in memory di erently.
nother is that arithmetic operations are di erent
For example, floating point arithmetic is implemented on most machines by a specialized Floating Point Unit
(FPU).
general, floats are more informative but arithmetic operations on integers are faster and more accurate.
ython provides numerous other built-in Python data types, some of which weʼve already met
strings, lists, etc.
etʼs learn a bit more about them.
.2.1. Primitive Data Types
2.1.1. Boolean Values
ne simple data type is Boolean values, which can be either True or False
x = True
x
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
True
e can check the type of any object in memory using the type() function.
type(x)
bool
the next line of code, the interpreter evaluates the expression on the right of = and binds y to this value
y = 100 < 10
y
False
type(y)
bool
arithmetic expressions, True is converted to 1 and False is converted 0 .
his is called Boolean arithmetic and is o en useful in programming.
ere are some examples
x + y
x * y
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
0
True + True
bools = [True, True, False, True] # List of Boolean values
sum(bools)
2.1.2. Numeric Types
umeric types are also important primitive data types.
e have seen integer and float types before.
omplex numbers are another primitive data type in Python
x = complex(1, 2)
y = complex(2, 1)
print(x * y)
type(x)
5j
complex
.2.2. Containers
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
ython has several basic types for storing collections of (possibly heterogeneous) data.
eʼve already discussed lists.
related data type is tuples, which are “immutable” lists
x = ('a', 'b') # Parentheses instead of the square brackets
x = 'a', 'b' # Or no brackets --- the meaning is identical
x
('a', 'b')
type(x)
tuple
Python, an object is called immutable if, once created, the object cannot be changed.
onversely, an object is mutable if it can still be altered a er creation.
ython lists are mutable
x = [1, 2]
x[0] = 10
x
[10, 2]
ut tuples are not
x = (1, 2)
x[0] = 10
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[13], line 2
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
1 x = (1, 2)
----> 2 x[0] = 10
TypeError: 'tuple' object does not support item assignment
eʼll say more about the role of mutable and immutable data a bit later.
uples (and lists) can be “unpacked” as follows
integers = (10, 20, 30)
x, y, z = integers
x
10
20
ouʼve actually seen an example of this already.
uple unpacking is convenient and weʼll use it o en.
2.2.1. Slice Notation
o access multiple elements of a sequence (a list, a tuple or a string), you can use Pythonʼs slice notation.
or example,
a = ["a", "b", "c", "d", "e"]
a[1:]
['b', 'c', 'd', 'e']
a[1:3]
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
['b', 'c']
he general rule is that a[m:n] returns n - m elements, starting at a[m] .
egative numbers are also permissible
a[-2:] # Last two elements of the list
['d', 'e']
ou can also use the format [start:end:step] to specify the step
a[::2]
['a', 'c', 'e']
sing a negative step, you can return the sequence in a reversed order
a[-2::-1] # Walk backwards from the second last element to the first element
['d', 'c', 'b', 'a']
he same slice notation works on tuples and strings
s = 'foobar'
s[-3:] # Select the last three elements
'bar'
2.2.2. Sets and Dictionaries
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
wo other container types we should mention before moving on are sets and dictionaries.
ctionaries are much like lists, except that the items are named instead of numbered
d = {'name': 'Frodo', 'age': 33}
type(d)
dict
d['age']
33
he names 'name' and 'age' are called the keys.
he objects that the keys are mapped to ( 'Frodo' and 33 ) are called the values .
ets are unordered collections without duplicates, and set methods provide the usual set-theoretic operations
s1 = {'a', 'b'}
type(s1)
set
s2 = {'b', 'c'}
s1.issubset(s2)
False
s1.intersection(s2)
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
{'b'}
he set() function creates sets from sequences
s3 = set(('foo', 'bar', 'foo'))
s3
{'bar', 'foo'}
.3. Input and Output
etʼs briefly review reading and writing to text files, starting with writing
f = open('newfile.txt', 'w') # Open 'newfile.txt' for writing
f.write('Testing\n') # Here '\n' means new line
f.write('Testing again')
f.close()
ere
The built-in function open() creates a file object for writing to.
Both write() and close() are methods of file objects.
here is this file that weʼve created?
ecall that Python maintains a concept of the present working directory (pwd) that can be located from with Jupyte
IPython via
%pwd
'/__w/lecture-python-programming.myst/lecture-python-programming.myst/lectures'
a path is not specified, then this is where Python writes to.
e can also use Python to read the contents of newline.txt as follows
f = open('newfile.txt', 'r')
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
out = f.read()
out
'Testing\nTesting again'
print(out)
Testing
Testing again
fact, the recommended approach in modern Python is to use a with statement to ensure the files are properly
cquired and released.
ontaining the operations within the same block also improves the clarity of your code.
Note
This kind of block is formally referred to as a context.
etʼs try to convert the two examples above into a with statement.
e change the writing example first
with open('newfile.txt', 'w') as f:
f.write('Testing\n')
f.write('Testing again')
ote that we do not need to call the close() method since the with block will ensure the stream is closed at th
nd of the block.
ith slight modifications, we can also read files using with
with open('newfile.txt', 'r') as fo:
out = fo.read()
print(out)
Testing
Testing again
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
ow suppose that we want to read input from one file and write output to another. Hereʼs how we could accomplish
is task while correctly acquiring and returning resources to the operating system using with statements:
with open("newfile.txt", "r") as f:
file = f.readlines()
with open("output.txt", "w") as fo:
for i, line in enumerate(file):
fo.write(f'Line {i}: {line} \n')
he output file will be
with open('output.txt', 'r') as fo:
print(fo.read())
Line 0: Testing
Line 1: Testing again
e can simplify the example above by grouping the two with statements into one line
with open("newfile.txt", "r") as f, open("output2.txt", "w") as fo:
for i, line in enumerate(f):
fo.write(f'Line {i}: {line} \n')
he output file will be the same
with open('output2.txt', 'r') as fo:
print(fo.read())
Line 0: Testing
Line 1: Testing again
uppose we want to continue to write into the existing file instead of overwriting it.
e can switch the mode to a which stands for append mode
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
with open('output2.txt', 'a') as fo:
fo.write('\nThis is the end of the file')
with open('output2.txt', 'r') as fo:
print(fo.read())
Line 0: Testing
Line 1: Testing again
This is the end of the file
Note
Note that we only covered r , w , and a mode here, which are the most commonly used modes. Python provides
a variety of modes that you could experiment with.
.3.1. Paths
ote that if newfile.txt is not in the present working directory then this call to open() fails.
this case, you can shi the file to the pwd or specify the full path to the file
f = open('insert_full_path_to_file/newfile.txt', 'r')
.4. Iterating
ne of the most important tasks in computing is stepping through a sequence of data and performing a given action
ne of Pythonʼs strengths is its simple, flexible interface to this kind of iteration via the for loop.
.4.1. Looping over Different Objects
any Python objects are “iterable”, in the sense that they can be looped over.
o give an example, letʼs write the file us_cities.txt, which lists US cities and their population, to the present working
rectory.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
%%writefile us_cities.txt
new york: 8244910
los angeles: 3819702
chicago: 2707120
houston: 2145146
philadelphia: 1536471
phoenix: 1469471
san antonio: 1359758
san diego: 1326179
dallas: 1223229
Overwriting us_cities.txt
ere %%writefile is an IPython cell magic.
uppose that we want to make the information more readable, by capitalizing names and adding commas to mark
ousands.
he program below reads the data in and makes the conversion:
data_file = open('us_cities.txt', 'r')
for line in data_file:
city, population = line.split(':') # Tuple unpacking
city = city.title() # Capitalize city names
population = f'{int(population):,}' # Add commas to numbers
print(city.ljust(15) + population)
data_file.close()
New York 8,244,910
Los Angeles 3,819,702
Chicago 2,707,120
Houston 2,145,146
Philadelphia 1,536,471
Phoenix 1,469,471
San Antonio 1,359,758
San Diego 1,326,179
Dallas 1,223,229
ere format() is a string method used for inserting variables into strings.
he reformatting of each line is the result of three di erent string methods, the details of which can be le till later.
he interesting part of this program for us is line 2, which shows that
1. The file object data_file is iterable, in the sense that it can be placed to the right of in within a for loop
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
2. Iteration steps through each line in the file.
his leads to the clean, convenient syntax shown in our program.
any other kinds of objects are iterable, and weʼll discuss some of them later on.
.4.2. Looping without Indices
ne thing you might have noticed is that Python tends to favor looping without explicit indexing.
or example,
x_values = [1, 2, 3] # Some iterable x
for x in x_values:
print(x * x)
1
4
9
preferred to
for i in range(len(x_values)):
print(x_values[i] * x_values[i])
1
4
9
hen you compare these two alternatives, you can see why the first one is preferred.
ython provides some facilities to simplify looping without indices.
ne is zip() , which is used for stepping through pairs from two sequences.
or example, try running the following code
countries = ('Japan', 'Korea', 'China')
cities = ('Tokyo', 'Seoul', 'Beijing')
for country, city in zip(countries, cities):
print(f'The capital of {country} is {city}')
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
The capital of Japan is Tokyo
The capital of Korea is Seoul
The capital of China is Beijing
he zip() function is also useful for creating dictionaries — for example
names = ['Tom', 'John']
marks = ['E', 'F']
dict(zip(names, marks))
{'Tom': 'E', 'John': 'F'}
we actually need the index from a list, one option is to use enumerate() .
o understand what enumerate() does, consider the following example
letter_list = ['a', 'b', 'c']
for index, letter in enumerate(letter_list):
print(f"letter_list[{index}] = '{letter}'")
letter_list[0] = 'a'
letter_list[1] = 'b'
letter_list[2] = 'c'
.4.3. List Comprehensions
e can also simplify the code for generating the list of random draws considerably by using something called a list
mprehension.
st comprehensions are an elegant Python tool for creating lists.
onsider the following example, where the list comprehension is on the right-hand side of the second line
animals = ['dog', 'cat', 'bird']
plurals = [animal + 's' for animal in animals]
plurals
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
['dogs', 'cats', 'birds']
ereʼs another example
range(8)
range(0, 8)
doubles = [2 * x for x in range(8)]
doubles
[0, 2, 4, 6, 8, 10, 12, 14]
.5. Comparisons and Logical Operators
.5.1. Comparisons
any di erent kinds of expressions evaluate to one of the Boolean values (i.e., True or False ).
common type is comparisons, such as
x, y = 1, 2
x < y
True
x > y
False
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
ne of the nice features of Python is that we can chain inequalities
1 < 2 < 3
True
1 <= 2 <= 3
True
s we saw earlier, when testing for equality we use ==
x = 1 # Assignment
x == 2 # Comparison
False
or “not equal” use !=
1 != 2
True
ote that when testing conditions, we can use any valid Python expression
x = 'yes' if 42 else 'no'
x
'yes'
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
x = 'yes' if [] else 'no'
x
'no'
hatʼs going on here?
he rule is:
Expressions that evaluate to zero, empty sequences or containers (strings, lists, etc.) and None are all
equivalent to False .
for example, [] and () are equivalent to False in an if clause
All other values are equivalent to True .
for example, 42 is equivalent to True in an if clause
.5.2. Combining Expressions
e can combine expressions using and , or and not .
hese are the standard logical connectives (conjunction, disjunction and denial)
1 < 2 and 'f' in 'foo'
True
1 < 2 and 'g' in 'foo'
False
1 < 2 or 'g' in 'foo'
True
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
not True
False
not not True
True
emember
P and Q is True if both are True , else False
P or Q is False if both are False , else True
e can also use all() and any() to test a sequence of expressions
all([1 <= 2 <= 3, 5 <= 6 <= 7])
True
all([1 <= 2 <= 3, "a" in "letter"])
False
any([1 <= 2 <= 3, "a" in "letter"])
True
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Note
all() returns True when all boolean values/expressions in the sequence are True
any() returns True when any boolean values/expressions in the sequence are True
.6. Coding Style and Documentation
consistent coding style and the use of documentation can make the code easier to understand and maintain.
.6.1. Python Style Guidelines: PEP8
ou can find Python programming philosophy by typing import this at the prompt.
mong other things, Python strongly favors consistency in programming style.
eʼve all heard the saying about consistency and little minds.
programming, as in mathematics, the opposite is true
A mathematical paper where the symbols ∪ and ∩ were reversed would be very hard to read, even if the autho
told you so on the first page.
Python, the standard style is set out in PEP8.
Occasionally weʼll deviate from PEP8 in these lectures to better match mathematical notation)
.6.2. Docstrings
ython has a system for adding comments to modules, classes, functions, etc. called docstrings.
he nice thing about docstrings is that they are available at run-time.
y running this
def f(x):
"""
This function squares its argument
"""
return x**2
er running this code, the docstring is available
f?
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Type: function
String Form:<function f at 0x2223320>
File: /home/john/temp/temp.py
Definition: f(x)
Docstring: This function squares its argument
f??
Type: function
String Form:<function f at 0x2223320>
File: /home/john/temp/temp.py
Definition: f(x)
Source:
def f(x):
"""
This function squares its argument
"""
return x**2
ith one question mark we bring up the docstring, and with two we get the source code as well.
ou can find conventions for docstrings in PEP257.
.7. Exercises
olve the following exercises.
or some, the built-in function sum() comes in handy).
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Exercise 5.1
Part 1: Given two numeric lists or tuples x_vals and y_vals of equal length, compute their inner product
using zip() .
Part 2: In one line, count the number of even numbers in 0,…,99.
Part 3: Given pairs = ((2, 5), (4, 2), (9, 8), (12, 10)) , count the number of pairs (a, b)
such that both a and b are even.
Hint
x % 2 returns 0 if x is even, 1 otherwise.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Solution to Exercise 5.1
Part 1 Solution:
Hereʼs one possible solution
x_vals = [1, 2, 3]
y_vals = [1, 1, 1]
sum([x * y for x, y in zip(x_vals, y_vals)])
This also works
sum(x * y for x, y in zip(x_vals, y_vals))
Part 2 Solution:
One solution is
sum([x % 2 == 0 for x in range(100)])
50
This also works:
sum(x % 2 == 0 for x in range(100))
50
Some less natural alternatives that nonetheless help to illustrate the flexibility of list comprehensions are
len([x for x in range(100) if x % 2 == 0])
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
50
and
sum([1 for x in range(100) if x % 2 == 0])
50
Part 3 Solution:
Hereʼs one possibility
pairs = ((2, 5), (4, 2), (9, 8), (12, 10))
sum([x % 2 == 0 and y % 2 == 0 for x, y in pairs])
Exercise 5.2
Consider the polynomial
2 n i
p(x) = a 0 + a 1 x + a 2 x + ⋯ an x = ∑ ai x (5.1)
i=0
Write a function p such that p(x, coeff) that computes the value in (5.1) given a point x and a list of
coe icients coeff (a 1 , a 2 , ⋯ a n ).
Try to use enumerate() in your loop.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Solution to Exercise 5.2
Hereʼs a solution:
def p(x, coeff):
return sum(a * x**i for i, a in enumerate(coeff))
p(1, (2, 4))
Exercise 5.3
Write a function that takes a string as an argument and returns the number of capital letters in the string.
Hint
'foo'.upper() returns 'FOO' .
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Solution to Exercise 5.3
Hereʼs one solution:
def f(string):
count = 0
for letter in string:
if letter == letter.upper() and letter.isalpha():
count += 1
return count
f('The Rain in Spain')
An alternative, more pythonic solution:
def count_uppercase_chars(s):
return sum([c.isupper() for c in s])
count_uppercase_chars('The Rain in Spain')
Exercise 5.4
Write a function that takes two sequences seq_a and seq_b as arguments and returns True if every element
in seq_a is also an element of seq_b , else False .
By “sequence” we mean a list, a tuple or a string.
Do the exercise without using sets and set methods.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Solution to Exercise 5.4
Hereʼs a solution:
def f(seq_a, seq_b):
for a in seq_a:
if a not in seq_b:
return False
return True
# == test == #
print(f("ab", "cadb"))
print(f("ab", "cjdb"))
print(f([1, 2], [1, 2, 3]))
print(f([1, 2, 3], [1, 2]))
True
False
True
False
An alternative, more pythonic solution using all() :
def f(seq_a, seq_b):
return all([i in seq_b for i in seq_a])
# == test == #
print(f("ab", "cadb"))
print(f("ab", "cjdb"))
print(f([1, 2], [1, 2, 3]))
print(f([1, 2, 3], [1, 2]))
True
False
True
False
Of course, if we use the sets data type then the solution is easier
def f(seq_a, seq_b):
return set(seq_a).issubset(set(seq_b))
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Exercise 5.5
When we cover the numerical libraries, we will see they include many alternatives for interpolation and function
approximation.
Nevertheless, letʼs write our own function approximation routine as an exercise.
In particular, without using any imports, write a function linapprox that takes as arguments
A function f mapping some interval [a, b] into R.
Two scalars a and b providing the limits of this interval.
An integer n determining the number of grid points.
A number x satisfying a <= x <= b .
and returns the piecewise linear interpolation of f at x , based on n evenly spaced grid points a =
point[0] < point[1] < ... < point[n-1] = b .
Aim for clarity, not e iciency.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Solution to Exercise 5.5
Hereʼs a solution:
def linapprox(f, a, b, n, x):
"""
Evaluates the piecewise linear interpolant of f at x on the interval
[a, b], with n evenly spaced grid points.
Parameters
==========
f : function
The function to approximate
x, a, b : scalars (floats or integers)
Evaluation point and endpoints, with a <= x <= b
n : integer
Number of grid points
Returns
=======
A float. The interpolant evaluated at x
"""
length_of_interval = b - a
num_subintervals = n - 1
step = length_of_interval / num_subintervals
# === find first grid point larger than x === #
point = a
while point <= x:
point += step
# === x must lie between the gridpoints (point - step) and point === #
u, v = point - step, point
return f(u) + (x - u) * (f(v) - f(u)) / (v - u)
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Exercise 5.6
Using list comprehension syntax, we can simplify the loop in the following code.
import numpy as np
n = 100
ϵ_values = []
for i in range(n):
e = np.random.randn()
ϵ_values.append(e)
Solution to Exercise 5.6
Hereʼs one solution.
n = 100
ϵ_values = [np.random.randn() for i in range(n)]
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com