Built-in functions in Python are a set of predefined functions that are available to use in any Python program without the need to import any additional modules or libraries.
These functions provide a wide range of functionality, including mathematical operations, string manipulation, data structure manipulation, and input/output operations.
Python Built-in Functions Explained Simply | With Examples #pythontutorial #programminglanguage
Python provides built-in tools to dynamically inspect the current scope and see all functions available out of the box. You can quickly list all built-in functions using Python's dir() function on the built-in scope.
1. Using dir(__builtins__)
The fastest way to list built-in functions is by using the __builtins__ object directly in your code:
print(dir(__builtins__))
2. Importing the builtins Module
You can also explicitly import the standard builtins module to retrieve the list:
import builtins
print(dir(builtins))
Filtering Callable Functions Only
Running dir(builtins) returns everything in the built-in namespace, including internal attributes (like __name__), constants (like True and False), and built-in exceptions (like ValueError).
To extract only callable functions while excluding internal dunder methods and constants, you can use a list comprehension with callable():
import builtins
# Filter out internal dunder attributes and select callable functions
builtin_functions = [
name for name indir(builtins)
ifcallable(getattr(builtins, name)) and not name.startswith("__")
]
print(builtin_functions)
lambda :Inline, concise expressions for simple operations.
To display above list we can use dir() with builtins module dir()
import builtins
for i in dir(builtins):
print(i)
print("Total Builtin Functions : ",len(dir(builtins)))
Based on the version of Python we are using the number of Builtin functions will varry.
Total Builtin Functions : 158
Checking if a word is Built-in function or not
import builtins
if 'sum' in dir(builtins):
print(' sum is a built-in function' )
else:
print(' No it is not a built-in function')
Output
sum is a built-in function
Can I use builtin functions as variable names ?
No, it's generally not recommended to use built-in functions as variable names in Python. While it's technically possible in some cases, doing so can lead to several drawbacks and potential issues:
Naming Conflicts and Overriding:
Python reserves certain names for built-in functions, classes, and modules. Assigning those names to your variables would make them inaccessible, as your variable would take precedence within its scope.
Even if a built-in function isn't directly used in the same scope, there's a risk of accidentally overriding its intended behavior later in your code, leading to unexpected results.
Examples
#int=225 # this line will override the built-in function int()
x=int(input('Enter a number : '))
print(x*2)
Reserved words as variables are not allowed (error ) but Built-in function we can use.
#True=50 # reserved words as variables are not allowed
x=sum([5,7]) # sum is a built-in function
sum=50 # Use of Built-in functions as variables allowed
#x=sum([6,7])
print(sum)
How to avoid using built-in functions and reserved words ?
Use descriptive names:
Choose names that reflect the variable's content or purpose.
Explain what the variable holds, making your code self-documenting.
Avoid ambiguous names like x, temp, or data.
Instead, use names like first_name, total_price, or average_score.
To minimize the risk of accidentally overriding built-in functions, consider using a consistent prefix for your variable names. This is a common practice that promotes code readability and maintainability.
Example:
Popular prefixes include my_, user_, or custom_. This improves code clarity and helps distinguish your variables from built-in functions.
Built-in Functions vs. Standard Library vs. Third-Party Modules 🔝
A common point of confusion for beginners is understanding why some functions are directly available while others require an import statement or external installation. In Python, tools are categorized based on how they are loaded into memory:
Built-in Functions: Functions like len(), print(), type(), and range() are loaded automatically into Python's global namespace whenever your script or environment runs. You do not need to use an import statement to use them—they are ready to execute out of the box.
Python Standard Library: Modules such as math, os, datetime, and sys come pre-installed with every standard Python download. While they do not require extra installation via pip, you must explicitly write import math or import os before using their functions. This keeps Python lightweight by loading specialized tools only when needed.
Third-Party Libraries: External packages like pandas, numpy, requests, or matplotlib are not included with default Python installations. To use them, you must install them into your environment first using a package manager like pip install pandas, and then load them using import pandas as pd in your code.
Understanding Built-in Items vs. Callable Functions 🔝
When you inspect Python's default environment using dir(__builtins__), Python returns a list of approximately 160 items. However, not every item in this list is a standard function that takes inputs and performs an action. Instead, the __builtins__ module contains a mix of functions, exception handlers, system constants, and internal properties.
To separate executable tools from static attributes, we use Python's callable() function. Filtering the list using callable() yields around 146 callable built-ins. Here is how the full list breaks down:
Callable Functions (~146 items): These are items you can execute using parentheses (). They include core utility functions like len(), print(), and sum(), as well as built-in type constructors like int(), str(), list(), and dict().
Built-in Exceptions & Errors: Standard exception classes like ValueError, TypeError, ZeroDivisionError, and FileNotFoundError are defined within the built-ins module so Python can handle errors across all scripts without requiring additional imports.
Constants & Internal Attributes: Static values and metadata like True, False, None, __name__, and __doc__ reside in the built-ins namespace. Since these are fixed values or properties rather than executable logic, passing them to callable() returns False.
By using [item for item in dir(__builtins__) if callable(getattr(__builtins__, item))], you can programmatically extract only the active, callable tools available in your Python environment.
The term "Vanilla Python" refers to writing Python code using only standard, unadorned language features—specifically built-in functions, native data structures, and modules provided by the standard library. When a project is written in Vanilla Python, it means it runs cleanly without relying on any third-party packages installed via pip.
Understanding Vanilla Python is essential before adopting external frameworks. It ensures your code remains fast, lightweight, and free from external dependency management issues or unexpected updates from third-party packages.
Comparing Built-in, Standard Library, and Third-Party Functions 🔝
Feature
Built-in Functions
Standard Library
Third-Party Libraries
Definition
Core functions embedded directly into the Python interpreter.
Pre-installed modules bundled with the official Python distribution.
External packages created and maintained by the Python developer community.
Installation
Built-in automatically.
Bundled with Python installation.
Requires installation via pip install package_name.
Import Required?
No (available globally).
Yes (e.g., import math).
Yes (e.g., import pandas as pd).
Examples
len(), print(), type()
math, datetime, os, random
pandas, numpy, requests
Primary Use Case
Essential data manipulation and basic operations.
Standard system, math, and utility tasks.
Specialized, complex domains (Data Science, Web Scraping, AI).
Download the above full source code from Github or run the code in your Google colab platform.
Passionate about coding and teaching, I publish practical tutorials on PHP, Python,
JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and
project‑oriented with real examples and source code.