0% found this document useful (0 votes)
4 views

29th OctPython

The float data type in Python reserves memory to store floating point or real number values with up to 16 digits of precision. Float values can be represented in fixed or exponential notation. The complex data type stores complex numbers with real and imaginary parts, where the imaginary part is denoted with a 'j' suffix. Both float and complex objects are immutable once created.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

29th OctPython

The float data type in Python reserves memory to store floating point or real number values with up to 16 digits of precision. Float values can be represented in fixed or exponential notation. The complex data type stores complex numbers with real and imaginary parts, where the imaginary part is denoted with a 'j' suffix. Both float and complex objects are immutable once created.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

float data type or real data type

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

In fixed notation float value is represented by separating integer value and


decimal values using a decimal point.

>>> 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
>>>

Exponent notation or scientific notation


If the value is very large, it is represented exponent notation.
In exponent notation we use one special character “e” or “E”
Example:
>>> f1=123e-1
>>> f1
12.3
>>> f2=1234e2
>>> f2
123400.0
>>> type(f1)
<class 'float'>
>>> type(f2)
<class 'float'>
>>> f3=1.45678e-3
>>> f3
0.00145678
>>>

The value of “e” is 10


123e3  123x10 pow 3

Complex data type or complex number


“complex” class or data type used to represent complex numbers.
Complex number is having two values.
1. Real
2. Imag
Syntax of representing complex number
real+imagj

imag value is suffix with “j”

>>> 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
>>>

You might also like