How to sum the items of an iterable in Python

The sum() function allows you to sum all of the items of an iterable in Python. Dictionaries, lists, and tuples are examples of iterables. This example sums the numbers 4, 8, 16, 32, 64 of a list. my_list = [4,…

intersection(): checking similarities between Sets in Python

The intersection() method checks the intersection of items between two or more sets, that is, items that exist in all sets. In the example below both sets together have the item ‘bmw’. car_brands_set_one = {'bmw', 'mclaren', 'ferrari'} car_brands_set_two = {'honda',…

How to round a number in Python

The round() function rounds a given number with the specified number of decimals. If you don’t specify the number of decimals, the default will be zero decimals. print(round(32.8476)) print(round(32.8476,1)) print(round(32.8476,2)) print(round(32.8476,3)) 33 32.8 32.85 32.848