Built-In Python Functions- Part 3

Built-in Python functions can extend the python code functionality of doing a specific job. We’ve been discussing these built-in python functions in this series of tutorials. You can start from the first part of this series if you didn’t see them. You can then jump into this third part of this series.

1. Filter Function

This filter() function is always applied to iterable Python objects such as lists or dictionaries, and it will create a new iterator. This newly generated iterator can be filtered out of its elements based on a particular condition you provide. Let’s see an example:

# Employee Years of Experience
years = [3, 6, 7, 8, 4, 9]

# Employee Function
def Employee(x):
    # Check Years of Experience
    if x < 5:
        return False
        return True

# Return Experienced Employee
experienced = filter(Employee, years)

# For Loop
for i in experienced:
    # Print The Result

First, we create a list containing employees’ years of experience inside the company. Then we use a function called Employee to check if the years of experience are less than 5 years. It will return False otherwise will return True. 

Then we use the filter() function to return only the years of experience that is more than 5 and store them inside the experienced variable. Finally, loop over the experienced variable elements and show them on the screen.

2. Format Function

This format() function is used for making complex string formatting very efficiently. It is a built-in method for the strings data types in python and can concatenate many parts to make one phrase. Let’s see an example:

# Use The Format Function
print("Hello, {}".format("Abdelhakim"))

This is a straightforward use for the format function. When you execute the code, it will concatenate the word “Abdelhakim” that you see inside the format() function with the word “Hello” and it will be placed in the place of the “{}”. 

This function is extensive and can perform many other things, but we’ve discussed only its basic usage. But you can explore more about this format() function in the python official documentation.

3. Input Function

I think all of the python developers know this function what it does. It can take the input from the user and store it inside a variable. You can use that variable later in your code. Let’s see an example of this input() function:

# Get The Name
name = input("What is your name? ")

# Print The Result

You will need to specify the message the user will get after running the code inside the input() function. It will ask for your name, store it inside the name variable, and print the result in the following line of code.

4. Help Function

You will need to use this help() function a lot from now when you know its functionality. It gives you the documentation of a particular module, function, class, or a specific data type such as a string. Let’s see an example:

# Help() Function

This simple code above shows you how to apply the help() function on the list keyword used to create a list in python. It will show you the documentation of the list and how you can use its method. You can also apply it to a function:

def Employee():
    name = "John"
    age = 33

You can use it also on specific modules of a library, so you know how to use it without the need to keep searching on the internet. See this example:

# Import SQRT Module
from math import sqrt

# Help() Function

We’ve applied the help() function only on the sqrt module of the math library, and it will tell you how you can use it.

5. Hex Function

If you want to get the hexadecimal string representation for any number, you can use the hex() function. Let’s see an example:

# Define a Variable
x = 55

# Hex() Function
y = hex(x)

# Print The Result

You will the value 0x37 as a result, but what if you tried to get the hexadecimal of a float value? You will get a TypeError error telling you that the float object cannot be interpreted as an integer. The solution is using this function:

# Define a Variable
x = 55.5

# Hex() Function
y = float.hex(x)

# Print The Result

So you can change the function from hex() to float.hex(), and everything will work fine.

6. Id Function

Every object in the python language has its id, and it is assigned to it once it is created. It is the object memory address, and to get this id. You will need to use the id() function. See this simple example:

# Define a Variable
x = ["Germany", "USA", "UK"]

# Id() Function
y = id(x)

# Print The Result

You will get a very long number that will change every time you run the code. Some objects with a unique id, such as the integer from -5 to 256, will give you the same id even after running the code many times because it is constant.


Thanks for reading! Built-in python functions are good when you know where to use them and can extend the functionality of your code and make you look more professional in your work. These Built-in python functions also abbreviate the code length you will create.

Scroll to Top