0% found this document useful (0 votes)
3 views

Python_notes_bytes_range

Uploaded by

anwithadata
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python_notes_bytes_range

Uploaded by

anwithadata
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

====================================================

2. bytes
====================================================
Properties
=>'bytes' is one of the pre-defined class name and treated as Sequence Data
Type.
-----------------------------------------------------------------------------------------------
=>The purpose of bytes data type is that "To organize the Numerical Integer
values
ranges from (0,256) for the implementation of End-to-End Encryption"
------------------------------------------------------------------------------------------------
=>bytes data type does not contains any symbolic notations for oraganizing
(0,256) data.
------------------------------------------------------------------------------------------------
But we can convert any type of Value(s) into bytes type by using bytes()
=>Syntax: varname=bytes(object)
------------------------------------------------------------------------------------------------
=>An object of bytes belongs to immutable bcoz bytes object does not support
Item Assigment.
------------------------------------------------------------------------------------------------
=>On the object of bytes, we can perform both Indexing and Slicing Operations.
------------------------------------------------------------------------------------------------
=>An object of bytes maintains Insertion Order (Which is nothing but, whatever
the order we insert the data, in the same order data will be displayed).
------------------------------------------------------------------------------------------------
Examples
>>> lst=[10,34,56,100,256,0,102]
>>> print(lst,type(lst))----------------------[10, 34, 56, 100, 256, 0, 102] <class
'list'>
>>> b=bytes(lst)----------------------------ValueError: bytes must be in range(0,
256)
>>> lst=[10,-34,56,100,255,0,102]
>>> print(lst,type(lst))-------------------[10, -34, 56, 100, 255, 0, 102] <class
'list'>
>>> b=bytes(lst)--------------------------ValueError: bytes must be in range(0,
256)
>>> lst=[10,34,56,100,255,0,102]
>>> print(lst,type(lst))-------------------[10, 34, 56, 100, 255, 0, 102] <class 'list'>
>>> b=bytes(lst)
>>> print(b,type(b))---------------------b'\n"8d\xff\x00f' <class 'bytes'>
>>> for val in b:
... print(val)
10

34

56

100

255

102

>>> print(b,type(b))----------------b'\n"8d\xff\x00f' <class 'bytes'>


>>> print(b[0])--------------------10
>>> print(b[-1])-------------------102
>>> print(b[0:4])--------------- b'\n"8d'
>>> for val in b[0:4]:
... print(val)
10

34

56

100

>>> b[0]----------------------------10
>>> b[0]=123---------------TypeError: 'bytes' object does not support item
assignment
==============================x==========================

=========================================================

3. bytearray
=========================================================
Properties
=>'bytearray' is one of the pre-defined class name and treated as Sequence Data
Type.
------------------------------------------------------------------------------------------------
=>The purpose of bytearray data type is that "To organize the Numerical Integer
values ranges from (0,256) for the implementation of End-to-End Encryption".
------------------------------------------------------------------------------------------------
=>bytearray data type does not contains any symbolic notations for organizing
(0,256) data. But we can convert any type of Value(s) into bytearray type by
using bytearray()
=>Syntax: varname=bytearray(object)
------------------------------------------------------------------------------------------------
=>An object of bytearray belongs to mutable bcoz bytearray object supports
Item Assignment.
------------------------------------------------------------------------------------------------
=>On the object of bytearray, we can perform both Indexing and Slicing
Operations.
------------------------------------------------------------------------------------------------
=>An object of bytearray maintains Insertion Order (Which is nothing but,
whatever the order we insert the data, In the same order data will be displayed).
------------------------------------------------------------------------------------------------
NOTE: The Functionality of bytes and bytearray are exactly same but bytes
object belongs to Immutable bcoz bytes object does not support Item assigment
where bytearray object blongs to Mutable bcoz bytearray object supports Item
Assigment.

Examples:
>>> tpl=(10,34,56,100,256,0,102)
>>> print(tpl,type(tpl))-----------------(10, 34, 56, 100, 256, 0, 102) <class
'tuple'>
>>> ba=bytearray(tpl)--------------------ValueError: byte must be in range(0,
256)
>>> tpl=(-10,34,56,100,255,0,102)
>>> print(tpl,type(tpl))-------(-10, 34, 56, 100, 255, 0, 102) <class 'tuple'>
>>> ba=bytearray(tpl)-------------ValueError: byte must be in range(0, 256)
-------------------------
>>> tpl=(10,34,56,100,255,0,102)
>>> print(tpl,type(tpl))------------(10, 34, 56, 100, 255, 0, 102) <class 'tuple'>
>>> ba=bytearray(tpl)
>>> print(ba,type(ba))------------bytearray(b'\n"8d\xff\x00f') <class 'bytearray'>
-------------------------------
>>> tpl=(10,34,56,100,255,0,102)
>>> print(tpl,type(tpl))-----------------(10, 34, 56, 100, 255, 0, 102) <class
'tuple'>
>>> ba=bytearray(tpl)
>>> print(ba,type(ba))---------------------bytearray(b'\n"8d\xff\x00f') <class
'bytearray'>
>>> for val in ba:
... print(val)
10

34

56
100

255

102

>>> for val in ba[::-1]:


... print(val)
102

255

100

56

34

10

>>> for val in ba:


... print(val)
10

34

56

100

255

102

>>> print(ba,type(ba),id(ba))---------bytearray(b'\n"8d\xff\x00f') <class


'bytearray'> 2149788291504
>>> ba[0]--------------10
>>> ba[-1]------------102
>>> ba[0]=123 # Updating / item assoigment on bytearray object--allowed
>>> for val in ba:
... print(val)
...
123

34

56

100
255

102

>>> print(ba,type(ba),id(ba))-----bytearray(b'{"8d\xff\x00f') <class 'bytearray'>


2149788291504
------------------------------------------
>>> b=bytes(ba) # Converting bytearray into bytes
>>> print(b,type(b))---------------b'{"8d\xff\x00f' <class 'bytes'>
>>> for val in b:
... print(val)
123

34

56

100

255

102

>>> b[0]---------------------123
>>> b[0]=12------TypeError: 'bytes' object does not support item
assignment
>>> x=bytearray(b) # Converting bytes into bytearray
>>> print(x,type(x),id(x))----bytearray(b'{"8d\xff\x00f') <class 'bytearray'>
2149788291888
>>> for v in x:
... print(v)
123

34

56

100

255

102

>>> x[1]----------------------34
>>> x[1]=234
>>> for v in x:
... print(v)
123

234

56

100

255

102

>>> print(x,type(x),id(x))-----bytearray(b'{\xea8d\xff\x00f') <class 'bytearray'>


2149788291888
------------------------------------------X----------------------------------------------------
====================================================

4. range()
====================================================
Properties
=>'range' is one of the pre-defined class and treated as Sequence data type.
------------------------------------------------------------------------------------------------

=>The purpose of range data type is that "To Store Sequence of Numerical
Integer Values with Equal Interval of Value."
------------------------------------------------------------------------------------------------
=>An object of range belongs to Immutable bcoz range object does not support
Item Assignment.
------------------------------------------------------------------------------------------------
=>On the object of range, we can perform Both Indexing Slicing Operations
------------------------------------------------------------------------------------------------
=>The range of Values can be stored either in forward or Backward Directions.
------------------------------------------------------------------------------------------------
=>range data type contains 3 syntaxes. They are
------------------------------------------------------------------------------------------------
Syntax-1: varname=range(Value)
=>This Syntax generates range of values from 0 to Value-1
------------------------------------------------------------------------------------------------
Examples:
>>> r=range(6)
>>> print(r,type(r))
range(0, 6) <class 'range'>
>>> for v in r:
... print(v)
...
0

>>> for val in range(6):


... print(val)
...
0

>>> for val in range(10):


... print(val)
...
0
1

------------------------------------------------------------------------------------------------

Syntax-2 : varname=range(Begin,End)
=>This Syntax generates range of values from Begin to End-1
------------------------------------------------------------------------------------------------
Examples:
>>> r=range(10,16)
>>> print(r,type(r))
range(10, 16) <class 'range'>
>>> for val in r:
... print(val)
10

11

12

13

14

15

>>> for val in range(10,16):


... print(val)
10

11

12

13
14

15

>>> for val in range(10,16):


... print(val,end=" ") # 10 11 12 13 14 15
------------------------------------------------------------------------------------------------
NOTE: The Syntax-1 and Syntax-2 generates range of values in FORWARD
DIRECTION with Default Interval 1.

Syntax-3: varname=range(Begin,End,Step)
=>This Syntax generates range of values from Begin to End-1 by maintaining
Equal Interval value in the form Step either in Forward Direction or Back
direction.
------------------------------------------------------------------------------------------------
Examples:
>>> r=range(10,21,2)
>>> for val in r:
... print(val)
...
10

12

14

16

18

20

>>> for val in range(10,21,2):


... print(val)
...
10

12

14

16
18

20

------------------------------------------------------------------------------------------------
Implementation of range data type
------------------------------------------------------------------------------------------------
Q1) 0 1 2 3 4 5 6 7 8 9 10--------range(11)
>>> for val in range(11):
... print(val)
...
0

10

------------------------------------------------------------------------------------------------
Q2) 10 11 12 13 14 15 16 17 18 19 20--range(10,21)
>>> for val in range(10,21):
... print(val)
...
10

11

12

13

14

15

16

17

18
19

20

------------------------------------------------------------------------------------------------
Q3) 1000 1001 1002 1003 1004 1005----range(1000,1006)
>>> for val in range(1000,1006):
... print(val)
...
1000
1001
1002
1003
1004
1005
------------------------------------------------------------------------------------------------
Q4) 10 12 14 16 18 20-------range(10,21,2)
>>> for val in range(10,21,2):
... print(val)
...
10

12

14

16

18

20

------------------------------------------------------------------------------------------------
Q5) 100 110 120 130 140 150 160 170 180 190 200----
range(100,201,10)
>>> for v in range(100,201,10):
... print(v)
...
100

110

120

130

140

150

160

170

180

190

200

------------------------------------------------------------------------------------------------
Q6) 10 9 8 7 6 5 4 3 2 1-----range(10,0,-1)
>>> for v in range(10,0,-1):
... print(v)
...
10

------------------------------------------------------------------------------------------------
Q7) 100 90 80 70 60 50 40 30 20 10 0------range(100,-1,-10)
>>> for hyd in range(100,-1,-10):
... print(hyd)
...
100

90
80

70

60

50

40

30

20

10

------------------------------------------------------------------------------------------------
Q8) -10 -11 -12 -13 -14 -15-------- range(-10,-16,-1)
>>> for val in range(-10,-16,-1):
... print(val)
...
-10

-11

-12

-13

-14

-15

------------------------------------------------------------------------------------------------
Q9) -100 -90 -80 -70 -60 -50 -40 -30 -20 -10 ----range(-100,-9,10)
>>> for val in range(-100,-9,10):
... print(val)
...
-100

-90

-80

-70

-60

-50

-40

-30

-20

-10
------------------------------------------------------------------------------------------------
10) -5 -4 -3 -2 -1 0 1 2 3 4 5 -----range(-5,6,1) OR range(-5,6)
>>> for val in range(-5,6,1):
... print(val)
...
-5

-4

-3

-2

-1

>>> for val in range(-5,6):


... print(val)
...
-5

-4

-3

-2

-1

------------------------------------------------------------------------------------------------
>>> for val in range(-1000,-1201,-50):
... print(val)
...
-1000

-1050

-1100

-1150

-1200

>>> r=range(-1000,-1201,-50)
>>> r[0]-------------------------1000
>>> r[-1]-------------------------1200
>>> for val in r[0:2]:
... print(val)
...
-1000

-1050

>>> r[0]=2000----TypeError: 'range' object does not support item


assignment
-----------------
>>> for val in range(10,21,2)[::-1]:
... print(val)
...
20

18

16

14

12

10

=====================X==================================

You might also like