0% found this document useful (0 votes)
8 views2 pages

Python Notes - Wk3

The document provides an overview of using the math and random modules in Python, including various functions such as calculating factorials and generating random numbers. It demonstrates different import methods for the math module and showcases random number generation techniques, including uniform distributions and random choices from a list. Additionally, it includes a custom factorial function and examples of using the random module's features.

Uploaded by

Kyle Rumble
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Python Notes - Wk3

The document provides an overview of using the math and random modules in Python, including various functions such as calculating factorials and generating random numbers. It demonstrates different import methods for the math module and showcases random number generation techniques, including uniform distributions and random choices from a list. Additionally, it includes a custom factorial function and examples of using the random module's features.

Uploaded by

Kyle Rumble
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

-----------------------------------------------------------------------------------

-------------------------------
import math # imports math module from anonaconda see general (recommended method)
dir(math) # shows all functions in math
print(math.pi) # shows the value of pi
round(math.pi, 2) # rounds the pi function to 2 decimal places
-----------------------------------------------------------------------------------
-------------------------------
import math as m # imports math module as m
m.pi # similar to math.pi
-----------------------------------------------------------------------------------
-------------------------------
from math import * # import all functions from math module
pi # does not need math as it has imported all functions, could lead to problems
-----------------------------------------------------------------------------------
-------------------------------
from math import pi # only imports pi function and nothing else
pi
-----------------------------------------------------------------------------------
-------------------------------
-----------------------------------------------------------------------------------
-------------------------------
import math
math.factorial(6) # 6*5*4*3*2*1
>> 720
math.factorial(0) # 1
>> 1
-----------------------------------------------------------------------------------
-------------------------------
# defines a function called "fac"
def fac(x: int)->int: # input integer, output (->) integer
'''to calculate factorial value of a given whole number''' #doc string
if x == 0:
return 1
else:
return x * fac(x-1)

x = 3, fac(3) output -> 3 * fac(2)


fac(2): input - 2, output -> 2 * fac(1) # -> output (2 * 2-1)
== 2 * fac(1)

help(fac)

help(math)
-----------------------------------------------------------------------------------
-------------------------------
-----------------------------------------------------------------------------------
-------------------------------
import random # import random module
dir(random) # shows built-in functions for random module

random.random() # generates a value between 0 and 1


>> 0.23596850404 # FLOAT

random.uniform(1,10) # generates a value between 1 and 10


>> 9.304i4440048i954 # FLOAT
round(random.unform(10000, 1000000), 2) # generates a value between 10,000 and
1,000,000 rounded to 2 decimal places

random.seed(1)
random.random

random.randint(1, 10) # generates a random whole number between 1 and 10


>> 2 # INT

random.randrange(1 , 5, 2) # generates a whole number between 1 and 5 increasing by


2 starting at 1
>> 1
>> 3

c = ['red', 'green', 'blue']


random.choice(c) # choose a random choice

>> 'red'
>> 'green'
>> 'blue'

random.choices(c, k=2) # pick 2 elements from list c *MUST use K*


>> 'red', 'red'

random.choices(c, k=20, weights=[10, 5, 1]) # total 16, 10 = red, 5 = green, 1 =


blue

random.sample(c, 2) # similar to choice but cannot change weights

random.shuffle(c) # changes order of list


res = random.shuffle(c)
>> None, ['r', 'g', 'b']

You might also like