MATH0011: Mathematical Methods II
Code we wrote during lecture 3
python
# Import an entire moduleimport math
print(math.log(2))
# Import a function from a module
from math import log
print(log(2))
# Using as to rename imports
import math as maths
print(maths.log(2))
from math import log as ln
print(ln(2))
#
# The random module
#
from random import random
print(random())
import random
a = [2,3,8,10]
print(random.choice(a))
print(a)
random.shuffle(a)
print(a)
print(random.randrange(0,10))
print(random.gauss(0, 1))
# Simulate rolling two (fair) dice
import random
first = random.choice([1,2,3,4,5,6])
second = random.randrange(1,7)
print(first, second)
# Simultate 1000 rolls of two dice and estimate probability that the sum is 7
import random
count = 0
for i in range(1000):
first = random.choice([1,2,3,4,5,6])
second = random.randrange(1,7)
if first+second == 7:
count += 1
print(count/1000)
#
# The itertools module
#
import itertools
for i in itertools.combinations(range(5), 3):
print(i)
for i,j,k in itertools.combinations(range(5), 3):
print(i,j,k)
for i,j,k in itertools.combinations_with_replacement(range(5), 3):
print(i,j,k)
from itertools import combinations_with_replacement as cwr
for i in cwr(range(5), 3):
print(i)
for i in itertools.permutations(range(5), 3):
print(i)
for i in itertools.permutations(range(5)):
print(i)
for i,j in itertools.product([0,4,10,3], "Hello"):
print(i,j)
for i in itertools.product([0,4,10,3], "Hello", "def"):
print(i)
for i in itertools.repeat(5, 10):
print(i)
for i in itertools.repeat("Hello"):
print(i)
# Calculate the probability that two (fair) dice will add up to 7
import itertools
sevens = 0
total = 0
for first,second in itertools.product(range(1,7), range(1,7)):
total += 1
if first+second == 7:
sevens += 1
print(sevens/total)
# Print the current date
# We used Google to find this
import datetime
now = datetime.datetime.now()
print(now.year)
# We found this better version on Google
import datetime
print(datetime.datetime.today().strftime('%d %b %Y'))
The next piece of code uses the listmaker module that we wrote during the lecture.
python
# Using the listmaker module (that we wrote)# See listmaker.py
import listmaker
print(listmaker.squares(1000))
print(listmaker.primes(100))
# print the number between 1 and 100 that cannot be written as the sum
# of a square and a prime
import listmaker
squares = listmaker.squares(100)
primes = listmaker.primes(100)
possible = {}
for i in range(1,100):
possible[i] = False
for i in squares:
for j in primes:
if i+j < 100:
possible[i+j] = True
# The following lines do the same thing as above in a different way
#from itertools import product
#for i,j in product(squares,primes):
# if i+j < 100:
# possible[i+j] = True
for i,j in possible.items():
if not j:
print(i)
# Using the Fraction class from the fractions module
from fractions import Fraction
half = Fraction(1, 2)
third = Fraction(1, 3)
print(half**2)
# Handling exceptions
a = 2
data = []
for b in range(-5,6):
try:
print(a/b)
except ZeroDivisionError:
print("Oops, you divided by zero")
a = 2
data = []
for b in range(-5,6):
try:
print(a/b)
print(data[2])
except ZeroDivisionError:
print("Oops, you divided by zero")
except IndexError:
print("There was an index error")
# Warning: except without a type can lead to infinite loops that are impossible to end
while True:
try:
print("Hello")
except KeyboardInterrupt:
print("Goodbye")
break
except:
pass