29th OctPython
29th OctPython
This data type is used to allocate memory for float value/real number. float
value is numeric value with precisions.
Note: all data types in python are dynamic size (OR) size of any data type
is unlimited.
C,C++,Java Python
Short – 2bytes int
Int – 4bytes float
Long – 8bytes
Float – 4bytes
Double – 8bytes
float literal
a float value is represented in two formats or notations
1. Fixed notation/Standard notation
2. Exponent notation
Fixed notation or fixed format
>>> f1=1.5
>>> type(f1)
<class 'float'>
>>> f2=1.123456789123456789123456789
>>> f2
1.1234567891234568
>>>
Float data type reserve memory for 16 precisions. if more than 16
precisions it performs either rounding or truncating. If performs rounding if
value >=.5 else it truncates.
Float is literal/constant/immutable, after creating float object we cannot
modify. Because of it is immutable, it can be shared.
Program
a=257
b=257
print(id(a),id(b))
f1=1.5
f2=1.5
print(id(f1),id(f2))
Output:
186484395408 186484395408
186484392912 186484392912
Note: IDLE shell is only for learning purpose not developing projects.
>>> a=1.6
>>> b=1.6
>>> id(a)
619202147280
>>> id(b)
619202149904
>>> i1=800
>>> i2=800
>>> id(i1)
619202149936
>>> id(i2)
619202149968
>>> i3=100
>>> i4=100
>>> id(i3)
619158328784
>>> id(i4)
619158328784
>>> a=255
>>> b=255
>>> id(a)
619158333808
>>> id(b)
619158333808
>>> x=256
>>> y=256
>>> id(x)
619158333840
>>> id(y)
619158333840
>>> p=257
>>> q=257
>>> id(p)
619202150000
>>> id(q)
619202150064
>>>
>>> c1=1+2j
>>> type(c1)
<class 'complex'>
>>> c1
(1+2j)
>>> c1.real
1.0
>>> c1.imag
2.0
>>> c1.real=1.2
Traceback (most recent call last):
File "<pyshell#56>", line 1, in <module>
c1.real=1.2
AttributeError: readonly attribute
>>>