Built-In Python Functions- Part 2

B

Built-in Python functions have been discussed in the previous one as it is functions created with the python language, and you can use them to extend the python code functionality and make the work much more manageable. Let’s learn some of the other built-in python functions in this post.

1. Bool Function

This bool() function will return the boolean value of a specific object, either a number, a list, value, word, or anything you want to see its boolean. And it will return either True or False. Let’s see this example:

# Boolean Example
print(bool(0))
print(bool(0.0))
print(bool([0]))
print(bool([]))
print(bool(None))
print(bool("Thanks"))
print(bool(True))
print(bool(False))
print(bool(55))

We’ve used many examples in this code, and you need to only run the code in your Jupyter Notebook, for example, and see the result. You will know which one is True and which one is False.

2. Chr Function

This chr() function returns the character or string representing the specified Unicode. Let’s see an example:

# Define a Variable
x = 97

# Chr() Function
y = chr(x)

# Print The Result
print(y)

Running the code will give you the “a” representing the value 97. But you need to know that this function has a valid range which starts from 0 to 1114111. If the given number is outside the range, it will give you an error, or if the number is negative also.

3. Ord Function

This ord() function reverses the previous one, the chr() function. It will return the number or integer representing the given character. Let’s see this example:

# Define a Variable
x = "g"

# Ord() Function
y = ord(x)

# Print The Result
print(y)

You will get the answer as 103, which is the integer representing the “h” character. Try other characters and rerun the code.

4. Dict() Function

This dict() function is the abbreviation of the word dictionary. It will create a python dictionary that is unordered, changeable (You can change its elements), and can be indexed. Let’s see an example:

# Create a Dictionary
x = dict(job = "Data Scientist", age = 24, name = "John")

# Print The Result
print(x)

The first word is always the key and the second one is the value. We’ve discussed more on the dictionaries in the blog and their methods.

5. Dir Function

The python objects have their properties and methods, and if you need to know them, you have to use the dir() function. These methods and properties are built-in python language by default. Let’s see an example:

# Define Some Variables
a = "John"
b = 33
c = ["Germany", "USA"]
d = {'job': 'Data Scientist', 'age': 24, 'name': 'John'}

# Get The Properties & Methods
dir(x)

We’ve created many different variables in this previous code, and we only applied the dir() function on the first variable, x, a string. You will get a long output and get confused about which method belongs to which data type.

6. Divmode Function

This divmode() function will return a tuple of the quotient and the remainder of the division operation. Let’s see a simple example:

# Divmode() Function
a = divmod(10, 3)

# Print The Result
print(a)

You will get a tuple (3, 1) where the 3 is the quotient and the value 1 is the remainder of this operation. The first value of the division operation, which is 10, is called the dividend, and the second value, which is 3, is called the divisor.

7. Enumerate Function

This enumerate() function took a collection such as a tuple and returned it as an enumerate object. It means it will add a counter to this object starting from 0. Let’s see an example:

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

# Enumerate() Function
y = enumerate(x)

# Print The Result
print(list(y))

We’ve created a list first. Then we applied the enumerate function to this list. Finally, print the result as a list, and you will see the object counted starting from 0. You can change the count starting value from 0 to any other number. Let’s see an example:

# Define a Tuple
x = ("Germany", "USA", "UK")

# Enumerate() Function
y = enumerate(x, 1)

# Print The Result
print(list(y))

We’ve specified the value 1 in the enumerate function to start counting from 1, but it is optional. This value would be 0 by default if you didn’t specify it. Let’s see also how to use the enumerate() function with the for loop:

# Define a Tuple
x = ("Germany", "USA", "UK")

# For Loop
for count, countries in enumerate(x, 1):
    # Print The Count & Countries
    print(count, countries)

We all know that for loop takes only one argument, but when you use the enumerate() function, it will take two, which are count and countries in this example. The count variable is the number to start counting, and we specify it to start from one in the enumerate() function. The countries are the countries’ values in the x variable.

8. Eval Function

This eval() function allows python developers to evaluate the arbitrary python code as a string-based input. Meaning it will see if that command can be executed or not. If it can be executed, then it will run on your IDE. Let’s see this simple example:

# Dfine a Variable
value = 4

# Eval() Function
mul = eval("value * 2")

# Print The Result
print(mul)

The eval() function will execute the string inside it, which is the multiplication operation since it can be executed.

Conclusion

Thanks for reading! Built-in python functions are fantastic when you know where you use them inside your code to make you more productive and make the work much more manageable. We will discuss more built-in python functions in the following tutorials.

Built-In Python Functions- Part 3
Built-In Python Functions- Part 1

Add comment

Tutor Network

en English
X

Pin It on Pinterest