15 Python Built-in Functions which You Should Know while learning Data Science
This article was published as a part of the Data Science Blogathon
In my previous article, I completely discuss all the basic fundamentals of Python Functions. If you did not read that article, then you may refer to the link. So, In continuation of that article, In this article, we will be discussing some of the most useful and important built-in Python Functions.
Note: If you are more interested in learning concepts in an Audio-Visual format, So to learn the basic concepts of Python Functions you may see this video.
To follow this article properly, I assume you are familiar with the basics of Python. If not, I recommend the below popular course given by Analytics Vidhya to get started with the Python Basics:
Python Built-in Functions
The built-in Python Functions which we are going to discuss in this article are as follows:
- print( ) function
- type( ) function
- input( ) function
- abs( ) function
- pow( ) function
- dir( ) function
- sorted( ) function
- max( ) function
- round( ) function
- divmod( ) function
- id( ) function
- ord( ) function
- len( ) function
- sum( ) function
- help( ) function
Image Source: Link
Now, let’s discuss each of the above functions one-by-one with some examples:
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")
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")
Analytics Vidhya Chirag Goyal Data Scientist Machine Learning
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))
Example 2: Return the type of the following objects:
variable_1 = "Analytics Vidhya" variable_2 = 105 print(type(variable_1)) print(type(variable_2))
<class 'str'> <class 'int'>
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')
Enter your name:Chirag Goyal Hello, Chirag Goyal to Analytics Vidhya
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))
Example 2: Return the absolute value of a complex number:
complex_number = 3+5j print(abs(complex_number))
The absolute value of a complex number is defined in the following way:
Image Source: Link
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)
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)
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))
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))
['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))
['h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']
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))
Example 2: Return the item in a tuple with the highest value:
number_tuple = (2785, 545, -987, 1239, 453) print(max(number_tuple))
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)
Example 2: Round the specified negative number to the nearest integer:
nearest_number = round(-87.76432) print(nearest_number)
Example 3: Round the specified number to the nearest integer:
nearest_number = round(87.46432) print(nearest_number)
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)
Example 2: Display the quotient and the remainder when 72 is divided by 6:
x = divmod(72, 6) print(x)
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))
Example 2: Return the unique id of a string object:
string = "Analytics Vidhya is the Largest Data Science Community" print(id(string))
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)
Example 2: Return the integer that represents the character “H”:
x = ord("H") print(x)
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))
Example 2: Return the number of items in a string object:
string = "Analytics Vidhya is the Largest Data Science Community" print(len(string))
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))
Example 2: Print the sum of all the elements of a list:
list = [1, 2, 3, 4, 5] print(sum(list))
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.
Example 2: Check the documentation of the sum function in the python console.
Other Blog Posts by Me
You can also check my previous blog posts.
Here is my Linkedin profile in case you want to connect with me. I’ll be happy to be connected with you.
For any queries, you can mail me on Gmail.
Thanks for reading!
I hope that you have enjoyed the article. If you like it, share it with your friends also. Something not mentioned or want to share your thoughts? Feel free to comment below And I’ll get back to you. 😉