Python comes with a library of built-in functions, readily available for use without the need for any imports. These functions are universal, versatile, and form the foundational building blocks for various tasks in Python programming.
print()
– Displays the specified message or value.print("Hello, World!") # Outputs: Hello, World!
input()
– Reads a line from input and returns it as a string.name = input("Enter your name: ")
len()
– Returns the length of an object.print(len("Python")) # Outputs: 6
abs()
– Returns the absolute value of a number.print(abs(-5)) # Outputs: 5
max()
and min()
– Returns the largest or smallest of input values.print(max(3, 5, 2)) # Outputs: 5
print(min(3, 5, 2)) # Outputs: 2
sum()
– Returns the sum of a list or tuple.print(sum([1, 2, 3, 4])) # Outputs: 10
round()
– Rounds a number to the nearest integer or to the specified number of decimals.print(round(5.6)) # Outputs: 6
print(round(5.678, 2)) # Outputs: 5.68
int()
, float()
, str()
– Converts a value to an integer, floating-point number, or string, respectively.print(int("5")) # Outputs: 5
print(float("5.6")) # Outputs: 5.6
print(str(100)) # Outputs: "100"
list()
, tuple()
, set()
, dict()
– Converts to respective data types.print(list("python")) # Outputs: ['p', 'y', 't', 'h', 'o', 'n']
range()
– Produces a sequence of numbers.for i in range(3):
print(i) # Outputs: 0, 1, 2
enumerate()
– Returns an enumerate object.for index, value in enumerate(["a", "b", "c"]):
print(index, value) # Outputs: 0 a, 1 b, 2 c
zip()
– Merges two or more lists into a list of tuples.names = ["Alice", "Bob"]
ages = [25, 30]
print(list(zip(names, ages))) # Outputs: [('Alice', 25), ('Bob', 30)]
type()
– Returns the type of an object.print(type(5)) # Outputs: <class 'int'>
isinstance()
– Checks if an object is an instance of a specified type or class tuple.print(isinstance(5, int)) # Outputs: True
print(isinstance(5, (str, int))) # Outputs: True
Python’s built-in functions are powerful tools that provide core functionality right out of the box. Leveraging these functions can significantly reduce the time and effort required to implement common tasks and checks. As you delve deeper into Python, you’ll find yourself turning to these built-ins frequently, making them an integral part of your coding arsenal.