1.
Introduction
2.
Built-in Functions
2.1.
abs()
2.2.
chr()
2.3.
ord()
2.4.
bytes()
2.5.
bytearray()
2.6.
bool()
2.7.
all()
2.8.
any()
2.9.
bin()
2.10.
ascii()
2.11.
compile()
2.12.
callable()
2.13.
complex()
2.14.
float()
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024

# Python Built-in Functions

Basics of Python
Free guided path
7 chapters
99+ problems

## Introduction

In the programming world, the python language has attracted a lot of mass due to its English-like syntax and portable nature.
As we all know, inbuilt functions help to ease our code readability, and we will cover some of the important inbuilt functions in python.

## Built-in Functions

### 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

Input:

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

Output

``2``

Example 2

Input:

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

Output

``3.605551275463989``

### 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

Input:

``````n = chr(97)
print(n)``````

Output

``a``

Example2

Input

``````n = chr(67)

print(n)``````

Output

``C``

### 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``

### 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.

### 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

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')``

### 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``````

### 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

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``````

### 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``````

### 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

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.

### 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’]”``

### 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``````

### 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``````

### 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)``````

### 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

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

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'``````
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## FAQs

1. What is the use of the abs() function?
The abs() function will return the absolute value of the specified number.
Example

``print(abs(3-4))``

Output
1

2. Why python is it better than other programming languages?
Python language is better and easier to write and understand than other programming languages due to its English-like syntax and portable nature.

## Key Takeaways

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.

Anyone interested in learning the python language can enroll in the intuitively designed courses on Coding Ninjas.

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

Happy Coding!!!

Guided path
Free
Basics of Python
7 chapters
127+ Problems