Built-In Python Functions- Part 6

We’ve discovered tons of these built-in python functions that can help you extend the code functionality and improve your work. We will complete discovering some of them in this part. Start learning from the first part of this series and then jump into the other ones before completing this part.

1. Round Function

This round() function is used a lot when you need to use math in some situations. It is used to round the result of the operation, and it will return a floating-point value which is a rounded version of the defined number, like 2 numbers after the comma. Let’s see an example:

# Define a Variable
x = 22.567

# Round() Function
rounded = round(x)

# Print The Result
print(rounded)

When you run the code, it will print only the value 23 because 22.567 is close to the value 23 than 22. But we can specify another optional argument to the round() function called ndigits, which will show a defined number of values after the comma. Let’s see this example:

# Define a Variable
x = 22.567

# Round() Function
rounded = round(x, 2)

# Print The Result
print(rounded)

It will print the value 22.56 because we told the round() function to print two values after the comma, and you can change that value of ndigits if you want.

2. Type Function

This type() function is mainly used for debugging purposes because it will return the type of the given object passed as an argument. The function can tell you if this variable is a list, class, tuple, etc. Let’s see an example:

# Define a Variable
x = [45, 35, 78]
y = "Welcome"
z = 44
a = (36, 74, 23)
b = {1:"a", 2:"b"}

# Print The Result
print(type(x))
print(type(y))
print(type(z))
print(type(a))
print(type(b))

We defined a few different data types, and that function could recognize all of them just by passing them as an argument.

3. Zip Function

This zip() function will return a zip object. The zip object is considered an iterator of tuples where the first item of this tuple is the pair of the first items of both passed iterator. Let’s see an example of this to understand more about this function:

# Define Some Variables
countries = ("Germany", "USA", "UK")
numbers = (1, 2, 3)

# Zip() Function
x = zip(numbers, countries)

# Print The Result
print(list(x))

You will see the first value, “Germany” is in the same tuple with the value 1 and the second word, “USA” with the second value 2 of the numbers tuple and so on.

4. Sorted Function

The sorted() function is used with the list, tuples data types to sort the values. It can be sorted in ascending or descending order. The strings are sorted alphabetically. Let’s see an example:

# Define Some Variables
countries = ("Germany", "USA", "UK", "Finland", "Canada")
numbers = (24, 43, 61, 1, 467, 888)

# Sorted() Function
sorted_countries = sorted(countries)
sorted_numbers = sorted(numbers)

# Print The Result
print(sorted_countries)
print(sorted_numbers)

It will sorte the first one alphabetically stating from “Canada” since it started with the letter C, which is the first one compared to the first letter of the other values. The second tuple will be sorted ascendingly, starting from value 1.

If you want to reverse the order from ascending to descending, you can use an argument inside the sort() function called reverse. Let’s see this example:

# Define Some Variables
countries = ("Germany", "USA", "UK", "Finland", "Canada")
numbers = (24, 43, 61, 1, 467, 888)

# Sorted() Function
sorted_countries = sorted(countries, reverse=True)
sorted_numbers = sorted(numbers, reverse=True)

# Print The Result
print(sorted_countries)
print(sorted_numbers)

We’ve specified the value True for the reverse argument, and it will change the sort order from ascending to descending. The default one is False even if you didn’t specify like we did the first time since it is optional.

5. Complex Function

This complex() function is a built-in python function that will return a complex number which is in the form of (real + imaginary) like this one (6+2j). Let’s see an example:

# Define Some Variables
x = complex(3, 5)
y = complex(4, -7)
z = complex()

# Print The Result
print(x)
print(y)
print(z)

The code prints the values as (3+5j), (4–7j), and the last one is an empty complex() function 0j. If you add a string-based value as the first parameter, you shouldn’t pass a second, imaginary value. This will raise a TypeError. Let’s see this example:

# Define a Variable
x = complex("3")

# Print The Result
print(x)

It prints the value (3+0j), so the second value is 0 by default if the first value is string-based. Try not to specify a second value like the previous example because you will have a TypeError message. 

6. Map Function

This map() function takes iterable items and applies them to a function you create using the def() keyword. The iterable object can be a list, tuple, etc. Let’s see this example:

# Define a Function
def lenght(x):
    return len(x)

# Map() Function
x = map(lenght, ("Germany", "USA", "UK"))

# Add The Lenght Values To a List
lenght = list(x)

# Print The List
print(lenght)

It will take every item of the tuple, which are the country’s names, then apply them to the length() function and calculate the length of every item. The function will return the calculated value, store it in a list, and print it on the screen.

Conclusion

Thanks for reading! In these six parts of the built-in python functions tutorials, it’s been a long journey because we’ve discovered tons of them. There are other built-in python functions, but I will let you complete discovering them since we’ve covered most of them in this series.

Scroll to Top