Skip to main content

Python Functions via Import

📦 Python has many useful functions and methods that you can access by importing modules. These extend Python’s basic functionality and are especially helpful for mathematics, randomness, time, files, system access and more.

Here’s an overview of the most important importable functions and methods – including module, purpose, and example:


🧮 Mathematics – math Module
#

import math
Function Description Example
math.sqrt(x) Square root math.sqrt(16)4.0
math.pow(x, y) Power math.pow(2, 3)8.0
math.floor(x) Round down math.floor(3.9)3
math.ceil(x) Round up math.ceil(3.1)4
math.pi Pi constant math.pi3.14159...

🎲 Random – random Module
#

import random
Function Description Example
random.random() Random number between 0 and 1 random.random()
random.randint(a, b) Integer between a and b random.randint(1, 6)
random.choice(seq) Random element from list random.choice(["red", "blue"])
random.shuffle(seq) Shuffle list randomly random.shuffle(list)

Time – time Module
#

import time
Function Description Example
time.sleep(x) Pause for x seconds time.sleep(2)
time.time() Current time in seconds since 1970 time.time()
time.ctime() Readable time time.ctime()

📁 Files & Paths – os Module
#

import os
Function Description Example
os.getcwd() Current working directory os.getcwd()
os.listdir(path) Show files in directory os.listdir(".")
os.remove(file) Delete file os.remove("test.txt")

📚 File Operations – open() Function
#

with open("text.txt", "r") as f:
    content = f.read()
Method Description Example
read() Read entire file f.read()
write() Write to file f.write("Hello")
readlines() Read line by line f.readlines()

🧪 Counting & Statistics – collections Module
#

from collections import Counter
Function Description Example
Counter() Counts elements in a list Counter(["a", "b", "a"])

📊 Statistics – statistics Module
#

import statistics
Function Description Example
mean() Average statistics.mean([1,2,3])
median() Median value statistics.median([1,2,3])
mode() Most frequent value statistics.mode([1,1,2])

🧠 Memory Tip for Students:
#

🔧 Functions like print() are directly available
📦 Modules like math, random, os must be imported