Day 5

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

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

#bytes are immutable in nature

7.bytearray data type ;


------------------------
bytearray is exactly same as bytes datatype except that the elements here can be
mutable.
eg;
#bytearray data type
a=10,20,30,40
b=bytearray(a)
print(b)
print(b[1])
b[1]=25
print(b[1])

#bytearray is mutable in nature

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

#bytearray is mutable in nature


8.list data type;
------------------
If we want to represent a group of elements as a single entity then we use list.
some of the features of the list are:

1.insertion order is maintained


2.it can store both homogenous and heterogenous elements
3.duplicates are allowed.
4.growable in nature
5.elements are always enclosed with [] square brackets.
6.list is mutable in nature

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

lst=[10,20,30,10,30] #duplicates are allowed

lst.append('India') #it is growable in nature

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)

#list is mutable in nature

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:

1.insertion order is maintained


2.it can store both homogenous and heterogenous elements
3.duplicates are allowed.
4.not growable in nature
5.elements are always enclosed with () simple brackets.
6.tuple is immutable in nature

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)

t[2]='India' #TypeError: 'tuple' object does not support item assignment


print(t)

#tuples are immutable in nature

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:

1.insertion order is not maintained or set is unordered.


2.it can store both homogenous and heterogenous elements
3.duplicates are not allowed.
4.growable in nature
5.elements are always enclosed with {} curly brackets.
6.set is mutable in nature

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)

#print(s[2])#TypeError: 'set' object is not subscriptable

for i in s:
print(i)

s.add('Asia')
print(s)

#set is mutable in nature

11.frozenset data type;


--------------------------
frozenset is exactly similar to the set datatype where frozenset is immutable in
nature.

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:

1.insertion order is not maintained or dict is unordered.


2.it can store both homogenous and heterogenous elements
3.duplicates values are allowed but duplicate keys are not allowed.
4.growable in nature
5.elements are always enclosed with {} curly brackets.
6.dict is mutable in nature

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

for i in d: #keys will be accessed


print(i)

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

You might also like