15 Python Built-in Functions which You Should Know while learning Data Science

Chirag Goyal Last Updated : 14 Oct, 2024
8 min read

Python built-in functions are pre-defined functions that come bundled with Python and can be used directly in a Python program without importing external libraries. They provide essential and commonly used functionality for various tasks, such as data type conversion, mathematical operations, string manipulation, file operations, etc. Here are the most commonly used 15 python built-in functions!

This article was published as a part of the Data Science Blogathon.

Table of contents

 

Top 15 Python Built-in Functions

The built-in Python Functions which we are going to discuss in this article are as follows:

FunctionExampleDescription
print()print("Hello, World!")Prints output to the console
len()len("Python")Returns the length of a string
range()range(0, 10)Generates a range of integers from 0 to 9
str()str(123)Converts the integer value to a string
int()int("123")Converts the string value to an integer
float()float(123)Converts the integer value to a floating-point number
max()max(1, 2, 3, 4, 5)Returns the maximum value in the list of numbers
min()min(1, 2, 3, 4, 5)Returns the minimum value in the list of numbers
sorted()sorted([3, 2, 1])Sorts the list in ascending order
type()type("Python")Returns the type of the object as “str”
input()name = input("Enter your name: ")Gets user input from the console and assigns it to a variable
sum()sum([1, 2, 3, 4, 5])Returns the sum of all elements in the list
abs()abs(-10)Returns the absolute value of the number
round()round(3.14159, 2)Rounds the number to 2 decimal places
chr()chr(65)Returns the character corresponding to the ASCII code 65, which is “A”

Now, let’s discuss each of the above functions one-by-one with some examples:

1. print( ) function

The print() function prints the specified message to the screen or another standard output device.

The message that wants to print can be a string or any other object. This function converts the object into a string before written to the screen.

Example 1: Print a string onto the screen:

print("Analytics Vidhya is the Largest Data Science Community over whole world")

Output:

Analytics Vidhya is the Largest Data Science Community over whole world
print("Analytics Vidhya is the Largest Data Science Community over whole world")

Example 2: Print more than one object onto the screen:

print("Analytics Vidhya", "Chirag Goyal", "Data Scientist", "Machine Learning")

Output:

Analytics Vidhya Chirag Goyal Data Scientist Machine Learning

2. type( ) function

The type() function returns the type of the specified object.

Example 1: Return the type of the following object:

list_of_fruits = ('apple', 'banana', 'cherry', 'mango')
print(type(list_of_fruits))

Output:

<class 'tuple'>

Example 2: Return the type of the following objects:

variable_1 = "Analytics Vidhya"
variable_2 = 105
print(type(variable_1))
print(type(variable_2))

Output:

<class 'str'>
<class 'int'>

3. input( ) function

The input() function allows taking the input from the user.

Example: Use the prompt parameter to write the complete message after giving the input:

a = input('Enter your name:')
print('Hello, ' + a + ' to Analytics Vidhya')

Output:

Enter your name:Chirag Goyal
Hello, Chirag Goyal to Analytics Vidhya

4. abs( ) function

The abs() function returns the absolute value of the specified number.

Example 1: Return the absolute value of a negative number:

negative_number  = -676
print(abs(negative_number))

Output:

676

Example 2: Return the absolute value of a complex number:

complex_number = 3+5j
print(abs(complex_number))

Output:

5.830951894845301

The absolute value of a complex number is defined in the following way:

How Do You Find the Absolute Value of a Complex Number? | Virtual Nerd

                                                  Image Source: Link

5. pow( ) function

The pow() function returns the calculated value of x to the power of y i.e, xy.

If a third parameter is present in this function, then it returns x to the power of y, modulus z.

Example 1: Return the value of 3 to the power of 4:

x = pow(3, 4)
print(x)

Output:

81

Example 2: Return the value of 3 to the power of 4, modulus 5 (same as (3 * 3 * 3 * 3) % 5):

x = pow(3, 4, 5)
print(x)

Output:

1

6. dir( ) function

The dir() function returns all the properties and methods of the specified object, without the values.

This function even returns built-in properties which are the default for all objects.

Example: Display the content of an object:

class Person:
  name = "Chirag Goyal"
  age = 19
  country = "India"
  education = "IIT Jodhpur"
print(dir(Person))

Output:

Python Built-in functions display object function

 

7. sorted( ) function

The sorted() function returns a sorted list of the specified iterable object.

You can specify the order to be either ascending or descending. In this function, Strings are sorted alphabetically, and numbers are sorted numerically.

NOTE: If a list contains BOTH string values AND numeric values, then we cannot sort it.

Example 1: Sort the specified tuple in ascending order:

tuple = ("h", "b", "a", "c", "f", "d", "e", "g")
print(sorted(tuple))

Output:

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

Example 2: Sort the specified tuple in descending order:

tuple = ("h", "b", "a", "c", "f", "d", "e", "g")
print(sorted(tuple, reverse=True))

Output:

['h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']

 

8. max( ) function

The max() function returns the item with the maximum value or the item with the maximum value in an iterable.

If the values this function takes are strings, then it is done using an alphabetical comparison.

Example 1: Return the name with the highest value, ordered alphabetically:

names_tuple = ('Chirag', 'Kshitiz', 'Dinesh', 'Kartik')
print(max(names_tuple))

Output:

Kshitiz

Example 2: Return the item in a tuple with the highest value:

number_tuple = (2785, 545, -987, 1239, 453)
print(max(number_tuple))

Output:

2785

9. round( ) function

The round() function returns a floating-point number that is a rounded version of the specified number, with the specified number of decimals.

The default number of decimals is 0, meaning that the function will return the nearest integer.

Example 1: Round the specified positive number to the nearest integer:

nearest_number = round(87.76432)
print(nearest_number)

Output:

88

Example 2: Round the specified negative number to the nearest integer:

nearest_number = round(-87.76432)
print(nearest_number)

Output:

-88

Example 3: Round the specified number to the nearest integer:

nearest_number = round(87.46432)
print(nearest_number)

Output:

87

10. divmod( ) function

The divmod() function returns a tuple containing the quotient and the remainder when the first argument i.e, the dividend is divided by the second argument i.e, the divisor.

Example 1: Display the quotient and the remainder when 7 is divided by 3:

x = divmod(7, 3)
print(x)

Output:

(2, 1)

Example 2: Display the quotient and the remainder when 72 is divided by 6:

x = divmod(72, 6)
print(x)

Output:

(12, 0)

11. id( ) function

The id() function returns a unique id for the specified object. Note that all the objects in Python have their own unique id.

The id is assigned to the object when it is created.

The id is the object’s memory address and will be different for each time you run the program. (except for some object that has a constant unique id, like integers from -5 to 256)

Example 1: Return the unique id of a tuple object:

names_tuple = ('Chirag', 'Kshitiz', 'Dinesh', 'Kartik')
print(id(names_tuple))

Output:

140727154106096

Example 2: Return the unique id of a string object:

string = "Analytics Vidhya is the Largest Data Science Community"
print(id(string))

Output:

140727154110000

12. ord( ) function

The ord() function returns the number representing the Unicode code of a specified character.

Example 1: Return the integer that represents the character “h”:

x = ord("h")
print(x)

Output:

104

Example 2: Return the integer that represents the character “H”:

x = ord("H")
print(x)

Output:

72

13. len( ) function

The len() function returns the count of items present in a specified object.

When the object is a string, then the len() function returns the number of characters present in that string.

Example 1: Return the number of items in a list:

fruit_list = ["apple", "banana", "cherry", "mango", "pear"]
print(len(fruit_list))

Output:

5

Example 2: Return the number of items in a string object:

string = "Analytics Vidhya is the Largest Data Science Community"
print(len(string))

Output:

54

14. sum( ) function

The sum() function returns a number, the sum of all items in an iterable.

If you are a beginner, I think that you don’t have a good understanding of what Iterables and Iterators are. To learn these concepts, you can refer to the link.

Example 1: Start with the number 7, and add all the items in a tuple to this number:

a = (1, 2, 3, 4, 5)
print(sum(a, 7))

Output:

22

Example 2: Print the sum of all the elements of a list:

list = [1, 2, 3, 4, 5]
print(sum(list))

Output:

15

15. help( ) function

The help() function is used to display the documentation of modules, functions, classes, keywords, etc.

If we don’t give an argument to the help function, then the interactive help utility starts up on the console.

Example 1: Check the documentation of the print function in the python console.

help(print)

Output:

Example 1: Check the documentation of the print function in the python console.

Example 2: Check the documentation of the sum function in the python console.

help(sum)

Output:

Example 2: Check the documentation of the print function in the python console.

End Notes

The media shown in this article on Python Built-in functions are not owned by Analytics Vidhya and are used at the Author’s discretion.

Frequently Asked Questions

Q1. What are in-built functions in Python?

A. In-built Python functions are pre-defined functions available for use without importing any external libraries or modules. These functions provide basic functionality and operations used in programming.

Q2. What is a built-in function in Python example?

A. An example of a built-in function in Python is the print() function, which displays output on the console.

Q3. What are the built-in functions?

A. Python has several built-in functions that provide functionality in different areas, such as data type conversion, mathematical operations, string manipulation, file operations, and more. Some examples of built-in functions in Python are len(), str(), int(), float(), range(), max(), min(), sum(), open(), sorted(), and type().

I am a B.Tech. student (Computer Science major) currently in the pre-final year of my undergrad. My interest lies in the field of Data Science and Machine Learning. I have been pursuing this interest and am eager to work more in these directions. I feel proud to share that I am one of the best students in my class who has a desire to learn many new things in my field.

Responses From Readers

Clear

Chaarvi
Chaarvi

Very interesting read!

Chaarvi
Chaarvi

This is a very interesting and insightful read!

hammad shoukat
hammad shoukat

how to only get alphabet as a input in python program?? please guide

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details