Python Numbers
Number data types store numeric values. Number objects are created when you
assign a value to them. For example-
var1 = 1
var2 = 10
You can also delete the reference to a number object by using the del statement.
The syntax of the del statement is −
del var1[,var2[,var3[ ... ,varN]]]]
You can delete a single object or multiple objects by using the del
statement. For example-
del var
del var_a, var_b
Python supports three different numerical types −
int (signed integers)
float (floating point real values)
complex (complex numbers)
All integers in Python 3 are represented as long integers. Hence, there is no
separate number type as long.
Python supports different numerical types-
int (signed integers): They are often called just integers or ints. They
are positive or negative whole numbers with no decimal point. Integers in
Python 3 are of unlimited size. Python 2 has two integer types - int and
long. There is no 'long integer' in Python 3 anymore.
float (floating point real values) : Also called floats, they represent
real numbers and are written with a decimal point dividing the integer and
the fractional parts. Floats may also be in scientific notation, with E or e
indicating the power of 10 (2.5e2 = 2.5 x 10 2 = 250).
complex (complex numbers) : are of the form a + bJ, where a and b
are floats and J (or j) represents 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 much in Python programming.
It is possible to represent an integer in hexa-decimal or octal form.
>>> number = 0xA0F #Hexa-decimal
>>> number
2575
>>> number=0o37 #Octal
>>> number
31
Number Type Conversion
Python converts numbers internally in an expression containing mixed types to a
common type for evaluation. Sometimes, you need to coerce a number explicitly
from one type to another to satisfy the requirements 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.