Previous Lecture Lecture 4 Next Lecture

Lecture 4, Thu 01/18

Math / Random Modules, Function Basics

Recorded Lecture: 1_18_24

Math and Random Modules

import math

print(math.ceil(3.1)) # 4
print(math.floor(3.9))# 3
print(math.factorial(4)) # 4*3*2*1 -> 24
print(math.pow(4, 2)) # 4 ** 2 -> 16
print(math.pi) # 3.141592653589793
print(math.sqrt(81)) # 9.0
import random
print(random.random()) # [0, 1)
print(random.randrange(2, 8)) # [2, 8)

print(random.randint(2, 4)) # [2, 4]
print(random.randrange(2, 5)) # same as random.randint(2,4)
import random

# Rolling one six-sided die
print(random.randrange(1, 7)) 

# Rolling two six-sided dice
print(random.randrange(1, 7) + random.randrange(1, 7))

Introduction to Functions

def double(n):
	''' Returns 2 times the parameter''' # Good to comment functions!
	print("In double function. n =", n)
	print(2 * n)
def double(n):
	'''Returns 2 times the parameter''' # Good to comment functions!
	print("In double function. n =", n)
	return 2 * n
print(double(10)) # -> print(20)
print(double(double(2)) # -> print(double(4)) -> print(8)
value = double(5) + double(6) # -> 10 + double(6) -> 10 + 12 -> 22
print(value)