Python is known for its simplicity and power—and much of that comes from its rich set of built-in functions and standard modules. Whether you’re a beginner or an experienced developer, mastering these core tools will save you time, simplify your code, and enhance your productivity.
This post covers the most useful built-in functions and how to use standard modules effectively with examples and explanations in a human-friendly manner.
1. What are Built-in Functions?
Built-in functions are functions that are always available in Python without needing to import anything. They handle common tasks like type conversions, math operations, string manipulations, and more.
Popular Built-in Functions with Examples
# Example 1: len() - Get length of a sequence
name = "Python"
print(len(name)) # Output: 6
# Example 2: type() - Find type of a variable
print(type(3.14)) # Output:
# Example 3: int(), float(), str() - Type conversions
print(int("5")) # Output: 5
print(float("3.5")) # Output: 3.5
print(str(100)) # Output: '100'
# Example 4: sum() - Sum of numbers in a list
print(sum([10, 20, 30])) # Output: 60
# Example 5: max() and min()
print(max(4, 7, 1)) # Output: 7
print(min(4, 7, 1)) # Output: 1
2. What are Modules in Python?
A module in Python is simply a file that contains Python code—functions, classes, or variables—that you can reuse. Python includes a standard library of modules for common tasks like working with dates, random numbers, math operations, file systems, and more.
You can use the import
statement to bring modules into your script.
import math
print(math.sqrt(16)) # Output: 4.0
3. Commonly Used Built-in Modules
3.1 math – Mathematical operations
import math
print(math.pi) # Output: 3.141592653589793
print(math.ceil(4.2)) # Output: 5
print(math.factorial(5))# Output: 120
3.2 random – Generate random numbers
import random
print(random.randint(1, 10)) # Random int between 1 and 10
print(random.choice(['A', 'B'])) # Randomly choose an item
3.3 datetime – Work with dates and times
import datetime
now = datetime.datetime.now()
print(now.strftime("%Y-%m-%d %H:%M:%S"))
3.4 os – Interact with the operating system
import os
print(os.getcwd()) # Get current directory
print(os.name) # OS name (e.g., posix or nt)
3.5 sys – Access system-specific parameters and functions
import sys
print(sys.version) # Python version info
print(sys.platform) # Platform info
4. Exploring All Built-in Functions
You can use the dir()
and help()
functions to explore what's available in Python.
# Example: List all built-in functions
print(dir(__builtins__))
# Get help on a specific function
help(len)
5. Creating Your Own Modules
Besides using built-in modules, you can also create your own. Just save your functions in a .py
file and import it wherever needed.
# greetings.py
def welcome(name):
return f"Welcome, {name}!"
# main.py
import greetings
print(greetings.welcome("Arjun"))
6. Summary Table
Function/Module | Purpose | Example |
---|---|---|
len() |
Length of a string/list | len("abc") |
math |
Advanced math functions | math.sqrt(25) |
random |
Random data generation | random.choice() |
datetime |
Date and time handling | datetime.now() |
os |
OS-level tasks | os.getcwd() |
7. Final Thoughts
Python’s built-in functions and standard modules are incredibly powerful tools that help you write efficient and readable code. Instead of reinventing the wheel, take time to explore these tools and use them to your advantage. Whether you need to calculate a square root, shuffle a list, or get the current date—Python has a built-in way to do it.