Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are the built-in functions in Python?
3.
Python abs()
4.
Python chr()
5.
Python ord()
6.
Python bytes()
7.
Python bytearray()
8.
Python bool()
9.
Python all()
10.
Python any()
11.
Python bin()
12.
Python ascii()
13.
Python compile()
14.
Python callable()
15.
Python complex()
16.
Python float()
17.
Frequently Asked Questions
17.1.
What is the built-in function product in Python?
17.2.
What is the built-in function found in Python?
17.3.
What are the built-in types provided by Python?
18.
Conclusion
Last Updated: Sep 5, 2024
Easy

Python Built-in Functions

Introduction

Python built-in functions are pre-defined functions provided by the language that simplify common programming tasks. They cover a wide range of operations, from basic input and output to complex data manipulations. These functions, such as print(), len(), and type(), enhance productivity by offering ready-to-use solutions, allowing developers to perform common tasks efficiently without needing to write custom code.

Python Built-in Functions

What are the built-in functions in Python?

Python offers a variety of built-in functions that perform common tasks, making programming more efficient. Some key built-in functions include:

  • print(): Outputs data to the console.
  • len(): Returns the length of an object (e.g., a string, list, or tuple).
  • type(): Returns the type of an object.
  • max(): Finds the maximum value in an iterable.
  • min(): Finds the minimum value in an iterable.
  • sum(): Calculates the sum of elements in an iterable.
  • abs(): Returns the absolute value of a number.
  • sorted(): Returns a sorted list of elements from an iterable.
  • range(): Generates a sequence of numbers, commonly used in loops.

Python abs()

This function returns the absolute value for a specified number. It is the most used built-in function of python. The syntax will be as follows:

abs(n)

Where n is the number to be specified for the absolute value.

Example 1

Input:

num = abs(3-5)
print(num)

Output

2

 

Example 2

Input:

num = abs(2+3j)
print(num)

Output

3.605551275463989

Python chr()

This function will return the character that represents the specified Unicode. The syntax for this function is as follows:

chr(n)

Where n is the number that specifies an ASCII value.

Example 1

Input:

n = chr(97)
print(n)

Output

a

 

Example 2

Input

n = chr(67)

print(n)

Output

C

Python ord()

This function will convert the character back to the specified Unicode. The syntax will be:

ord(c)

Where c is any string or character.

Example 

Input:

c = ord("g")

print(c)

Output

103

Python bytes()

The bytes() function deals with raw data and returns an immutable byte object, converting objects into byte objects of a specified size. The syntax will be:

bytes(x, encoding, error)

x: It is a source used when creating the byte objects.

Encoding: It represents the encoding of the string.

Error: It helps to provide a solution if the encoding fails.

Example

Input:

x = bytes(4)
print(x)

y=bytes('hello','utf-8')
print(y)

z=bytes([1,2,3])
print(z)

Output

b'\x00\x00\x00\x00'
b'hello'
b'\x01\x02\x03'

The difference between bytearray() and bytes is that bytes() are immutable while bytearray is mutable.

You can practice by yourself with the help of online python compiler.

Python bytearray()

The bytearray() function will return a bytearray object, an array of given byte size. It gives a mutable sequence of integers. The syntax will be as follows:

bytearray(source, encoding, error)

source: It will be used to initialize the array in different ways, covered in the examples below.

Encoding: Encoding of the string.

Errors: It helps to provide a solution if the encoding fails.

Example 1

Input:

# We will initialize the array with a given size. 
arr = bytearray(6)
print(arr)

# A array of 0 size
y = bytearray()
print(y)

# Encoding the string with 8 and 16
arr1 = bytearray('hello', 'utf-8')
arr2 = bytearray('hello', 'utf-16')
print(arr1)
print(arr2)

Output

bytearray(b'\x00\x00\x00\x00\x00\x00')
bytearray(b'')
bytearray(b'hello')
bytearray(b'\xff\xfeh\x00e\x00l\x00l\x00o\x00')

 

Example 2

Input

list = [1, 2, 6]
  
# passing the list in the bytearray()
arr = bytearray(list)
  
print(arr)

Output

bytearray(b'\x01\x02\x06')

Python bool()

This function will return the boolean value for the specified object, i.e., True or False. The object will always return True unless:

The object is False

The object is 0

The object is empty, like [], (), {}

The object is None

The syntax of bool() function is as follows:

bool(x)

x can be anything like string, list, number, etc. If nothing is passed in the parameter, then by default, it returns false. So passing a parameter is optional.

Example

Input:

# Returns false when 0 is passed
y = bool(0)
print(y)

# Returns True when true is passed as the parameter
y = bool(True)
print(y)

# Returns false when nothing is passed in the parameter
y = bool()
print(y)

# Returns false when empty list is passed
y = bool({})
print(y)

# Returns true when non empty string is passed
y = bool('hello')
print(y)

# Checking if x is divisble by 2 or not
x = 6
print(bool(x%2==0))

Output

False
True
False
False
True
True

Python all()

This function will return true if all the elements in an iterable are true; otherwise, it will return false. 

If the iterable object is empty, the all() function also returns True. The syntax will be as follows:

all(iterable)

The iterable object can be a list, tuple, dictionary, etc.

Example 1

Input:

# Returns False, as we know '0' represents False
list1 = [0, 1, 2]
y = all(list1)
print(y)

# Returns true as all the items of the tuple are true
tup1 = (1, True, True)
print(all(tup1))

Output

False
True

 

Example 2

Input:

# Returns False because the first key is false.
dict1 = {0 : "Hello", 1 : "World"}
y = all(dict1)
print(y)

tup = ()
print(all(tup))

Output

False
True

Python any()

This function will return true if any of the iterable items is true; else, it will return false.

The syntax will take one parameter just like all() functions.

any(iterable)

The iterable object can be a list, tuple, dictionary, etc.

Example 1

Input:

# Returns True because the second element is True
tup = (0, 1, False)
x = any(tup)
print(x)

tup = (0,0, False)
print(any(tup))

tup = ()
print(any(tup))

Output

True
False
False

 

Example 2

Input:

s = {1, 2}
print(any(s))

s = {}
print(any(s))

s = 'Hello'
print(any(s))

Output

True
False
True

Python bin()

This function will return a binary string for a given integer. The syntax will be as follows:

bin(n)

Where n is the integer to be converted to a binary string.

Example 1

Input:

x = bin(40)
print(x)

Output

0b101000

 

Example 2

Input:

y = bin(6.0)
print(y)

Output

Traceback (most recent call last):
  File "./prog.py", line 3, in <module>
TypeError: 'float' object cannot be interpreted as an integer.

We can't apply on float number.

Python ascii()

This function will return a readable version of any object like string, tuple, etc.

This function will replace any non-ASCII characters with escape characters.

For example:

ascii("Ë")

'\xcb'

The syntax will be as follows:

ascii(object)

An object like a list, tuple, string, etc.

Input:

x = ascii("åle")
print(x)

Output

'\xe5le'

 

Input:

ascii(['s','ș'])

Output

“[‘s’, ‘\\u0219’]”

Python compile()

This function will return a python code object. The syntax of this function is as follows:

compile(source, filename, mode, flag, dont_inherit, optimize)

Source: The source can be a normal string, byte string, etc.

Filename: The name of the file from that source comes from.

Mode: Legal value: exec, eval, single.

Flag: How to compile the source. The default value is 0.

Dont_herit: How to compile the source, the default value is False.

Optimize: It represents the optimization level of the compiler. 

Example

Input:

y = compile('print(61)\nprint(76)', 'test', 'exec')
exec(y)

Output

61
76

Python callable()

This function will return true if the specified object appears to be callable; otherwise, it will return false. The syntax of this function takes one parameter as an object and returns one of the two values: False or True.

callable(object)

The object you want to check is whether it is callable or not.

Example

Input:

n = 8
print(callable(n))

n = 4*4
print(callable(n))

n = 'Hello'
print(callable(n))

# A function is callable
n = callable
print(callable(n))

#A 
x = list
print(callable(x))

# A function is callable, but the list is not.
x = [3, 5]
print(callable(x))

Output

False
False
False
True
True
False

Python complex()

This function will return a complex number using a real number and an imaginary number. The syntax will be as follows:

complex(real, imaginary)

The real part will contain a number representing the complex number's real part.

The imaginary part is optional, but it will represent the imaginary part of the complex number.

Example

Input:

y = complex('6+5j')
print(y)

y = complex(3, 5)
print(y)

Output

(6+5j)
(3+5j)

Python float()

This function will return the floating-point number from int or a compatible value. The syntax will be as follows:

float(n)

Here' n' can be an integer, floating-point number, or a string.

Example 1 

Input

x = float("7.500")
print(x)

x = float("23")
print(x)

x = float(True)
print(x)

Output

7.5
23.0
1.0

 

Example 2

Input

x = float("3s")
print(x)

Output

Traceback (most recent call last):
  File "./prog.py", line 10, in <module>
ValueError: could not convert string to float: '3s'

 

Also see, Floor Division in Python, and Convert String to List Python

Frequently Asked Questions

What is the built-in function product in Python?

Python does not have a built-in product function. To calculate the product of elements, use math.prod() from the math module or a custom function.

What is the built-in function found in Python?

Python provides built-in functions like print(), len(), type(), max(), and min(), which perform common tasks without needing external libraries.

What are the built-in types provided by Python?

Python's built-in types include int (integers), float (floating-point numbers), str (strings), list (lists), tuple (tuples), dict (dictionaries), and set (sets). These types cover fundamental data structures and operations in Python.

Conclusion

This blog has covered the several built-in functions of the python language with the implementation of each one. Built-in functions help to simplify the code.

For more clarity and details regarding the Python language, one can refer to this article. 

Recommended Readings:

Code360 is a one-stop destination for various DSA questions typically asked in interviews to practice more such problems.

Live masterclass