Built-in functions in Python are pre-defined functions that are readily available. We don’t need to import any module to use these functions. These functions provide common functionalities and simplify various programming tasks.
Python provides a large set of built-in functions that are always available for use without needing to import any modules. They simplify common tasks and cover a wide range of operations, from simple input/output to more complex data manipulation.
Core Built-in Functions
print()
The print() function is used to output data. We can print strings, numbers, or other objects. We can print strings, numbers, or other objects.
Example – 1:
print(“Hello, Python!”)
Output:
Hello, Python!
Example – 2:
name = “Raja”
age = 20
print(f”My name is {name} and I am {age} years old.”)
Output:
My name is Raja and I am 20 years old
input()
The input() function is used to take input from user in the form of string.
Example:
name = input(“Enter your name: “)
print(f”Hello {name}”)
Output:
Enter your name: Eliza
Hello Eliza
len()
The len() function is used to return the length of a string, list, tuple etc.
Example-1
my_string = “Python”
print(len(my_string))
Output:
6
Example -2
my_list = [1, 2, 3, 4, 5]
print(len(my_list))
print(f”Length of list: {len([1, 2, 3])}”)
Output:
5
Length of list: 3
type()
This function is used to identify the type of any value or variable.
Example:
x = 10
kilometer =1.5
name = ”Eliza”
is_student=True
print(type(x))
print(type(kilometer))
print(type(name))
print(type(is_student))
print(type(42))
print(type(“hello”))
print(type([1, 2, 3]))
Output:
<class ‘int’>
<class ‘float’>
<class ‘str’>
<class ‘bool’>
<class ‘int’>
<class ‘str’>
<class ‘list’>
eval()
The evaluate function, or eval() function is used to evaluate the value of a string.
Example:
x=eval(“45+10“)
print(x)
print(eval(‘1+2’))
print(eval(“sum([1, 2, 3, 4])”))
Output:
55
3
10
Type Conversion Functions
int()
The int() function is used to convert a string or other object into an integer.
Example -1:
# int() – Convert string to integer
num = int(“42”)
print(f”Integer value: {num}”)
Output:
Integer value: 42
Example -2
number_str = “123”
number_int = int(number_str)
print(number_int + 1)
Output:
124
float()
The float() function is used to convert a string or other object into a floating-point number.
Example -1:
# float() – Convert integer to float
num = float(7)
print(“Float value:”, num)
Output:
7.0
Example -2:
float_num = float(“3.14”)
print(float_num * 2)
Output:
6.28
str()
We use the str() function to convert a value into a string.
Example:
# str() – Convert number to string
num = 123
text = str(num)
print(“String value:”, text)
Output:
String value: 123
bool()
The bool() function is used to convert a value or expression to its corresponding Boolean value, which is either True or False.
Example:
print(bool(10))
print(bool(0))
print(bool(“hello”))
print(bool(“”))
print(bool([1, 2]))
print(bool([]))
Output:
True
False
True
False
True
False
list()
We use the list() function in Python to create a list from an iterable like a string, a tuple, or a set. If no argument is passed, it returns an empty list.
Example:
# list() – Convert string to list
letters = list(“ABC”)
print(f”List: {letters}”)
Output:
List: [‘A’, ‘B’, ‘C’]
tuple()
We use the tuple() function to create a tuple from an iterable like a list, string, or set. It returns an immutable ordered collection.
Example:
# tuple() – Convert list to tuple
items = [1, 2, 3]
t = tuple(items)
print(“Tuple:”, t)
Output:
Tuple: (1, 2, 3)
set()
The set() function is used to create a set
Example:
# set() – Create a set from a list
nums = [1, 2, 2, 3]
unique_nums = set(nums)
print(“Unique elements:”, unique_nums)
Output:
Unique elements: {1, 2, 3}
dict()
The dict() function is used to create a dictionary. It takes several key-value pairs as arguments and uses them to create a dictionary.
Example -1:
my_dict = dict(name=”Bob”, age=25)
print(my_dict)
Output:
{‘name’: ‘Bob’, ‘age’: 25}
Example -2
my_dict = dict(a=1, b=2)
print(“Dictionary:”, my_dict)
Output:
Dictionary: {‘a’: 1, ‘b’: 2}
Mathematical Functions
abs()
This function returns the absolute value of a number.
Example-1:
print(abs(-15))
print(abs(3.14))
Output:
15
3.14
Example -2
num = -10
print(f”Absolute value: {abs(num)}”)
Output:
Absolute value: 10
sum()
The sum() function returns the sum of all items in an iterable. sum() calculates the sum of elements.
Example:
nums = [10, 20, 5, 30]
print(f”Total Sum: {sum(nums)}”)
Output:
Total sum: 65
max()
The max() function returns the greatest or largest item in an iterable. It also returns the largest of two or more arguments.
Example:
numbers = [10, 20, 5, 30]
print(max(numbers))
print(f”Maximum value: {max([1, 5, 3])”)
Output:
30
Maximum value:5
min()
We use the min() function to find the smallest item in an iterable or the smallest among two or more arguments. It works with numbers, strings, and other comparable types.
Example:
nums = [6, 9, 3, 15]
print(“Minimum value:”, min(nums))
Output:
Minimum value: 3
round()
The round() function rounds off the digits of the given number and returns floating-point numbers rounded to the specified number of decimals.
Example-1:
print(round(12.12321,3))
print(round(12.12321,2))
print(round(12.12321,0))
Output:
12.123
12.12
12.0
Example-2:
pi = 3.14159
print(round(pi, 2))
Output:
3.14
pow()
We use the pow() function to raise a number to the power of another.
Example:
print(pow(2, 3))
Output:
8
reversed()
We use the reversed() function in Python to reverse the reversed iterator of the given sequence.
Example:
# reversed() – Reverse a list
nums = [1, 2, 3, 4]
rev = reversed(nums)
print(“Reversed list:”, list(rev))
Output:
Reversed list: [4, 3, 2, 1]
divmod()
This function computes the quotient and remainder of integer division. divmod() function takes two numbers as arguments and returns a tuple with the quotient and remainder that result from the integer division of the input numbers.
Example:
print(divmod(8,4))
print(divmod(6.5,3.5))
Output:
(2, 0)
(1.0, 3.0)
Iterator and Iterable Function
range()
This function is used to generate a sequence of numbers, commonly used in for loops. If we want the series between two numbers then we can use this function.
Its syntax is – range( start, stop, step)
This gives the series from START to STOP-1 and the interval between two numbers of series will be STEP.
Example-1:
print(list(range(1,6)))
Output:
[1, 2, 3, 4, 5]
Example-2:
# Print numbers from 0 to 4
for i in range(5):
print(i)
Output:
0
1
2
3
4
Example-3:
# Print numbers from 1 to 5
for i in range(1,6):
print(i,end=’ ‘)
Output:
1 2 3 4 5
sorted()
The sorted() function returns a new sorted list from the items in an iterable. It does not change the original data and can sort in ascending or descending order.
Example -1:
# sorted() – Sort a list in ascending order
nums = [5, 2, 9, 1]
print(“Sorted list:”, sorted(nums))
Output:
Sorted list: [1, 2, 5, 9]
Example -2:
# sorted() – Sort a list in descending order
nums = [5, 2, 9, 1]
print(“Sorted list:”, sorted(nums, reverse=True))
Output:
Sorted list: [9, 5, 2, 1]
enumerate()
The enumerate() function adds a counter to an iterable and returns an enumerated object. It’s commonly used to get both the index and value in a loop.
Example -1:
# enumerate() – Loop with index and value
for i, val in enumerate([‘a’, ‘b’, ‘c’], start=1):
print(i, val)
Output:
1 a
2 b
3 c
Example 2:
fruits = [“apple”, “banana”, “cherry”]
for index, fruit in enumerate(fruits):
print(f”Index: {index}, Value: {fruit}”)
Output:
Index: 0, Value: apple
Index: 1, Value: banana
Index: 2, Value: cherry
zip()
The zip() function creates an iterator that aggregates elements from two or more iterables.
Example:
names = [“Alice”, “Bob”]
ages = [30, 25]
for name, age in zip(names, ages):
print(f”{name} is {age} years old.”)
Output:
Alice is 30 years old.
Bob is 25 years old.
map()
map () function helps in transforming every single original item into a new (transformed) item. The Map () function take a function and a iterable as arguments. It loops over all items of an iterable, and then the transformation function is applied to the items. After passing the map function, it returns a map object storing the value of each transformed item.
Example:
my_list = [2.6743,3.63526,4.2325,5.9687967,6.3265,7.6988]
updated_list = map(round, my_list)
print(updated_list)
print(list(updated_list))
Output:
<map object at 0x0000025DB6347D60>
[3, 4, 4, 6, 6, 8]