Program 1
Use the math module
Import and call math functions.
python
import math
print(math.sqrt(16))
print(math.pi)
print(math.factorial(5))Topic 13 of 17
Reuse code with imports, standard library highlights, and pip.
Program 1
Import and call math functions.
import math
print(math.sqrt(16))
print(math.pi)
print(math.factorial(5))Program 2
Generate random data.
import random
print(random.randint(1, 10))
print(random.choice(["red", "green", "blue"]))
print(random.sample(range(100), 5))Program 1
Use datetime for date arithmetic.
from datetime import datetime, timedelta
now = datetime.now()
print("Now:", now.strftime("%Y-%m-%d %H:%M"))
print("Next week:", now + timedelta(days=7))Program 1
Split code across files and import.
# file: mymath.py
def add(a, b):
return a + b
def mul(a, b):
return a * b
# file: main.py
import mymath
print(mymath.add(2, 3))
print(mymath.mul(4, 5))