Built-In Python Functions- Part 5

Built-in Python functions have been discussed in a series of tutorials in this blog. Also, we’ve seen how you can benefit from these functions to extend your python code functionality and make your work much easier and more productive.

1 Str Function

This str() function is also similar to the previous two int(), and float() functions that we’ve learned in the previous part, but instead of converting a string-based value to a real value, it will convert a real value into a string. Let’s see this example:

# Convert String Value
x = str(3)

# Print The Result
print(x)

The value x now is a string and if you want to check, try to apply any mathematical operation on the value x, and you will get a TypeError error message.

2. Len Function

This len() function returns the number of items inside the object, such as a list and tuple. Also, you can calculate the number of characters in a string. Let’s see an example:

# Define Some Variables
x = ["Germany", "USA", "UK"]
y = "John"

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

When you execute the code, you will get the result 3 for the first object, the list, since there are 3 items inside the list. The second result will be 4 since there are 4 characters in the “John” string.

3. Max Function

The max() function can be used to get the most significant value inside an iterable object, such as a list. If the given values are strings, an alphabetical comparison will be done, meaning it will return the string that the first character is the last one alphabetically. Let’s see a simple example:

# Define Some Variables
x = ["Germany", "USA", "UK"]
y = [22, 44, 63, 11, 5]

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

It will return “USA” for the first object since the string starts with the letter U. The second object will return 63, the highest value in the list. You can also provide numbers inside the max() function or string, and it will work properly.

4. Min Function

This min() function is similar to the max() function, but it will return the minimum value instead of the highest one. See the following example:

# Define Some Variables
x = ["Germany", "USA", "UK"]
y = [22, 44, 63, 11, 5]

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

The first one will return “Germany” since it starts with the letter G, and it is the first one alphabetically if we compare it with the other string-based values inside the list. The second list will return the number 5. You can also provide the values inside the min() function directly.

5. Pow Function

This pow() function is used for mathematical calculation, calculating the power of a value. It accepts three values but let’s see how you can use the simple form of this function:

# Define Some Variables
x = 4
y = 3

# Print The Result
print(pow(x, y))

It will return the value 64, which is the value x, to the power of y. You can also calculate it as x**y in python code:

# Define Some Variables
x = 4
y = 3

# Print The Result
print(x**y)

You will get the same result as the previous one. Now let’s provide the third value for the pow() function, which is optional and called the modulus:

# Define Some Variables
x = 4
y = 3
z = 6

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

You will get the result as 4 because 64 divided by 6, which is the value of z will give you 10, and the remainder of this operation is 4, which is the value you will get after running the code.

6. Range Function

This range() function is often used among python developers who can generate a sequence of numbers starting from 0 and stopping at the given number. Let’s see an example:

# Generate a Sequence of Numbers
print(list(range(10)))

You will get a sequence of numbers starting from 0, and it stops at 9 because it is ten numbers. Remember always that the last number is not included. You can also use the range() function with the for loop:

# Use Range() Function With For Loop
for a in range(5):
    print(a)

It will loop over the numbers from 0 to 4 and print them on the screen. There are other two arguments you can specify in the range() function. The first one is the starting number which is 0 by default, and the other one is the increment value which is 1 by default. Let’s change these two values:

# Use Range() Function With For Loop
for a in range(2, 31, 4):
    print(a)

It will start counting from the number 2 instead of the default one, which is 0, and it will stop at usual at the value 31, but the increment value will be 4 instead of the default one, which is by 1.

7. Sum Function

This sum() built-in python function will sum the values in an iterable such as a list or tuple. Let’s see this example:

# Define Some Variables
x = (33, 44, 14)
y = [24, 64, 78]

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

We’ve defined a tuple and a list, and we’ve used the sum() function to sum the values and print the final result.

Conclusion

Thanks for reading! We’ve explored a lot about the built-in python function, and if you explored all the previous parts of this series of tutorials, you would learn a lot about these built-in python functions, but there are more others to explore.

Scroll to Top