This article entitle Python Number is a continuation of the previous article entitled Python Loops.
Python Numbers are data types that store numeric values and these are immutable data types which means that there will be no changes in the number results in the newly allocated object.
What is a Python number?
In Python, a number refers to a numeric data type and has three in-built numeric data types listed below.
- Floating point numbers
- Complex numbers
An integer is a whole number without any decimal places, it’s a positive and a negative number and has unlimited length.
num1 = 1 num2 = 9732827472724242 num3 = -77382232 print(type(num1)) print(type(num2)) print(type(num2))
<class 'int'> <class 'int'> <class 'int'>
Python Floating Point Number
A floating point number or a float in short is a number that contains single or multiple decimal places.
num1 = 1.20 num2 = 2.0 num3 = -45.65 print(type(num1)) print(type(num2)) print(type(num3))
<class 'float'> <class 'float'> <class 'float'>
Python Complex Numbers
Complex numbers in Python are written with a (j) as an imaginary part of a number.
num1 = 4+6j num2 = 6j num3 = -7j print(type(num1)) print(type(num2)) print(type(num3))
<class 'complex'> <class 'complex'> <class 'complex'>
Number Type Conversion in Python
Python can also convert a number internally into an expression that contains a mixed type to a common type for evaluation. In some instances, you should need to coerce a number explicitly from a single type to another just to satisfy all the requirements of the operator or a parameter function.
- A type int(x) is used to convert an x to a plain integer.
- A type long(x) is used to convert an x to a long integer.
- A type float(x) is used to convert an x to a floating-point number.
- A type complex(x) is used to convert an x to a complex number with a real part x and a number imaginary part to zero.
- A type complex(x, y) is used to convert an x to a complex number with a real part x and an imaginary part y. And the x and y is a numeric expression.
Python Mathematical Functions
Python also includes the following functions which can perform mathematical calculations.
|Function & Returns||Description|
|abs(x)||The absolute value of x: the (positive) distance between x and zero.|
|ceil(x)||The ceiling of x: the smallest integer not less than x|
|cmp(x,y)||-1 if x < y, 0 if x == y, or 1 if x > y|
|exp(x)||The exponential of x: ex|
|fabs(x)||The absolute value of x.|
|floor(x)||The floor of x: the largest integer not greater than x|
|log(x)||The natural logarithm of x, for x> 0|
|log10(x)||The base-10 logarithm of x for x> 0.|
|max(x1, x2,…)||The largest of its arguments: the value closest to positive infinity|
|min(x1, x2,…)||The smallest of its arguments: the value closest to negative infinity|
|modf(x)||The fractional and integer parts of x in a two-item tuple. Both parts have the same sign as x. The integer part is returned as a float.|
|pow(x, y)||The value of x**y.|
|round(x [,n])||x rounded to n digits from the decimal point. Python rounds away from zero as a tie-breaker: round(0.5) is 1.0 and round(-0.5) is -1.0.|
|sqrt(x)||The square root of x for x > 0|
Python Random Numbers Functions
Random numbers are mostly used for games, testing, security, testing, and privacy applications.
The following table below shows functions that are commonly used in development.
|choice(seq)||A random item from a list, tuple, or string.|
|randrange ([start,] stop [,step])||A randomly selected element from range(start, stop, step)|
|random()||A random float r, such that 0 is less than or equal to r and r is less than 1|
|seed([x])||Sets the integer starting value used in generating random numbers. Call this function before calling any other random module function. Returns None.|
|shuffle(lst)||Randomizes the items of a list in place. Returns None.|
|uniform(x, y)||A random float r, such that x is less than or equal to r and r is less than y|
Python Trigonometric Functions
The following table below are functions that commonly use for trigonometric calculations.
|acos(x)||Return the arc cosine of x, in radians.|
|asin(x)||Return the arc sine of x, in radians.|
|atan(x)||Return the arc tangent of x, in radians.|
|atan2(y, x)||Return atan(y / x), in radians.|
|cos(x)||Return the cosine of x radians.|
|hypot(x, y)||Return the Euclidean norm, sqrt(x*x + y*y).|
|sin(x)||Return the sine of x radians.|
|tan(x)||Return the tangent of x radians.|
|degrees(x)||Converts angle x from radians to degrees.|
|radians(x)||Converts angle x from degrees to radians.|
Python Mathematical Constants
The following modules can also define the two mathematical constants
|pi||The mathematical constant pi.|
|e||The mathematical constant e.|
In summary, you have read about Python Number. We also discussed in this article what is a Python number, integer, floating point, complex numbers, number type conversion, mathematical functions, random numbers functions, trigonometric functions, and mathematical constants.
I hope this article about Numbers in Python programming language could help you a lot to continue pursuing learning this powerful programming language.
If you want to learn more check out my previous and latest articles for more career-changing articles.