-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_types.py
More file actions
50 lines (42 loc) · 1012 Bytes
/
binary_types.py
File metadata and controls
50 lines (42 loc) · 1012 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#byte=cannote be modified prints the ascii value
data=b'keerthi'
print(data)
print(data[0])
print(data[1])
print(data[2])
print(data[3])
print(data[4])
#bytearray =it can be modified
#direct indexing
data=bytearray(b'hello')
data[0]=72 #changes 'h' (104)to'H'(72)
print(data)
#slicing
data=bytearray(b'hello')
data[1:3]=b'EL'
print(data)
#append
data=bytearray(b'hello')
data.append(33)
print(data)
#extend
data=bytearray(b'hello')
data.extend(b'world')
print(data)
# we can use insert,pop(using index value),remove(using ascii value)
#byte cannot be modified if u want to modify change byte to bytearray and u can modify it
data=b'hello'
#converts byte to bytearray
mutable_data=bytearray(data)
mutable_data[0]=72
#convert binary back to bytes
modified_data=bytes(mutable_data)
print(modified_data)
#memory view
data=b'hello world'
view=memoryview(data)
print(view[0])
mutable_value=bytearray(view)
mutable_value[0]=72
print(mutable_value)
print(data)