Data Type Introduction
Aviral Bhardwaj
Introduction
we are not required to declare type explicitly
Everything in python is an object
a=10 then a is object reference variable
Int ,float,complex,bool,str,list,tuple,set,frozenset,dict,byte,bytearray,range,None
Built In Function
type()- checking type of variable
id()- to check address of variable
print()- to print object
Int Data Type
Int means integer values it must have whole number
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
4) Hexa decimal form
Binary Form
Allowed digit 0 and 1
Value 0b or 0B
a= 1111
print(a)
a=0b111
print(a)
Octal Form
Allowed digit 0 and 7
Value 0o or 0O ,zero and O
>>> 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
By Value can take lesser space
f=12000000000000.00
f=1.2e+16
Complex Data Type
A+bj
x=10+20j
print([Link])
print([Link])
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])
Positive index and negative index support in python
Slice Operator
What mean by Slice
s[beginindex:endindex]= return substring/slice from begin to end-1 index
s([:9]) 0 to 9-1
s([3:]) from 3 to end
s([:]) all char
s([3:9999]) 3 to last char
Slice Operator
s=’aviral’
Result = Aviral
output=s[0].upper()+s[1:]
Last char as uppercase
output=s[:len(s)-1]+s[-1].upper()
First and last capital word
Type Casting
int()
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))
Order is important and duplicates,hetro,indexing,slicing are allowed and
mutable
Tuple
Immutable
()
Order is important and duplicates,hetro,indexing,slicing are allowed and
immutable
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
[Link](50),
[Link](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)