Python Built-in functions
Built-in functions of Python are predefined functions that are always available without the need to import any additional modules. They offer a vast range of utilities for input/output, mathematical calculations, sequence manipulations, type conversion, and much more. They save time and make common operations easy.
Main features of Built-in functions
- Always Available: No imports are necessary.
- Versatile: Implements a variety of tasks such as math, I/O, string handling.
- Optimized: Efficient and strong implementations.
- Dynamic: It can work on various data types (polymorphism).
1. Input/Output
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
- Displays output to the console.
- Example:
print("Hello", "World", sep=", ", end="!\n") # Output: Hello, World!
input(prompt=None)
- Takes input from the user and returns it as a string.
- Example:
name = input("Enter your name: ")
print("Hello,", name)
2. Type Conversion
int(x, base=10)
- Converts
x
to an integer. - Example:
int("123") # Output: 123
int("101", 2) # Output: 5
float(x)
- Converts
x
to a floating-point number. - Example:
float("3.14") # Output: 3.14
str(x)
- Converts
x
to a string. - Example:
str(123) # Output: "123"
bool(x)
- Converts
x
to a boolean value (True
orFalse
). - Example:
bool(0) # Output: False
bool(1) # Output: True
3. Mathematical Functions
abs(x)
- Returns the absolute value of
x
. - Example:
abs(-5) # Output: 5
pow(x, y, z=None)
- Calculates
x
raised to the powery
(x^y
). Ifz
is provided, computes(x^y) % zv
. - Example:
pow(2, 3) # Output: 8
pow(2, 3, 5) # Output: 3
round(number, ndigits=None)
- Rounds
number
tondigits
decimal places. - Example:
round(3.14159, 2) # Output: 3.14
divmod(a, b)
- Returns a tuple
(a // b, a % b)
. - Example:
divmod(10, 3) # Output: (3, 1)
4. Sequence Operations
len(s)
- Returns the number of items in a sequence.
- Example:
len("Python") # Output: 6
len([1, 2, 3]) # Output: 3
min(iterable, *[, key, default])
- Returns the smallest item in an iterable.
- Example:
min([3, 1, 4]) # Output: 1
max(iterable, *[, key, default])
- Returns the largest item in an iterable.
- Example:
max([3, 1, 4]) # Output: 4
sum(iterable, start=0)
- Returns the sum of a sequence.
- Example:
sum([1, 2, 3]) # Output: 6
5. Object Inspection
type(object)
- Returns the type of
object
. - Example:
type(123) # Output: <class 'int'>
id(object)
- Returns the memory address of
object
. - Example:
id(123) # Output: A unique integer
dir([object])
- Lists attributes and methods of
object
(or current scope if omitted). - Example:
dir(str) # Lists methods of the string class
6. Advanced Functions
enumerate(iterable, start=0)
- Returns an iterator of tuples
(index, value)
. - Example:
for i, v in enumerate(["a", "b", "c"]):
print(i, v)
# Output:
# 0 a
# 1 b
# 2 c
zip(*iterables)
- Combines multiple iterables into tuples.
- Example:
list(zip([1, 2], ['a', 'b'])) # Output: [(1, 'a'), (2, 'b')]
map(function, iterable)
- Applies
function
to every item ofiterable
. - Example:
list(map(str.upper, ['a', 'b', 'c'])) # Output: ['A', 'B', 'C']
filter(function, iterable)
- Filters items in
iterable
wherefunction
returnsTrue
. - Example:
list(filter(lambda x: x > 2, [1, 2, 3])) # Output: [3]
7. Miscellaneous
help([object])
- Displays help documentation for
object
. - Example:
help(print) # Shows documentation for the print function
isinstance(object, classinfo)
- Checks if
object
is an instance ofclassinfo
. - Example:
isinstance(123, int) # Output: True
sorted(iterable, key=None, reverse=False)
- Returns a sorted list from the items in
iterable
. - Example:
sorted([3, 1, 2]) # Output: [1, 2, 3]
Python’s built-in functions form the foundation of everyday programming tasks. They improve efficiency, readability, and simplicity so that developers can focus on solving complex problems rather than reinventing the wheel.