Numbers in Python
Numbers in Python
Number data type stores Numerical Values. This data type is immutable i.e. value of its object cannot be changed (we will talk about this aspect later). These are of three different types:
a) Integer & Long
b) Float/floating point
c) Complex
Range of an integer in Python can be from -2147483648 to 2147483647, and long integer has unlimited range subject to available memory.
1.1 Integers are the whole numbers consisting of + or – sign with decimal digits like 100000, -99, 0, 17. While writing a large integer value, don‟t use commas to separate digits. Also integers should not have leading zeros.
When we are working with integers, we need not to worry about the size of integer as a very big integer value is automatically handled by Python. When we want a value to be treated as very long integer value append L to the value. Such values are treated as long integers by python.
>>> a = 10
>>> b = 5192L #example of supplying a very long value to a variable
>>> c= 4298114
>>> type(c) # type ( ) is used to check data type of value
<type 'int'>
>>> c = c * 5669
>>> type(c)
<type 'long'>
Integers contain Boolean Type which is a unique data type, consisting of two constants, True & False. A Boolean True value is Non-Zero, Non-Null and Non-empty.
Example
>>> flag = True
>>> type(flag)
<type 'bool'>
1.2 Floating Point: Numbers with fractions or decimal point are called floating point numbers.
A floating point number will consist of sign (+,-) sequence of decimals digits and a dot such as 0.0, -21.9, 0.98333328, 15.2963. These numbers can also be used to represent a number in engineering/ scientific notation.
-2.0X 105 will be represented as -2.0e5
2.0X10-5 will be 2.0E-5
Example
y= 12.36
A value when stored as floating point in Python will have 53 bits of precision.
1.3 Complex: Complex number in python is made up of two floating point values, one each for real and imaginary part. For accessing different parts of variable (object) x; we will use x.real and x.image. Imaginary part of the number is represented by „j‟ instead of „i‟, so 1+0j denotes zero imaginary part.
Example
>>> x = 1+0j
>>> print x.real,x.imag
1.0 0.0
Example
>>> y = 9-5j
>>> print y.real, y.imag
9.0 -5.0
- Str and int
- int and int
- str and str
- int and str