Python Built In Functions Python

Aug 20th, 2021 - written by Kimserey with .

Python comes with a handful of built in functions provided by the standard library. In today’s post, we will look some of them which are very useful in day to day programming.

Built-in Functions

The following list contains the functions I have used the most for the past year:

  • abs
  • all
  • any
  • divmod
  • enumerate
  • len
  • max
  • min
  • range
  • sum
  • zip

Let’s look at each one of them in order with examples.

abs

abs is the absolute function, turning numbers to absolute values.

1
2
3
4
5
>>> a = -1
>>> a = abs(a)

>>> a
>>> 1

all

all takes an iterable and returns a boolean which is True if all elements are True, else False.

1
2
3
4
>>> a = [1, 2, None, 3, 4]

>>> all([v != None for v in a])
>>> False

any

any takes an iterable and returns a boolean which is True if any element is True, else False.

1
2
3
4
>>> a = [1, 2, None, 3, 4]

>>> any([v != None for v in a])
>>> True

divmod

divmod returns the division and the modulo in one operation.

1
2
3
4
>>> help(divmod)

divmod(x, y, /)
    Return the tuple (x//y, x%y).  Invariant: div*y + mod == x.

x//y would be the integral part of the division and x%y would be the modulo.

1
2
>>> divmod(5, 3)
>>> (1, 2)

enumerate

enumerate takes an iterable and returns an iterable which returns the index plus value on each iteration.

1
2
3
4
5
6
7
8
9
10
11
>>> a = ["a", "b", "c"]

>>> a
>>> ['a', 'b', 'c']

>>> for idx, ch in enumerate(a):
         print(idx, ch)
    
0 a
1 b
2 c

len

len will return the length of an iterable. Useful for lists but also dictionaries as it would return the size of the dictionary.

1
2
3
4
5
>>> len([1, 2, 3])
>>>  3

>>> len({'a': 1, 'b': 2})
>>> 2

max

max will return the maximum value in an iterable. A common example is keeping track of the maximum value:

1
max_val = max(max_val, next_val)

min

min will return the minimum value in an iterable.

1
min_val = min(min_val, next_val)

range

range returns a sequence from the start to stop specified or if not specified from 0 till stop.

1
2
3
4
5
6
7
8
>>> list(range(0, 10))
>>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> list(range(10))
>>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> list(range(9, -1, -1))
>>> [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

As we can see we can also use range to return a sequence of element descreasing toward 0.

sum

sum will return the sum of all values in an iterable.

1
2
>>> sum([1, 2, 3])
>>> 6

zip

zip will combine two list stopping when the first list is exhausted.

1
2
3
4
5
6
7
8
9
10
>>> a = [1, 2, 3, 4, 5, 6]

>>> b = ["a", "b", "c"]

>>> for x, y in zip(a, b):
        print(x, y)
    
1 a
2 b
3 c

zip combined with range is very useful to traverse diagonals starting from each corner.

For example if we imagine a matrice 4x4:

       
(0, 0) (0, 1) (0, 2) (0, 3)
(1, 0) (1, 1) (1, 2) (1, 3)
(2, 0) (2, 1) (2, 2) (2, 3)
(3, 0) (3, 1) (3, 2) (3, 3)

Get the diagonal from top left to bottom right:

1
2
3
4
5
6
>>> for  x, y in zip(range(0, 4), range(0, 4)):
         print(x, y)
0 0
1 1
2 2
3 3

Get the diagonal from top right to bottom left:

1
2
3
4
5
6
>>> for  x, y in zip(range(0, 4), range(3, -1, -1)):
         print(x, y)
0 3
1 2
2 1
3 0

Get the diagonal from bottom left to top right:

1
2
3
4
5
6
>>> for  x, y in zip(range(3, -1, -1), range(0, 4)):
         print(x, y)
3 0
2 1
1 2
0 3

Or lastly get the diagonal from bottom right to top left:

1
2
3
4
5
6
>>> for  x, y in zip(range(3, -1, -1), range(3, -1 , -1)):
         print(x, y)
3 3
2 2
1 1
0 0

And that concludes today’s post!

Conclusion

Today we looked at a bunch of built in functions provided by Python standard library. By making sure we are aware of those, we can reduce the complexity of our algorithm by leveraging them. I hope you liked this post and I see you on the next one!

External Sources

Designed, built and maintained by Kimserey Lam.