The world’s leading publication for data science, AI, and ML professionals.

Python Beginner Breakthroughs (Top Python Built-In Functions)

Why reinvent the wheel? Python has many built-in functions that do some of the things you are currently wrapping your brains around.

Photo by serjan midili on Unsplash
Photo by serjan midili on Unsplash

Python natively has many built-in functions that everyone who wants to become familiar with the programming language should know. In this article, I’ll highlight the most important ones that you may use to develop the code that will power your machine learning or data science project. How often do you find yourself Coding and want to do something that seems simple, but when you start to try and type it out you think ‘There’s got to be a better way’. If this happens to you and the concept is actually really simple, there probably is an easier way, the Python built-in functions. The list below is in no particular order of importance, but I wanted to highlight some of the most commonly used built-in functions that Python has to offer.

One of the beauties of Python is that as the naming of the functions goes, their name pretty much infers its function. Below I’ve broken down the functions it three groups: Basic, Data Type Related, and Advanced

Photo by Hunter Haley on Unsplash
Photo by Hunter Haley on Unsplash

Basic Functions:

abs()

As the name probably infers, the abs() function returns the absolute value of a number. The input can be a variety of numerical inputs.

any()

The any() function works by returning a True/False if the item is an iterable. This can be used as a way to determine data coming from a sequence/iterable type or not.

len()

This is probably one of the most used functions in Python and the len() function returns the number of elements in an object. So if your list has 5 items in it, the len() function will return 5.

# Examples of Basic Functions
# abs() function
x = -24
abs(x)
24

# any() function
y = [0, 1, 2, 3, 4]
any(y)
True

# len() function
len(y)
5

max()

Another really commonly used function and as it implies, returns the max element of an iterable (if you only supply with one argument). There are two other keyword-only aguments that you can supply: key and default. Using the max() function like max(list, key=len) will return the max length (or longest) element in the list.

min()

The min function performs the same functionality as the max() function, but in the reverse direction. Similarly, there are additional keyword-only arguments you can use to make the function more customizable.

print()

Everyone learns this function on their Hello World first day exercise and it allows a user to display items in the text stream.

# max() function
l = [1, 4, 7, 21, 35]
m = [[1, 2, 3], [1, 2], [0, 3, 2, 1]]
max(l)
35
max(m, key=len)
[0, 3, 2, 1]

# min() function
l = [1, 4, 7, 21, 35]
m = [[1, 2, 3], [1, 2], [0, 3, 2, 1]]
min(l)
1
min(m, key=len)
[1, 2]

# print() function
x = 'hello world'
print(x)
hello world

round()

Depending on how you’d like to represent numerical numbers, the round() function enables rounding of numbers to the specified decimal point (with additional arguments)

sum()

The last of the basic functions is sum() which as it names implies will take an iterable and sum the elements together. You can add an additional argument to specify a starting point as well.

#round() function
x = 24.3452
round(x)
24
round(x, ndigits=2)
24.35

# sum() function
z = [1, 3, 5, 10, 51]
sum(z)
70
Photo by Markus Spiske on Unsplash
Photo by Markus Spiske on Unsplash

Advance Functions:

enumerate()

The enumerate() function is one that is really useful to help take a sequnce (like a list) and get back a tuple pair of all the corresponding index and element. You can define with an additional start argument the first tuple elements (the numbering) starting number is. Note the output of enumerate() is an enumerate object and you will need to convert it to a list or other type to see it.

filter()

As the name implies, the filter() function will filter through an iterable based on if the function argument is true. The arguments are function (which is a function that will be applied to the iterator) and iterable (which can be of any iterable or sequence types). Note, a common way to handle the filter argument if it is something simple and non-reused throughout your code is to use a lambda function.

#enumerate() function
k = ['first', 'second', 'third', 'fourth']
z = enumerate(k)
print(list(z))
[(0, 'first'), (1, 'second'), (2, 'third'), (3, 'fourth')]

# filter() function
p = [1, 2, 3, 4, 5]
q = filter(lambda x: x % 2 == 0, p)
print(list(q))
[2, 4]

map()

The map() function evaluates a given function for all items in an iterable and outputs the corresponding iterator. In essence, for example, a list integers will be evaluated with a function that will square the item and yield a new list that contains all the squared integers values.

sorted()

As with many of the more advanced functions, this one is key to help represesnt sequence type data in the desired format. The sorted() function does exactly what it’s name says and sorts the data. There are additional arguments that you can use to build a little more advanced logic in how you want to sort. You supply a key to sort by as well as define reverse = True or False, to reverse the direction of the sort. Note that the function works for both numerical and string types of data

zip()

Working with multiple sequence types and wanting to combine them is possible with the zip() function. Like you would imagine with a zipper on clothes, it takes two iterables and combines them in zipper fashion. The way it does it is by supplying as inputs two iterables and then returning an iterator of tuples. A key thing to note is that you should be careful with iterables of differing lengths as the longer items could create issues. A neat thing is that you can unzip by using the function call zip(*zipped_item) as well.

# map() function
z = [0 , 2, 5, 21]
y = map(lambda x: x ** 2, z)
print(list(z))
[0, 4, 25, 441]

# sorted function
a = ['r', 'd', 'a', 'b', 'z']
print(sorted(a))
['a', 'b', 'd', 'r', 'z']
print(sorted(a, reverse=True))
['z', 'r', 'd', 'b', 'a']

# zip() function
number_list = [1, 2, 3]
str_list = ['one', 'two', 'three']
zipped = list(zip(number_list, str_list))
print(zipped)
[(1, 'one'), (2, 'two'), (3, 'three')]
unzipped = list(zip(*zipped))
print(unzipped)
[(1, 2, 3), ('one', 'two', 'three')]
Photo by Franki Chamaki on Unsplash
Photo by Franki Chamaki on Unsplash

Data Type Related:

type()

The type() function in it’s simplest form returns the type of an object that it is supplied. A 3 argument use of type() creates a new type object. The arguments are name(a class name), bases (a tuple that itemizes the base class), and dict (a dictionary which is the namespace containing definitions for the class body).

int()

The int() function returns an integer object for a number or string that is supplied. Note that for floating point numbers it will truncate towards zero.

str()

Similarly to int() the str() function returns a string object when given an object as an input.

dict()

the dict() function creates a new dictionary based off the inputs supplied.

# type() function
x = 'string'
type(x)
str
x = 2.14
type(x)
float

#int() function
p = 2.21
int(p)
2

# str() function
z = 21
str(z)
'21'

# dict() function
z = dict()
print(z)
{}
# note there are many ways to build dictionaries

Wrap Up

There are tons of already built functions within Python that can perform some basic operations and manipulations of data. By understanding and knowing them you may be able to simply call on one of these functions, rather than code out the procedures necessary. As always, if you have any questions or comments please feel free to respond or comment back! Thanks


Related Articles