Python Modules — math, random, time

Modules — General concept

Make your own: elkan.py then elkan.logo / elkan.data / elkan.list.

import module
import module as m       # alias
from module import a, b, c
from module import *     # less ideal

Installing modules

  1. Go to Python Packages (PyCharm) — just press the red bulb and install.

math

import math as m
 
print(m.exp(x))         # Return exponential value with exponent x (e^x)
print(m.factorial(x))   # Return the factorial of x (x!)
print(m.pow(x, y))      # Return x raised to the power of y (x^y)
print(m.sin(x))         # Return the sine of x (x is measured in radians)
print(m.e, m.pi)        # Return the constants e (Euler's number) and pi
print(m.log10(100))     # Return log base 10 of number
print(m.log(100))       # Return natural log (log base e) of number
print(m.log(x, y))      # Return the log of x with base y
print(m.ceil(x))        # Return the round up of x (4.4 to 5)

random

randint(a, b)

random.randint(a, b)
# random integer from a to b inclusive

random()

random.random()
# returns a random floating number between 0.0 to 1.0 (exclusive)
 
random.random() * 5
# to get a floating number between 0.0 - 5.0, multiply by 5

choice()

random.choice(list)
# chooses a random item from a list. e.g., ["A", "B", "C"] gives "A".
 
# notes:
# can be used in a for loop. e.g., password += random.choice(list)
# can add a .lower() after if you want the input stored all lowercased.

shuffle()

random.shuffle(list)
 
# shuffles the items in a list. Don't need to assign a variable for this.
# just write in a line of code "random.shuffle(list)"

rand()

random.rand()
 
# Random value from 0 to 1

time

time.sleep()

time.sleep(1)
# adds a 1-second delay

See next