Built-In Python Functions- Part 1

Built-in python functions are developed with this language, and it is used to extend the python language functionality. There are many built-in functions, and we will discuss some of them in this part of a series of tutorials, and we will leave the other ones in the following one.

1. Abs Function

This abs() function will get a number and return its absolute value like if you gave it a negative value, it would return it to a positive number. Let’s see an example:

# Define a Variable
x = -66

# Convert To The Absolute Value
y = abs(x)

# Print The Absolute Value
print(y)

We’ve executed the abs() function on the Jupyter Notebook cell to return the negative value, which is -66, to its absolute value, which is a positive value. The function is som simple and doesn’t require any other arguments.

2. All Function

This all() function will return True if all of the iterable items inside a python list, for example, are True. Otherwise, it will return False if only one of them is not True. Also, if it is empty, it will return True. Let’s see an example:

# Create a List
x = [True, True, True]

# All() Function
y = all(x)

# Print The Result
print(y)

You can see that all of the values are True, which means it will return True in the final result. We’ve used the True value in this example, but you can use numbers also. Let’s see an example:

# Create a List
x = [0, 33, 66]

# All() Function
y = all(x)

# Print The Result
print(y)

This time it will return False. Could you guess why? The number 0 is considered False, which means one of the values is False, and the final one will also be False. All other values, either negative or positive, are considered True. It also works on the tuples and dictionaries. Let’s see an example:

# Create a Dictionary
x = {0 : "Germany", 1 : "USA"}

# All() Function
y = all(x)

# Print The Result
print(y)

Running this code will give you the False result because the first value is 0. It will look for the values, not the keys inside the dictionary.

3. Any Function

This any() function is the same as the previous one, but it will return True even if one of the elements is False. But if all the elements are False, it will definitely return False. Let’s see an example:

# Create a List
x = [0, 33, 66]

# Any() Function
y = any(x)

# Print The Result
print(y)

Although the first value is False, 0, you will get the True value. Change all values to 0 and try to rerun the code to see the new result.

4. Bin Function

This bin() function is the abbreviation of the word binary. It will convert the regular numbers to their binary equivalent. Let’s see this simple example:

# Bin() Function
x = bin(33)

# Print The Result
print(x)

You will get the value 0b100001 which is the binary representation of the value 33, and you should only specify the value inside the bin() function. You can also specify the negative value, but it will give you the same binary value but in negative.

5. Bytearray Function

This bytearray() function will return the bytearray of the given object. This object can be a number, character, string, list, etc. See this example:

# Create a String (Text)
Text = "Thanks for your help"

# Bytearray Function
Result = bytearray(Text, 'utf-16')

# Print The Result
print(Result)

You can use many other types of encoding, not only “utf-16”. If you are going to convert a number, you don’t have to specify the encoding, and you won’t get an error.

6. Callable Function

Some of the objects in the python language are callable, meaning you need to call them to use them. For instance, the functions are callable and also classes. If you want to know if that specific object is callable, you need to use the callable() function. Let’s see an example:

# Define a Variable
x = 5

# Define a Function
def mul(x):
    a = x * 2
    return a

# Define a Class
class Employee():
    Name = "John"
    age = 24
    
# Check The Callable Objects
print(callable(x))
print(callable(mul))
print(callable(Employee))

You can see after executing the code the variable is not callable since you got the False result. But for the other two objects, function and class, you will get True because they are callable, and you need even to call them in your python code to use them.

7. Bytes Function

This bytes() function is the same as the previous one, which is the bytearray() function, but this bytes() will return an immutable bytes objects. The previous one is returning an object that can’t be modified. Let’s see an example:

# Create a String (Text)
Text = "Thanks for your help"

# Bytes Function
Result = bytes(Text, 'utf-16')

# Print The Result
print(Result)

You see that it is the same code as the previous one, but you will get an immutable object in the result, and you can use any encoding you want. It also accepts numbers or any object like the previous one.

Conclusion

Thanks for reading! I hope you’ve liked this article. There are a lot of built-in python functions that we couldn’t discuss them his post because it will be very long, and we will discuss the other built-in python functions in the following tutorial.

Scroll to Top