Useful Built-in Functions

  1. round(): The round() function is used to round a number to a specified number of decimal places.

It takes one required argument, the number to be rounded, and an optional argument that specifies the number of decimal places (default is 0). The function returns a floating-point number.

#Example
num = 3.14159
rounded_num = round(num, 2)
print(rounded_num)  # Output: 3.14
  1. chr(): The chr() function returns a string representing a character whose Unicode code point is the specified integer.

It takes an integer argument and returns the corresponding character.

#Example
unicode_num = 65
character = chr(unicode_num)
print(character)  # Output: 'A'
  1. ord(): The ord() function returns an integer representing the Unicode character.

It takes a single character as an argument and returns the Unicode code point of that character.

# Example
character = 'A'
unicode_num = ord(character)
print(unicode_num)  # Output: 65
  1. len(): The len() function is used to determine the length or size of an object.

It returns the number of items in an object such as a string, list, tuple, or dictionary.

# Example
my_string = "Hello, World!"
length = len(my_string)
print(length)  # Output: 13
  1. min(): The min() function is used to find the minimum value among the given arguments or an iterable object.

It can be used with numbers, strings, or other comparable objects.

# Min Example #1
numbers = [4, 7, 2, 9, 1]
minimum = min(numbers)
print(minimum)  # Output: 1

The min() function can also take multiple arguments separated by a comma to determine the minimum value from variables or multiple data objects.

# Min Example #2
x = 13
y = 11
z = 12
print(min(x, y, z)) # Output is 11 from variable y
  1. max(): The max() function is used to find the maximum value among the given arguments or an iterable object.

It works similarly to min() but returns the maximum value instead. max() can also take multiple arguments as well.

# Max Example
numbers = [4, 7, 2, 9, 1]
maximum = max(numbers)
print(maximum)  # Output: 9
  1. sum(): The sum() function is used to calculate the sum of all elements in an /sequence, such as a list or tuple.

It takes an iterable as an argument and returns the sum of its elements. The items of each iterable must be addable.

# Example
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print(total)  # Output: 15

These are some of the commonly used functions in Python that perform specific operations, such as rounding, character conversions, length calculation, finding minimum and maximum values, and summing elements.

Last updated