Full Stack Python
Full Stack Python
Bytes objects are immutable sequences of single bytes. Since many major binary
protocols are based on the ASCII text encoding, bytes objects offer several
methods that are only valid when working with ASCII compatible data and are
closely related to string objects in a variety of other ways.
>>> b1=b'python'
>>> print(type(b1))
<class 'bytes'>
>>> s1='python'
>>> print(type(s1))
<class 'str'>
>>> b2=b"python"
>>> print(b2)
b'python'
>>> print(type(b2))
<class 'bytes'>
>>> b3=b'python is "easy" language'
>>> print(b3)
b'python is "easy" language'
>>> b4=b"python is 'easy' language"
>>> print(b4)
b"python is 'easy' language"
>>> b5=b'''python is
... programming
... language'''
>>> print(b5)
b'python is\nprogramming\nlanguage'
>>> b6=b"""python is
... programming
... language"""
>>> print(b6)
b'python is\nprogramming\nlanguage'
Only ASCII characters are permitted in bytes literals (regardless of the declared
source code encoding). Any binary values over 127 must be entered into bytes
literals using the appropriate escape sequence.
Example:
>>> b1=bytes(10)
>>> print(b1)
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>> b2=bytes(range(65,91))
>>> print(b2)
b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> list1=[65,66,67,68]
>>> b3=bytes(list1)
>>> print(b3)
b'ABCD'
>>> b4=bytes(b3)
>>> print(b4)
b'ABCD'
>>> print(b2[0])
65
>>> for value in b2:
... print(value,end=' ')
...
...
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
>>> for value in b1:
... print(value,end=' ')
...
...
0000000000
bytes object is immutable sequence, after creating bytes object changes cannot be
done.
>>> b1=b'ABC'
>>> print(b1)
b'ABC'
>>> b1.append(65)
Traceback (most recent call last):
File "<pyshell#41>", line 1, in <module>
b1.append(65)
AttributeError: 'bytes' object has no attribute 'append'
>>> b1[0]=66
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
b1[0]=66
TypeError: 'bytes' object does not support item assignment
bytearray
Example:
>>> ba1=bytearray()
>>> print(ba1)
bytearray(b'')
>>> ba1.append(65)
>>> print(ba1)
bytearray(b'A')
>>> ba1.append(66)
>>> print(ba1)
bytearray(b'AB')
>>> ba1.extend([67,68,69,70])
>>> print(ba1)
bytearray(b'ABCDEF')
>>> ba1[0]=71
>>> print(ba1)
bytearray(b'GBCDEF')
>>> del ba1[0]
>>> print(ba1)
bytearray(b'BCDEF')
>>> ba2=bytearray(10)
>>> print(ba2)
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
>>> ba2[0]=65
>>> print(ba2)
bytearray(b'A\x00\x00\x00\x00\x00\x00\x00\x00\x00')
>>> ba2[-1]=90
>>> print(ba2)
bytearray(b'A\x00\x00\x00\x00\x00\x00\x00\x00Z')
>>> ba3=bytearray(range(97,123))
>>> print(ba3)
bytearray(b'abcdefghijklmnopqrstuvwxyz')
>>> ba4=bytearray(ba3)
>>> print(ba4)
bytearray(b'abcdefghijklmnopqrstuvwxyz')
Sets
1. set (mutable)
2. frozenset (immutable)
Application of sets
1. To remove duplicates from sequences
2. To perform mathematical set operations
a. Union
b. Intersection
c. Difference
…
3. To represent unique data
A set object is an unordered collection of distinct hashable objects. Common uses
include membership testing, removing duplicates from a sequence, and computing
mathematical operations such as intersection, union, difference, and symmetric
difference.
Empty set cannot create using empty {}, this represents dictionary.
Example:
>>> s1={}
>>> type(s1)
<class 'dict'>
>>> s2=set()
>>> print(s2)
set()
>>> s3={10}
>>> type(s3)
<class 'set'>
>>> s4={10,20,30,40,50}
>>> type(s4)
<class 'set'>
>>> print(s4)
{50, 20, 40, 10, 30}
>>> s5={10,10,10,10,10,10}
>>> print(s5)
{10}
>>> s6={10,20,10,20,10,20,30}
>>> print(s6)
{10, 20, 30}