Python Built-in Functions: 15 Essential for Data Science

Python is the number one choice of programming language for many data scientists and analysts. One of the reasons for this choice is that python is relatively easier to learn and use. In this post, we will cover important python built-in functions.

1. print( ) function

The print() function is one of the Python built-in functions that prints a message to the screen or another standard output device.

A string or any other object can be used as the message to be printed. Before returning the output, this python built-in functions code turns the object into a string.

Example 1: Write the following string on the screen:

print(“Analytics Arya is the Largest Data Science Community over the whole world”)

Output:

Analytics Arya is the Largest Data Science Community in the whole world

2. type( ) function

The type() function returns the type of the input and is one of Python’s built-in functions.

1st example: The type of the following object should be returned:

list_of_fruits = (‘apple’, ‘banana’, ‘cherry’, ‘mango’)

print(type(list_of_fruits))

Output:

<class ‘tuple’>

3. input( ) function

The input() function is one of the Python built-in functions that allow the user to provide input.

Example: After providing the input, use the prompt parameter to write the entire message:

a = input(‘Enter your name:’)

print(‘Hello, ‘ + a + ‘ to Analytics Arya’)

Output:

Enter your name: Shyam Goyal

Hello, Shyam Goyal to Analytics Arya

4. abs( ) function

The abs() function returns us the absolute value of a number. It is one of Python’s built-in functions.

Example: Return the absolute value of a negative number.

negative_number = -676

print(abs(negative_number))

Output:

676

5. pow( ) function

The pow() function returns the calculated value of x to the power of y and is one of Python’s built-in functions code.

This function returns x to the power of y, modulus z if a third parameter is given.

Example: Return the value of 3 raised to the fourth power:

x = pow(3, 4)

print(x)

Output:

81

6. dir( ) function

The dir() function is one of the Python built-in functions source code that returns all of the supplied object’s properties and methods, but not their values.

Even the built-in properties, which are defaulted for all objects, are returned by this function.

Example: To show the content of an object, do the following:

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 us a sorted list of the input object and is one of Python’s built-in functions.

You can choose whether the order should be ascending or descending. Strings are sorted alphabetically by the function, and numbers are sorted numerically with this function.

NOTE: We are unable to sort a list that comprises both string and numeric data.

Example: Sort the provided letters in ascending order

Letters = (“h”, “b”, “a”, “c”, “f”, “d”, “e”, “g”)

print(sorted(letters))

The output will be given as:

[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’]

8. max( ) function

The max() function is one of the Python built-in functions that return the item with the highest value or the item in an iterable with the highest value.

If the values passed to the max() function are strings, an alphabetical comparison is used.

Example: Return the highest-valued name, in alphabetical order:

names_tuple = (‘Chirag’, ‘Kshitiz’, ‘Dinesh’, ‘Kartik’)

print(max(names_tuple))

Output:

Kshitiz

9. round( ) function

The round() function returns us a floating-point number that is a rounded-off version of the supplied number, with the given number of decimals and is one of the Python built-in functions source code.

The function will return the nearest integer if the number of decimals is set to 0.

Example: Round the provided input to the nearest integer.

nearest_number = round(87.76432)

print(nearest_number)

Output:

88

10. divmod( ) function

When the first parameter, the dividend, is divided by the second argument, the divisor, the divmod() function returns a tuple comprising the quotient and the remainder.

Example: When 7 is divided by 3, show the quotient and the remainder:

x = divmod(7, 3)

print(x)

Output:

(2, 1)

11. id( ) function

The id() function returns a unique id for the supplied object and is one of Python’s built-in functions. It’s worth noting that each object in Python has its own unique id.

When an item is formed, it is given an id.

The id() function is the memory address of the object, and it will be different each time you execute the application. (unless for objects with a fixed unique id, such as integers from -5 to 256)

Example: Return the unique id of a tuple object

names_tuple = (‘Chirag’, ‘Kshitiz’, ‘Dinesh’, ‘Kartik’)

print(id(names_tuple))

Output:

140727154106096

12. ord( ) function

The ord() function is one of the Python built-in functions that returns a number that represents the Unicode code of a character.

Example: Return the integer corresponding to the letter “h”:

x = ord(“h”)

print(x)

Output:

104

13. len( ) function

The len() method is one of the Python built-in dictionary functions that returns the number of items in a given object.

This function returns to us the number of characters present in a string when the object is a string.

Example: The number of items in a list is returned:

fruit_list = [“apple”, “banana”, “cherry”, “mango”, “pear”]

print(len(fruit_list))

Output:

5

14. sum( ) function

The sum() method is one of the Python built-in functions that returns a number that represents the total of all items in an iterable.

Example: Begin with the number 7 and multiply it by all the components in a tuple:

a = (1, 2, 3, 4, 5)

print(sum(a, 7))

Output:

22

15. help( ) function

The help() method is one of the Python built-in functions that displays documentation for modules, functions, classes, and keywords, among other things.

If the help function is called without an argument, the interactive help utility appears on the console.

Example: In the Python console, look up the documentation for the print function.

help(print)

Output:

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

Conclusion

We listed python built-in functions with examples in this article. This list is obviously far from exhaustive, as the Python environment includes numerous other tools that aid in the completion of machine learning tasks and the development of algorithms. Many of these tools will be used by data scientists and software engineers working on Python-based data science projects, as they are necessary for developing high-performing ML models.

Python Built-in Functions: 15 Essential for Data Science

Leave a Reply

Your email address will not be published. Required fields are marked *