Number Data Type
Number data type is used to store numeric values. Python creates Number objects when we assign a numeric value to a variable.
num1 = 17 num2 = 121
We can delete a single or multiple objects by using the del statement.
del num1 #deletes single object del num2, num3 #deletes multiple objects
In python, integers can be of any length but a floating point number is accurately only up to 15 decimal places.
Python can also express Binary, Octal and Hexadecimal numbers.
Binary numbers have base 2 and are represented with 0b or 0B as prefix before the number.
Octal numbers have base 8 and are represented with 0o or 0O as prefix before the number.
Hexadecimal numbers have base 16 and are represented with 0x or 0X as prefix before the number.
Python supports the following 4 types of numeric data,
1. int: used to store signed integers like 21, 452 etc.
2. long: used to store higher range of integer values like 123456789L, 987654321L etc.
3. float: used to store floating point numbers like 15.2, 756.98 etc.
4. complex: used to store complex numbers like 5+2j, 21-13j etc.
We can use the type() function to know which class a variable or value belongs to and isinstance() function to check if it belongs to a particular class.
We can use python built-in functions to convert between types explicitly. These functions can even convert from strings.
1. int(x) is used to convert a number to a plain integer.
2. long(x) is used to convert a number to a long integer.
3. float(x) is used to convert a number to a floating point number.
4. complex(x) is used to convert a number to a complex number with real part x and imaginary part 0.
5. complex(x,y) is used to convert a number to a complex number with real part x and imaginary part y.
Python built-in number functions
|abs(x)||Returns the absolute value of x.|
|ceil(x)||Returns the ceil value of x.|
|floor(x)||Returns the floor of x.|
|cmp(x,y)||Returns -1 if x < y, 0 if x == y or 1 if x > y.|
|exp(x)||abcReturns the exponent of x (ex).|
|log(x)||Returns the natural logarithm of x.|
|max()||Returns the maximum value.|
|min()||Returns the minimum value.|
|pow(x,y)||Returns the value of x**y.|
|sqrt(x)||Returns the square root of x.|
|sin(x)||Returns the sine of x radians.|
|cos(x)||Returns the cosine of x radians.|
|tan(x)||Returns the tangent of x radians.|
|degrees(x)||Converts angle x from radians to degrees.|
|radians(x)||Converts angle x from degrees to radians.|
|random()||Returns the random float number greater than or equals to 0 and less than 1.|
|randrange([start,] stop [,step])||Returns a randomly selected number from range(start, stop, step).|