Professional Documents
Culture Documents
Day 5
Day 5
Day 5
------
6.bytes data type:
-------------------
bytes data type represent a group of byte numbers just like an array.
eg;
#bytes data type
a=10,20,30,40
b=bytes(a)
print(b) #adrress of the byte function
print(type(b))
print(b[0])
print(b[-2])
for i in b:
print(i)
eg;
#bytes data type
a=10,20,30,40
b=bytes(a)
print(b)
print(b[1])
b[1]=25 #TypeError: 'bytes' object does not support item assignment
print(b[1])
eg;
#bytearray data type
a=10,20,30,40
b=bytearray(a)
print(b)
print(b[1])
b[1]=258 #ValueError: byte must be in range(0, 256)
print(b[1])
eg;
#list datatype eg-1
lst=[]
lst=list()
lst=[10]
lst=[10,20,30] #homogenous elements
lst=[10,2.5,3+5j,True,'sagar'] #heterogenous elements
print(lst)
print(type(lst))
eg-2
lst=[10,20,30,40]
print(lst)
#print(lst[2])
#print(lst[-2])
#for i in lst:
#print(i)
lst[2]='sagar'
print(lst)
9.tuple datatype;
------------------
If we want to represent a group of elements as a single entity then we use tuple.
some of the features of the tuples are:
eg;
t=()
t=tuple()
t=(10)
t=(10,)
t=20,
t=10,20,30,40
t=(10,2.5,'sagar')
print(t)
print(type(t))
eg;
t=(10,20,30,40)
print(t)
print(t[1])
print(t[-1])
for i in t:
print(i)
10.set datatype;
-------------------
If we want to represent a group of elements as a single entity then we use set.
some of the features of the set are:
eg;
#set datatype
s={} #dict datatype
s=set() #set datatype
s={10}
s={10,20,30,40}
s={10,2.5,4+6j,False,'sagar'}
s={10,40,10,30,20,40}
s.add('sagar')
print(s)
print(type(s))
eg;
---
s={'sagar','banglore','karnataka','india'}
print(s)
for i in s:
print(i)
s.add('Asia')
print(s)
eg;
s={10,20,30,40}
s.add('india')
print(s)
fs=frozenset(s)
print(fs)
fs.add('aditi')
#AttributeError: 'frozenset' object has no attribute 'add'
12.dict datatype;
-------------------
If we want to represent a group of elements as a key:valve pair then we use dict.
some of the features of the dict are:
eg;
d={}
d=dict()
d={1:10}
d={1:10,2:20,3:30}
d={1:10,2:2.5,3:3+4j} #unordered
d={1:10,2:10}
d={1:10,1:10}
d[5]=50
print(d)
print(type(d))
eg;
----
d={1:10,2:20,3:30}
print(d)
#print(d[0]) #KeyError: 0
print(d[1])
d[2]='sagar'
print(d)
d[6]='india'
print(d)
13.None datatype;
------------------
None means nothing or no value associated
if the value is not available then to handle such type None datatype is
used.
in java this is equalent to null value.
eg;
def fun():
a=10
print(fun())
#None
14.range datatype;
--------------------
Range datatype represents a sequence of numbers.The elements present in the
range datatype are not mutable.
*range datatype is immutable.
eg;
#range datatype
r=range(10)
r=range(10,21)
r=range(10,21,2)
for i in r:
print(i)
#for i in range(40,50):
#print(i)
'''
starting point --> 0 inclusive ,optional
ending point -- >n-1 exclusive , complusory
step size --> +1 ,optional
'''
Mutabality and immutability of datatypes:
---------------------------------------------
Python contains 14 datatypes
Python contains the fallowing datatypes
#Fundamental datatypes
-----------------------
1.int -->immutable
2.float -->immutable
3.complex -->immutable
4.bool -->immutable
5.str -->immutable
#intermediate datatypes
------------------------
6.bytes -->immutable
7. bytearray -->mutable
8. range -->immutable
Advanced datatypes
-------------------
9. list -->mutable
10. tuple -->immutable
11. set -->mutable
12. frozenset -->immutable
13. dict -->mutable
extra
-------
14. None -->NA