You are on page 1of 7

bytes

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.

bytes class or data type represents bytes object.

How to create bytes object?

 Single quotes: b'still allows embedded "double" quotes'


 Double quotes: b"still allows embedded 'single' quotes"
 Triple quoted: b'''3 single quotes''', b"""3 double quotes"""

>>> 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.

Applications of bytes object


1. Binary files
a. Images
b. Audio
c. Video
2. Networking applications
3. Persisting objects/Saving objects into binary file

In addition to the literal forms, bytes objects can be created in a number of


other ways:

 A zero-filled bytes object of a specified length: bytes(10)


 From an iterable of integers: bytes(range(20))
 Copying existing binary data via the buffer protocol: bytes(obj)

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

bytearray objects are a mutable counterpart to bytes objects.


It support all mutable methods,
1. append()
2. insert()
3. remove()
4. pop()
5. sort()
6. extends()
7. ….
“bytearray” class or data type represents bytearray object.

bytearay object is created in different ways.

1. Creating an empty instance: bytearray()


2. Creating a zero-filled instance with a given length: bytearray(10)
3. From an iterable of integers: bytearray(range(20))
4. Copying existing binary data via the buffer protocol: bytearray(b'Hi!')

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

Characteristics of sequence data types


1. Ordered collection where insertion order preserved
2. Index based collection
3. Allows duplicates
4. Sequences can be homogeneous or heterogeneous data

In application development sequences are used to represent group of individual


objects where reading and writing is done sequentially and random.

Sets

Sets are unordered collection, where insertion order is not preserved.


Sets are non index based collections. It does not support indexing and slicing.
Sets does not allows duplicate values.

Python support two set types

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.

set data type class

set is a mutable data type or object.


After creating set changes can be done.
The set type is mutable — the contents can be changed using methods
like add() and remove().

Sets can be created by several means:


 Use a comma-separated list of elements within braces: {'jack', 'sjoerd'}
 Use a set comprehension: {c for c in 'abracadabra' if c not in 'abc'}
 Use the type constructor: set(), set('foobar'), set(['a', 'b', 'foo'])

In set data is organized using hashing data structure.

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}

You might also like