Week 12: Optional Python revision session 2


This revision session is completely optional and is intended for students who would like to consolidate their understanding of the Python material covered in Weeks 8-12. The session takes the place of my usual office hour in Week 12. If you would like to book an office hour appointment with me at a different time this week, please email me.

In this revision session, I will summarize the Python material, and work through the additional exercises shown below. There will also be an opportunity for you to ask questions.

Week 6

In the lab, we looked at how to import, and use functions from, Python modules, how to generate random numbers in Python, and how to use familiar mathematical functions and constants in Python. Important concepts included how to generate random numbers uniformly from a given continuous or discrete range, how to store results of random experiments in a list, and the inexact nature of arithmetic using floats.

Additional exercises.
8.1  Identify and correct all syntax errors in the following code.

for i in range(10)
    Print("The next number is, i)

8.2  Identify the type of any bugs in the following code, and correct them.

a, b = 2, 3
print("The mean of a and b is" A+B/2)

8.3  Using random.randrange(), write a Python program that prints 10 random numbers drawn uniformly from the set $\{ 1, 100, 10000, 1000000 \}$.

Week 9

In the lab, we looked at how to use the string format method to tailor outputs, how to copy lists and strings, and how to define and use anonymous functions in Python. Important concepts included width and decimal space specification using format(), the use of list(), and the keyword lambda.

Additional exercises.
9.1  Write a Python program to print $\sqrt{2}$ to an increasing odd number of decimal places from 1 to 9, on consecutive lines, with each number aligned centrally.

9.2  Write a Python function that takes as arguments an arithmetic function f and a list of floats l, and returns l unchanged along with a new list that is the elementwise operaton of f on l.

9.3  Using an anonymous function, test your Python function from additional exercise 9.2 by squaring the list [1.0, 2.0, 3.0, 4.0].

Week 10

In the lab, we looked at how to calculate integrals numerically using different approximations, how to use local variables in functions, and how to define recursive functions. Important concepts included passing functions as arguments to other functions, the shorthand notation += and *=, the use of return statements in functions, and the use of recursive functions in calculating factorials.

Additional exercises.
10.1  What will the output of the following be? Type it in and check.

x = 1
def fun1():
    x = 2
    def fun2():

Will the output change if lines 4-6 are unindented? How?

10.2  Write a recursive Python function to calculate the $n$th Fibonacci number. Use your function to calculate the tenth Fibonacci number.

Week 11

In the lab, we looked at how to use the Matplotlib module to plot graphs, and how to create and edit Numpy arrays. Important concepts included the Matplotlib functions plot(), show(), set_aspect(), and the Numpy functions array() and linspace().

Additional exercises.
11.1  Using Matplotlib and Numpy arrays, write a program to plot, in green, the graph of $\tan^{-1}(x)$ for $x \in [-10, 10]$. Remember to label your axes.

Week 12

In the lab, we looked at how to draw and save parametrically defined curves and scatter plots, and how to define graphical elements using functions. Important concepts included the Matplotlib functions axis(), savefig(), and scatter(), and use of for loops to vary parameters when plotting.

Additional exercises.
12.1  Write a Python program that plots 100 points $(x, y)$ drawn uniformly at random from $[0,1] \times [0,1]$ and plots these points, colouring them green if they lie inside the unit circle $x^{2} + y^{2} < 1$ and red otherwise, and prints the proportion of points that are green. What mathematical constant does this proportion approximate?