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

Using Python's Math Module Functions

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)
3 views2 pages

Using Python's Math Module Functions

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

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

-------------------------------
import math # imports math module from anonaconda see general (recommended method)
dir(math) # shows all functions in math
print([Link]) # shows the value of pi
round([Link], 2) # rounds the pi function to 2 decimal places
-----------------------------------------------------------------------------------
-------------------------------
import math as m # imports math module as m
[Link] # similar to [Link]
-----------------------------------------------------------------------------------
-------------------------------
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
[Link](6) # 6*5*4*3*2*1
>> 720
[Link](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

[Link]() # generates a value between 0 and 1


>> 0.23596850404 # FLOAT

[Link](1,10) # generates a value between 1 and 10


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

[Link](1)
[Link]

[Link](1, 10) # generates a random whole number between 1 and 10


>> 2 # INT

[Link](1 , 5, 2) # generates a whole number between 1 and 5 increasing by


2 starting at 1
>> 1
>> 3

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


[Link](c) # choose a random choice

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

[Link](c, k=2) # pick 2 elements from list c *MUST use K*


>> 'red', 'red'

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


blue

[Link](c, 2) # similar to choice but cannot change weights

[Link](c) # changes order of list


res = [Link](c)
>> None, ['r', 'g', 'b']

You might also like