Number data types store only numerical values. They are immutable data types, signifies that changing the value of a number data type conclude in a newly assigned object.
Number objects are created when you allocate a value to them. For instance −
var1 = 1 var2 = 10
It is also possible to delete the reference to a number object with the help of delstatement. The syntax of the del statement is as follows −
You can also delete a single object or multiple objects with the help of del statement. For instance −
del var del var_a, var_b
Python supports four various numerical categories which are as follows −
int (signed integers) − They are sometimes called just integers or ints are positive or negative whole numbers with no decimal point.
long (long integers ) − Also known as longs, they are integers of infinite size, written as integers and followed by an uppercase or lowercase L.
float (floating point real values) − Also known as floats, they specify the real numbers and are written along with a decimal point dividing the integer and fractional parts. Floats can also be in scientific notation, with E or e expressing the power of 10 (2.5e2 = 2.5 x 102 = 250).
complex (complex numbers) − are of the type a + bJ, where a and b are floats and J (or j) indicates the square root of -1 (which is an imaginary number). The real part of the number is a, and the imaginary part is b. Complex numbers are not used up to high extent in Python programming.
Lets discuss few illustrations of numbers as follows:
Python permits you to use a lowercase L with long, but it is suggested that you use only an uppercase L to avoid confusion with the number 1. Python displays long integers with an uppercase L.
A complex number contains an ordered pair of real floating point numbers indicated by a + bj, where a is the real part and b is the imaginary part of the complex number.
Number Type Conversion
Python converts numbers internally in an expression consisting mixed types to a common type for evaluation. But sometimes, its important to coerce a number explicitly from one type to another to satisfy the necessities of an operator or function parameter.
Type int(x) to convert x to a plain integer.
Type long(x) to convert x to a long integer.
Type float(x) to convert x to a floating-point number.
Type complex(x) to convert x to a complex number as real part x and imaginary part zero.
Type complex(x, y) to convert x and y to a complex number as real part x and imaginary part y. x and y are numerical expressions
Python involves various functions that perform mathematical calculations described as follows:
|Sr.No.||Function & Returns ( Explanation )|
The absolute value of x: the positive distance between x and zero.
The ceiling of x: the smallest integer greater than or equal to x
-1 if x < y, 0 if x == y, or 1 only if x > y
The exponential of x: ex
The absolute value of x.
The floor of x: the largest integer less than or equal to x
The natural logarithm of x, x> 0
The base-10 logarithm of x ,x> 0.
The largest of its arguments: the value nearest to positive infinity
The smallest of its arguments: the value nearest to negative infinity
The fractional and integer parts of x in a two-item tuple. Both of the parts have the identical sign as x. The integer part is returned as a float.
The value of x**y.
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.
The square root of x for x > 0
Random Number Functions
Random numbers are used for various objectives such as games, simulations, testing, security, and privacy applications. Python containsseveral functions that are mainly used described as follows.
|Sr.No.||Function & Explanation|
A random item from a list, tuple, or string.
|2||randrange ([start,] stop [,step])
An element selected randomly from range(start, stop, step)
A random float r, such that 0 not greater than r and r is less than 1
Sets the integer starting value used in creating random numbers. Call this function prior to calling any other random module function. Returns None.
Randomizes the things of a list in place. Returns None.
A random float r, such that x is not greater than r and r is less than y
Python involves various functions that perform trigonometric calculations described aas follows:
|Sr.No.||Function & Explanation|
Return the arc cosine of x, in radians.
Return the arc sine of x, in radians.
Return the arc tangent of x, in radians.
Return atan(y / x), in radians.
Return the cosine of x radians.
Return the Euclidean norm, sqrt(x*x + y*y).
Return the sine of x radians.
Return the tangent of x radians.
Chnage angle x from radians to degrees.
Change angle x from degrees to radians.
The module also explains two mathematical constants as below−
|Sr.No.||Constants & Explanation|
The mathematical constant pi.
The mathematical constant e.