In Python, numbers are a fundamental data type and they can be used to perform various operations and calculations. Python provides several built-in functions that make it easier to work with numbers. In this article, we will discuss some of the most commonly used number functions in Python.
abs(): This function returns the absolute value of a number.
For example:
print(abs(-5)) # Output: 5
round(): This function rounds a floating-point number to a specified number of decimal places.
For example:
print(round(3.14159265, 2)) # Output: 3.14
int(): This function converts a number to an integer.
For example:
print(int(3.14)) # Output: 3
float(): This function converts a number to a floating-point number.
For example:
print(float(3)) # Output: 3.0
complex(): This function creates a complex number.
For example:
print(complex(3, 4)) # Output: (3+4j)
divmod(): This function returns the quotient and remainder of a division operation as a tuple.
For example:
print(divmod(10, 3)) # Output: (3, 1)
pow(): This function returns the result of a power operation.
For example:
print(pow(2, 3)) # Output: 8
min() and max(): These functions return the minimum and maximum value from a collection of numbers, respectively.
For example:
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3] print(min(numbers)) # Output: 1 print(max(numbers)) # Output: 9
In conclusion, these are some of the most commonly used number functions in Python. By understanding and using these functions, we can perform various operations and calculations with numbers in a straightforward and efficient manner.