0% found this document useful (0 votes)
5 views6 pages

12 8 2024

Py

Uploaded by

M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

12 8 2024

Py

Uploaded by

M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Bytes data type:

==========
Python supports Bytes data type.The main objective of bytes data type is to audio,
video,semistructure file(HML,JSON).Bytes data type is a immutable object.While
working with Bytes data type the range must be from 0-256-1=255

Ex1:
===
import time
L1=[101,102,103,105,575,11,12,13,14,15]
res1=bytes(L1)
print(res1)
print()
print(type(res1))
print()
time.sleep(3)
print("End of an application")

OUTPUT:
======
C:\Users\Admin\Desktop>Python test1.py
Traceback (most recent call last):
File "test1.py", line 3, in <module>
res1=bytes(L1)
ValueError: bytes must be in range(0, 256)

Ex1:
===
import time
L1=[101,102,103,105,255,11,12,13,14,15]
res1=bytes(L1)
print(res1)
print()
print(type(res1))
print()
time.sleep(3)
print("End of an application")

Ex2:
===
import time
L1=[101,102,103,105,255,11,12,13,14,15]
res1=bytes(L1)
print(res1)
print()
print(*res1)
print()
print(type(res1))
print()
time.sleep(3)
print("End of an application")

Ex3:
===
import time
L1=[101,102,103,105,255,11,12,13,14,15]
res1=bytes(L1)
print(res1)
print()
for x1 in res1:
print(x1)
print()
print(type(res1))
print()
time.sleep(3)
print("End of an application")

Ex4:
===
import time
L1=[101,102,103,105,255,11,12,13,14,15]
print("====Before_Immutable_Operations=====")
res1=bytes(L1)
print()
print(res1)
print()
print(*res1)
print()
print(type(res1))
print()
print("====After_Immuable_Operation======")
res1[0]=121
res1[1]=122
res1[2]=123
res1[3]=124
print(res1)
print()
print(*res1)
print()
print(type(res1))
print()
time.sleep(3)
print('End of an application')

->Bytearray data type:


==============
Python supports Bytes data type.It is exactly same as Bytearray data type.While
bytearray data type the range must be from 0-256-1=255.Bytearray is a mutable
object.

Ex1:
===
import time
L1=[101,102,103,105,255,11,12,13,1400,15]
res2=bytearray(L1)
print(res2)
print()
print(type(res2))
print()
time.sleep(2)
print('End of an application')

OUTPUT:
=====
C:\Users\Admin\Desktop>Python test1.py
Traceback (most recent call last):
File "test1.py", line 3, in <module>
res2=bytearray(L1)
ValueError: byte must be in range(0, 256)

C:\Users\Admin\Desktop>

Ex2:
===
import time
L1=[101,102,103,105,255,11,12,13,255,15]
res2=bytearray(L1)
print(res2)
print()
print(type(res2))
print()
time.sleep(2)
print('End of an application')

Ex3:
==
import time
L1=[101,102,103,105,255,11,12,13,255,15]
res2=bytearray(L1)
print(res2)
print()
print(*res2)
print()
print(type(res2))
print()
time.sleep(2)
print('End of an application')

Ex4:
===
import time
L1=[101,102,103,105,255,11,12,13,255,15]
res2=bytearray(L1)
print(res2)
print()
for b in res2:
print(b)
print()
print(type(res2))
print()
time.sleep(2)
print('End of an application')
Ex5:
===
import time
L1=[101,102,103,105,255,11,12,13,255,15]
print("====Before_Mutable_Operation====")
res3=bytearray(L1)
print(res3)
print()
print(*res3)
print()
print("====After_Mutable_Operation=====")
res3[0]=121
res3[1]=122
res3[2]=123
res3[3]=17000
res3[4]=1
res3[5]=2
print(res3)
print()
print(*res3)
print()
time.sleep(3)
print('End of an application')

OUTPUT:
=====

C:\Users\Admin\Desktop>Python test1.py
====Before_Mutable_Operation====
bytearray(b'efgi\xff\x0b\x0c\r\xff\x0f')

101 102 103 105 255 11 12 13 255 15

====After_Mutable_Operation=====
Traceback (most recent call last):
File "test1.py", line 13, in <module>
res3[3]=17000
ValueError: byte must be in range(0, 256)

C:\Users\Admin\Desktop>

Ex6:
===
import time
L1=[101,102,103,105,255,11,12,13,255,15]
print("====Before_Mutable_Operation====")
res3=bytearray(L1)
print(res3)
print()
print(*res3)
print()
print("====After_Mutable_Operation=====")
res3[0]=121
res3[1]=122
res3[2]=123
res3[3]=255
res3[4]=1
res3[5]=2
print(res3)
print()
print(*res3)
print()
time.sleep(3)
print('End of an application')

Frozenset data type:


==============
Python supports Frozen data type.It is exacltly same as Set data type.

->Set data type is mutable object(State_Full Object)


->Frozenset data type is Immutable object(State_Less_Object)

In real time application frozenset data type used for to perform readability
operations.

Ex1:
===
import time
S1={'a','b','c','d','e','f','g','h','i','j'}
S2=frozenset(S1)
print(S2)
print()
print(type(S2))
print()
for x1 in S2:
print(x1)
print()
time.sleep(2)
print('End of an application')

Interview_Question:
=============
Q1)What is the common difference between Tuple data type and Frozenset
data type Explain with an example?

Ex1:
===
import time
T1=(1,2,3,4,5,6,7,8,9,1,2,3,4,5,6)
print("====Using_Tuple_Data_Type====")
print(T1)
print()
print(type(T1))
print()
for x1 in T1:
print(x1)
print()
print("=====Using_Frozenset_Data_Type====")
S1={1,2,3,4,5,6,7,8,9,1,2,3,4,5,6}
S2=frozenset(S1)
print(S2)
print()
for y1 in S2:
print(y1)
print()
time.sleep(2)
print('End of an application')

OUTPUT:
======
1
2
3
4
5
6

=====Using_Frozenset_Data_Type====
frozenset({1, 2, 3, 4, 5, 6, 7, 8, 9})

1
2
3
4
5
6
7
8
9

End of an application

C:\Users\Admin\Desktop>

You might also like