Module & Import
Module adalah file Python yang berisi definisi dan statement. Package adalah folder berisi module.
Import Module
import math
print(math.sqrt(16)) # 4.0
print(math.pi) # 3.141592653589793
# Import specific function
from math import sqrt, pi
print(sqrt(25)) # 5.0
# Import dengan alias
import json as j
data = j.dumps({"nama": "Budi"})
Module Bawaan Penting
math— Fungsi matematika (sqrt, ceil, floor, pi)random— Random number generationjson— Parse dan serialize JSONdatetime— Tanggal dan waktuos— Interaksi dengan operating systemsys— System-specific parameters
Contoh: math module
import math
print(math.factorial(5)) # 120
print(math.gcd(12, 8)) # 4
print(math.ceil(4.2)) # 5
print(math.floor(4.8)) # 4