# 21 Built-in Python Function That Increases Your Productivity

## What will you learn?

Built-in functions help programmers do complex things they do all the time with one function. Here are the ones you need to know – and some of them I am sure you didn’t know.

## #1 zip()

This is a favorite. If you have multiple lists with connected elements by position, then you can iterate over them as follows.

```words = ['as', 'you', 'wish']
counts = [3, 2, 5]
for word, count in zip(words, counts):
print(word, count)
```

Which gives.

```as 3
you 2
wish 5
```

Also see how this can be done using List Comprehension.

## #2 type()

I don’t really understand how little known this function is. It can help you a lot to understand your code by giving the types of your variables.

```words = ['as', 'you', 'wish']
print(type(words))
a = 3.4
print(type(a))
```

This will give.

```list
float
```

## #3 sum()

Often you need to get the sum of an iterable like a list.

```my_list = [43, 35, 2, 78, 23, 45, 56]
sum(my_list)
```

Printing 282 in this case.

## #4 set()

If you have a list of elements and you only need to iterate over the unique elements, then set is a great built-in function.

```my_list = [3, 2, 6, 3 ,6 ,3 ,5 ,2 ,5, 4, 6, 2, 8, 4, 3]
for item in set(my_list):
print(item)
```

This will print the items 2, 3, 4, 5, 6, 8.

But only once each.

## #5 list()

If you have an iterable and want it as a list.

```my_list = [3, 2, 6, 3 ,6 ,3 ,5 ,2 ,5, 4, 6, 2, 8, 4, 3]
unique_items = list(set(my_list))
```

Then unique_items will be a list with the elements [2, 3, 4, 5, 6, 8].

## #6 sorted()

If you have a list but want a sorted copy of it.

```my_list = [3, 2, 6, 3 ,6 ,3 ,5 ,2 ,5, 4, 6, 2, 8, 4, 3]
sorted_list = sorted(my_list)
```

Which will be [2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 6, 8].

## #7 range()

I love this one and use it all the time. It will give you all the numbers in a range. I often use it with for-loops.

```for i in range(10):
print(i)
```

It will print the numbers 0, 1, …, 9.

You can also generate a list as follows.

```my_list = list(range(10))
```

## #8 round()

When you calculate with floats you often get a lot of digits that you actually don’t need. Then round is a great built-in function.

```pi = 3.1415
print(round(pi, 2))
```

This will give you 3.14 only.

## #9+10 min() and max()

Gives the minimum and maximum value of a list.

```my_list = [3, 2, 6, 3, 6, 3, 5, 2, 5, 4, 6, 2, 8, 4, 3]
print(min(my_list))
print(max(my_list))
```

Which will print 2 and 8.

## #11 map()

If you want to apply a function to all the elements in an iterable like a list.

```def my_func(x):
return 'x'*x
my_list = list(map(my_func, [1, 2, 3, 4]))
```

Here we also convert to a list with the elements from the map function [‘x’, ‘xx’, ‘xxx’, ‘xxxx’].

## #12 isinstance()

Very useful to check the type of a variable.

Here we combine List Comprehension to filter all items of type str (string).

```this_list = ['foo', 3, 4.14, 'bar', 'foobar']
my_list = [item for item in this_list if isinstance(item, str)]
```

Then my_list will contain [‘foo’, ‘bar’, ‘foobar’].

Also check the 7 List Comprehensions to improve your code.

## #13 help()

Wow! Did you know that? You can get help.

```help(isinstance)
```

This will give you.

```Help on built-in function isinstance in module builtins:
isinstance(obj, class_or_tuple, /)
Return whether an object is an instance of a class or of a subclass thereof.

A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to
check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)
or ...`` etc.
```

## #14-17 int(), str(), float(), and bool()

These are so useful and easy to use. They basically convert a variable to the given type.

```a = '13'
# converts to an integer (here the string a)
b = int(a)
# converts to a float (here the string a)
c = float(a)
# converts to a string (here the float c)
d = str(c)
# converts to a bool - an empty list will be False
e = bool([])
# A list with items will be True
f = bool([2, 3, 4])
```

## #18-19 any() and all()

Given a list of items that can be True of False, if you want to check if any element is True or all elements are True.

```a_list = [False, False, True, False, False, False]
if any(a_list):
print('at least one item is True')
if all(a_list):
print('all elements are True in the list')
```

## #20 abs()

If you work with numbers and values, then you must often need to take the absolute value. There is a built-in function that does that for you in Python.

Love to Python.

```a = -12
b = abs(a) # will be 12
c = 12
d = abs(c)  # will be 12
```

## #21 input()

Sometimes you need to interact with the user of your Python program from the command line. I try to avoid it, but sometimes it actually makes sense. Here Python has done it simple to achieve.

```user_input = input('How do you feel?')
print(f'You feel {user_input}')
```

That is cool.

Check out this guide on f-strings (used above) if you want learn about them.

## Final thoughts

The Python built-in functions are actually amazing. I used to program in C, then Java, and some other languages. But the built-in functions in Python just make a lot of simple tasks that are difficult to make in other languages easy to achieve.

Thank you Python.