Professional Documents
Culture Documents
Data Type Introduction
Data Type Introduction
Aviral Bhardwaj
Introduction
we are not required to declare type explicitly
Built In Function
a=124
Type(a)- int
Long in Python2x
a=12387654376543
type(a)-long
Representation of Int
We can represent int values in the following ways
1) Decimal form
2) Binary form
3) Octal form
Value 0b or 0B
a= 1111
print(a)
a=0b111
print(a)
Octal Form
Allowed digit 0 and 7
>>> a=0O1234567
>>> a
342391
Hexa Form
Allowed digit 0 to 9 and A to F
Value 0x or 0X
>>> a=0xface123
>>> a
262988067
Base Conversions
bin()
oct()
hex()
Float Data Type
f=12.456
type(f)=float
Exponential Form
f=1.2e3
f=scientific Notation
f=12000000000000.00
f=1.2e+16
Complex Data Type
A+bj
x=10+20j
print(x.real)
print(x.imag)
Bool Data Type
True 1
False 0
Type
Print(True+True)
Str Data Type
Any sequence of char
s=‘aviral’
s2=”aviral”
s3=’’’aviral’’
Index
s=’aviral’
print(s[0])
print(s[100]) -out of index error
Str Data Type
Any sequence of char
s=‘aviral’
print(s[-1])
s([:9]) 0 to 9-1
Result = Aviral
output=s[0].upper()+s[1:]
output=s[:len(s)-1]+s[-1].upper()
float()
str()
bool()
complex()
Mutability and Immutability
Once we create object we cannot perform any changes in that
Memory utilization
A is b
List
[]
l=[1,2,3,4,5,6,’aviral’]
print(l)
print(id(l))
l[0]=123
print(l)
print(id(l))
()
t1=(1,2,3,”bingo”) (10,)
Set
Duplicates are not allowed
No order,hetro is fine,growable, mutable
{}
s={1,2,3,1,2,4,5,6,’aviral’}
No order, no slice,no index
s.add(50),
s.remove(1)
Frozen Set
Same as set but immutable
s={1,2,3,1,2,4,5,6,’aviral’}
fs=frozenset(s)
print(type(fs))
Dict
Dict = {1: 'Aviral', 2: 'is',
3:’awesome’ }
dict[2]=’bingo’
print(dict)
Duplicate key is not allowed but value is allowed(old value will be removed
with new one)
Order is not applicable , indexing not available
Range
r=range(10)
print(type(r))
For x in r:
print(x)
r=range(1,10)
r=range(1,20,1/2)
Bytes and Byte Array
b=[1,2,3,4,5,6]
r=byte(b)
print(type(r))
b=[1,2,3,4,5,6]
r=bytearray(b)