We have the answers to your questions! - Don't miss our next open house about the data universe!

Top 10 native Python functions

- Reading Time: 4 minutes
Top 10 native Python functions

In this article, you'll discover a dozen native Python functions (buit-in) that are sure to come in very handy! Just starting out in Python programming?

You’ll find all the Python programming basics in our article.
As a reminder, native functions are functions that do not require the import of an additional library in order to be used.

1-Format:

The native format function inserts variables you specify into a string. The function takes the variables to be inserted as arguments (in order). And since an example is better than a long explanation :

The braces within the string specify the place where we wish to insert the variables.

This can be particularly useful when calling up a user implementation via the input function, for example.

2-ISINSTANCE

The braces within the string specify the place where we wish to insert the variables.

This can be particularly useful when calling up a user implementation via the input function, for example.

Practical example: this can be very useful for cleaning up lists of variables to remove numbers, or strings to keep only numbers.
In the example below, the line number is contained in a list in which you wish to keep only strings.

The TYPE () function is also useful for this purpose. It returns the type of variable you specify.

3.DICT, LIST, TUPLE, SET

  • Python’s native function list lets you create a list, a type of object in Python. A list can contain many different objects. For example, you can create a list of integers, a list of lists or a list of dictionaries. A list is ordered and mutable, so you can add and remove objects of any type. List elements can also be modified. In terms of syntax, lists are enclosed in square brackets.
  • The tuple function is used to create a tuple, a type of object in Python. Unlike lists, tuples are enclosed in parentheses.
  • The dict function is used to create a dictionary, another type of object in Python. This object resembles a list, but its elements are not ordered (there is no index associated with a dictionary). A dictionary contains keys and associated values. The object is mutable and each key is unique. Key-value associations create pairs. You can therefore obtain the value associated with a key simply by calling it up. It’s a very practical object! In terms of syntax, the dictionary is surrounded by curly braces, and a colon “:” is used to assign a value to a key.
  • The set function is used to create a set, yet another type of object in Python. Like a list or tuple, a set can contain objects. However, it has a few specific features that can be very useful, depending on the problem you’re facing. Each element of a set is unique, so there are no duplicates. Set elements are immutable, and cannot be modified. On the other hand, a set is itself mutable. You can therefore add or remove elements from a set. A little unclear? Here’s an example to clear things up!

Another interesting application is to use the function on a list to obtain a list of unique elements.

4.ENUMERATE

The enumerate function is used to enumerate the elements contained in an iterable object (list, tuple…) by value and indexing. The function can take up to two arguments. The first corresponds to the iterable object on which you wish to iterate, the second (optional) corresponds to the index at which you wish to start iteration: start=0 is the default value if you don’t specify this argument.

5.MAP

The map function executes a function on each element contained in an object. It therefore takes at least two arguments. The first corresponds to the function you wish to apply and the second to the object concerned. If the function involves several objects, you need to specify the objects in succession as arguments.

6.MAX

The max function returns the maximum value of an object containing several elements, which it takes as an argument.

7.MIN

The min function returns the minimum of an object containing several elements, which it takes as an argument.

8.ROUND

The round function returns the rounded value of a number (integer or float). The function takes as argument the variable to be rounded and the number of digits after the decimal point to be retained. If you don’t specify the second argument, the default number is 0 and the function returns an integer, so there’s no decimal. Otherwise, the function returns a float.

9.SORTED / REVERSED

The sorted function is used to sort the elements of an object. It takes the object as argument. It can also take as argument a function (key=) to determine the sorting order (by default, no function is used). Finally, it can also take the reverse argument, which specifies whether sorting is done in ascending or descending order; by default, sorting is done in ascending order (reverse=False).

The reversed function reverses the position of elements in an iterable object. It takes the iterable object as argument. The output will be a reversed object, which you can convert into the object of your choice (list, tuple).

10.ZIP

The zip function is used to associate the elements of two or more objects. The function returns a zipped object that can be converted into a list or tuple. The function associates elements in pairs, so it’s best to use iterable objects if you don’t want to end up with any surprises in terms of association. The function takes as arguments the objects you wish to associate; the minimum number of arguments is 1.

An interesting use of the zip function can also be within the same object. If a list is a list of lists, then these lists can be zipped. To do this, simply add an asterisk before the name of the argument object. The output is a zipped object that can be converted into the object of your choice (list, tuple), while pairs are tuples, which can also be converted.

 

💡Related articles:

Folium: Discover the open source Python library
Matplotlib: Master Data Visualization in Python
Python Crash Course: Get started
Mastering Machine Learning in Python: Data-Driven Success
Python Programming for Beginners – Episode 3
Django: All about the Python web development framework

Did you like this article? Would you like to learn to program in Python? Start one of our data science courses soon. Make an appointment to discuss it with a member of our admissions team.

You are not available?

Leave us your e-mail, so that we can send you your new articles when they are published!
icon newsletter

DataNews

Get monthly insider insights from experts directly in your mailbox