The number data types are used to store the numeric values inside the variables. Number objects are created when some value is assigned to a variable. For example, a = 5 will create a number object a with value 5.
Python also allows us to delete the reference of a number by using the del keyword. For example, del a will delete the object a declared above.
In Python, there are four types of numbers that can be assigned to a variable given as follows.
- Int (signed Integer object) : they are the negative or non-negative numbers with no decimal point. There is no limit on an integer in python. However, the size of the integer is constrained by the amount of the memory our system has.
Python also provides the support for various number systems like binary, hexadecimal, and octal values. To store values in different number systems, consider the following pattern.
||0b10 = 2 (decimal)
0b1010 = 10 (decimal)
0B101 = 5 (decimal)
||0o10 = 8
0o12 = 10
||0xA = 10
0xB = 11
0XBE = 190
- float (floating point numbers) :
The float type is used to store the decimal point (floating point) numbers. In python, float may also be written in scientific notation representing the power of 10. for example, 2.5e2 represents the value 250.0.
- Complex (complex numbers)
Complex numbers are of the form a+bj where a is the real part of the number and bj is the imaginary part of the number. The imaginary i is nothing but the square root of -1. It is not as much used in the programming.
Number type conversion
The various types of numbers like int, float, complex are also as the functions in python. It is similar to the wrapper classes of Java which is mostly used for typecasting. However, python internally perform type casting in the expression evaluation and binds the values to their actual types but there are some cases where we may have to define the data type of a value explicitly.
For example, while accepting the value from the user using the input() function, we have to convert the entered string by the user to integer explicitly if we need an integer value.
There are the following functions which are used to perform type conversion.
- int (a) converts a to integer.
- float(a) converts a to float.
- complex(a) converts a to complex.
- complex (a,b) converts a and b to complex numbers with real part a and imaginary part b.
Python Number functions
There are various in-built functions which can be directly used to perform various calculations on the numbers defined in the program.
||The (positive) distance between x and 0.
||The ceiling value of x, i.e., the smallest integer that is not less than x.
||Compares x and y. It returns 0 if x == y, -1 if x<y, 1 if x > y.
||The exponent of x that is ex.
||The absolute value of x.
||The floor value of x, i.e., the greatest integer that is less than x.
||The natural log value of x.
||The base 10 log of x is returned.
||The maximum of the sequence is returned.
||The minimum of the sequence is returned.
||A tuple is returned containing the fractional and integer parts of a floating point number. The integer part is also returned as a float.
||Returns x ** y.
||The value of x is rounded to n digits.
||The square root of x is retuned.
Random number functions
Random number functions are mainly used in games to create some uncertainty. Python provides various in-built functions to generate and manipulate random numbers. There are the following random number functions.
||A random item is returned from the passed list, tuple, or dictionary.
||A random integer is returned selected randomly from a list.
||A random value is returned which is greater than or equal to 0 and less then 1.
||It is used to set the starting value used in generating the random numbers. It returns none.
||It shuffles the passed list object.
||Generates a random integer which is greater than or equal to x and less than y.