0% found this document useful (0 votes)
27 views136 pages

Python Notes

Uploaded by

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

Python Notes

Uploaded by

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

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

II. Sequence Category Data Types


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

=>The purpose of Sequence Category Data Types is that "To store Sequence of
values".
=>In Python programming, we have 4 data types in Sequence Category. They
are
1. str
2. bytes
3. Bytearray
4. range

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

1. str (Part-1)
========================================================================

INDEX
=>Purpose of str
=>Definition of str
=>Types of strs
=>Notation used for storing str data
=>Memory Management of str data
=>Operations on str Data
a) Indexing
b) Slicing
=>Programming Examples
PROPERTIES
=>'str' is one of the pre-defined classe and treated as Sequence Data Type.
=>The purpose of str data type is that "To store String data or text data or
Alphanumeric data or numeric data or special symbols within double Quotes or
single quotes or triple double quotes and triple single quotes. "
=> String is Immutable since it will not support assignment (=)
Example:
>>> s="PYTHON"
>>> s[0]="J"----TypeError: 'str' object does not support item assignment
=>Def. of str:
=>str is a collection of Characters or Alphanumeric data or numeric data or any
type of data enclosed within double Quotes or single quotes or triple double
quotes and triple single quotes. "
=>Types of Str data
=>In Python Programming, we have two types of Str Data. They are
1. Single Line String Data
2. Multi Line String Data
1. Single Line String Data:
=>Syntax1:- varname=" Single Line String Data "
(OR)
=>Syntax2:- varname=' Single Line String Data '
=>With the help double Quotes (" ") and single Quotes (' ') we can store single
line str data only but not possible to store multi line string data.
2. Multi Line String Data:
=>Syntax1:- varname=" " " String Data1
String Data2
------------------
String data-n " " "
(OR)
=>Syntax2:- varname=' ' ' String Data1
String Data2
------------------
String data-n ' ' '

=>With the help OF tripple double Quotes (" " " " " ") and Tripple single
Quotes (' ' ' ' ' ')
=>we can store single line str data and multi-line string data.
Examples:
>>> s1="Python"
>>> print(s1,type(s1))--------------Python <class 'str'>
>>> s2='Python'
>>> print(s2,type(s2))-------------Python <class 'str'>
>>> s3='A'
>>> print(s3,type(s3))-----------A <class 'str'>
>>> s4="A"
>>> print(s4,type(s4))--------------A <class 'str'>
>>> s5="Python3.10"
>>> print(s5,type(s5))--------------Python3.10 <class 'str'>
>>> s6="123456"
>>> print(s6,type(s6))----------123456 <class 'str'>
>>> s7="Python Programming 1234$Rossum_Guido"
>>> print(s7,type(s7))---Python Programming 1234$Rossum_Guido <class
'str'>
>>> addr1= "Guido van Rossum ----SyntaxError: unterminated string literal
(detected at line 1)
>>> addr1= ' Guido van Rossum ---- SyntaxError: unterminated string
literal (detected at line 1)
-------------------------------------
>>> addr1=" " "Guido van Rossum
... HNO:3-4,Hill Side
... Python Software Foundation
... Nether Lands-56"""
>>> print(addr1,type(addr1))
Guido van Rossum
HNO:3-4,Hill Side
Python Software Foundation
Nether Lands-56 <class 'str'>
>>> addr2=' ' 'Travis Oliphant
... HNO:23-45, Sea Side
... Numpy Organization
... nethr lands-67 '''
>>> print(addr2,type(addr2))
Travis Oliphant
HNO:23-45, Sea Side
Numpy Organization
nethr lands-67 <class 'str'>
>>> s7=" " " Python " " "
>>> print(s7,type(s7))--------------Python <class 'str'>
>>> s8=' ' ' Python ' ' '
>>> print(s8,type(s8))-----------------Python <class 'str'>
>>> s9=" " " Z " " "
>>> print(s9,type(s9))-------------Z <class 'str'>
>>> s10=' ' ' K ' ' '
>>> print(s10,type(s10))------------K <class 'str'>
>>> s1="Python Programmin"
>>> print(s1,type(s1))--------------------Python Programmin <class 'str'>
>>> s1--------------------------------------- ' Python Programmin '
>>> s7=" " " Python " " "
>>> print(s7,type(s7))------------------Python <class 'str'>
>>> s7-------------------------------------- ' Python '
>>> s1=" " "Python is an oop lang
... python is also fun Prog lang
... Python is also Modular Prog lang " " "
>>> print(s1,type(s1))
Python is an oop lang
python is also fun Prog lang
Python is also Modular Prog lang <class 'str'>
>>> s1
' Python is an oop lang \n python is also fun Prog lang \n Python is also Modular
Prog lang '
=================================x=======================
Operations on str Data
=>On str data, we can perform Two Types of Operations. They are
1. Indexing
2. Slicing
1. Indexing
=>The Process of Obtaining Single Value from given Main Str object is called
Indexing
Syntax: strobj [Index]
=>here strobj is an object of <class,'str'>
=>index represents Either +Ve Index or -Ve Index
=>If we enter Valid Index value then we get Corresponding Indexed Value.
=>If we enter Invalid Index value then we get IndexError.
Examples
>>> s="PYTHON"
>>> print(s,type(s))-------------------PYTHON <class 'str'>
>>> print(s[0])----------------------P
>>> print(s[-6])---------------------P
>>> print(s[-1])--------------------N
>>> print(s[5])---------------------N
>>> print(s[3])--------------------H
>>> print(s[-3])-------------------H
>>> print(s[-2])-------------------O
>>> print(s[-4])-------------------T
>>> print(s[-5])------------------Y
>>> print(s[2])-------------------T
>>> s[2]----------------------------'T'
>>> s[-2]--------------------------'O'
>>> print(s[8])-------------------IndexError: string index out of range
>>> print(s[-12])----------------IndexError: string index out of range
-----------------------------------------
>>> s="123453456"
>>> print(s,type(s))---------------123453456 <class 'str'>
>>> s[2]----------------------'3'
>>> s[-1]-------------------'6'
>>> s[0]--------------------'1'
>>> s[len(s)-1]-------------'6'
>>> s[-len(s)]---------------'1'
--------------------------------------------
>>> s="JAVA"
>>> print(s,type(s))-------------JAVA <class 'str'>
>>> len(s)-------------------------4
>>> s[len(s)-1]-----------------'A'
>>> s[-len(s)]------------------'J'
>>> s[len(s)-len(s)]-------------'J'
>>> s[-2]---------------------------'V'
>>> s[2]----------------------------'V'
>>> s[len(s)]---------------------- IndexError: string index out of range
---------------------------------------------------
>>> s="MISSISSIPPI"
>>> s[len(s)-1]----------------'I'
>>> s[-len(s)]-----------------'M'
>>> s[len(s)]-----------------------IndexError: string index out of range
2.Slicing
=>The Process obtaining Range of Values OR Sub String from Given main Str
Object is called Slicing.
=>To Perform Slicing Operations, we have 5 Syntaxes. They are
a) Syntax1: strobj [BEGIN: END]
=>This Syntax generates Range of Values OR Sub String from BEGIN Index
to END-1 Index provided BEGIN<END Otherwise we never get any result OR
Space OR ' ' as a Result
Examples:
>>> s="PYTHON"
>>> print(s,type(s))-------------------PYTHON <class 'str'>
>>> s[0:3]--------------------------------'PYT'
>>> s[2:6]--------------------------------'THON'
>>> s[1:6]---------------------------------'YTHON'
>>> s[2:1]---------------------------------' '
>>> s[0:6]----------------------------------'PYTHON'
--------------------------------------------------------
>>> s="PYTHON"
>>> print(s,type(s))-----------------PYTHON <class 'str'>
>>> s[-6:-2]---------------------------'PYTH'
>>> s[-3:-1]---------------------------'HO'
>>> s[-6:-3]---------------------------'PYT'
>>> s[-3:-6]---------------------------' '
>>> s[-6:-1]---------------------------'PYTHO'
Sub Rule:
b) strobj [BEGINPOSINDEX: ENDNEGATIVEINDEX]
=>This Syntax Gives Range of Characters from BEGINPOSINDEX to
ENDNEGATIVEINDEX-1 provided BEGINPOSINDEX >
ENDNEGATIVEINDEX. Otherwise, we get Space or NO Result OR ' ' as a
result.
Examples
>>> s="PYTHON"
>>> print (s, type(s)) ----------PYTHON <class 'str'>
>>> s[1:-2]-------------'YTH'
>>> s[2:-1]--------------'THO'
>>> s[-3:1]--------------' '
------------------------------------------------------------------------------------------------
Syntax:2 : strobj[BEGIN : ]
=>In This Syntax, we are Specifying BEGIN Index and we did't Specify END
Index.
=>If we don't specify END Index then PVM Takes END Index as len(strobj)
(OR)
=>If we don't specify END Index then PVM Takes BEGIN INDEX Character to
Last Character.
Examples:
>>> s="PYTHON"
>>> print(s,type(s))--------------PYTHON <class 'str'>
>>> s[2:]----------------------------'THON'
>>> s[0:]----------------------------'PYTHON'
>>> s[3:]----------------------------'HON'
>>> s[4:]----------------------------'ON'
>>> s[1:]---------------------------'YTHON'
-------------------------------
>>> s="PYTHON"
>>> print(s,type(s))----------------PYTHON <class 'str'>
>>> s[-3:]-----------------------------'HON'
>>> s[-2:]-----------------------------'ON'
>>> s[-6:]-----------------------------'PYTHON'
>>> s[-5:]----------------------------'YTHON'
>>> s[-4:]----------------------------'THON'
>>> s[-1:]----------------------------'N'
***********************Special Points************************
>>> s="PYTHON"
>>> print(s,type(s))-----------------------PYTHON <class 'str'>
>>> s[-12:]-----------------------------------'PYTHON'
>>> s[-23:]----------------------------------'PYTHON'
>>> s[23:]----------------------------------' '
>>> s[2:4]---------------------------------- 'TH'
>>> s[12:44]------------------------------- ' '
>>> s[-66:-1]------------------------------ 'PYTHO'
>>> s[2:122]------------------------------ 'THON'
>>> s[2:-122]-------------------------------' '
>>> s[2:-1]----------------------------------'THO'
>>> s[2:-22]--------------------------------' '
------------------------------------------------------------------------------------------------
Syntax3: strobj[ : End]
=>In This Syntax, we are Specifying END Index and we didn’t Specify BEGIN
Index.
=>If we don't specify BEGIN Index then PVM Takes BEGIN Index as 0
OR -len(strobj)
(OR)
=>If we don't specify BEGIN Index then PVM Takes First Character to END-1
Index.
Examples:
>>> s="PYTHON"
>>> print(s,type(s))--------------PYTHON <class 'str'>
>>> s[:3]----------------------------'PYT'
>>> s[:6]----------------------------'PYTHON'
>>> s[:5]---------------------------'PYTHO'
>>> s[:4]---------------------------'PYTH'
------------------------------
>>> s="PYTHON"
>>> print(s,type(s))-------------PYTHON <class 'str'>
>>> s[:-3]---------------------------'PYT'
>>> s[:-2]--------------------------'PYTH'
>>> s[:-1]--------------------------'PYTHO'
>>> s[:-4]--------------------------'PY'
>>> s[:-5]---------------------------'P
------------------------------------------------------------------------------------------------
Syntax4: strobj [ : ]
=>In This Syntax, we are not Specifying BEGIN Index and END Index
=>If we don't specify BEGIN Index and END Index then PVM Takes BEGIN
Index as 0 OR -len(strobj) and END Index as len(strobj) or -1
(OR)
=>If we don't specify BEGIN Index and END Index then PVM Takes from First
Character (0th Index or -len(strobj) index ) to last Character ( len(strobj) or -1).
=>Hence This Syntax always gives Total String Data.
Examples:
>>> s="PYTHON"
>>> print(s,type(s))-------------------PYTHON <class 'str'>
>>> s[:]-----------------------------------'PYTHON'
>>> s="JAVA PROG"
>>> print(s,type(s))-------------------JAVA PROG <class 'str'>
>>> s[:]----------------------------------'JAVA PROG'
>>> s[0:]--------------------------------'JAVA PROG'
>>> s[:len(s)]--------------------------'JAVA PROG'
>>> s[-len(s):]-------------------------'JAVA PROG'
NOTE: All the above Syntaxes are Extracting the data from strobj in Forward
Direction with Default Step value 1.
------------------------------------------------------------------------------------------------
Syntax 5: strobj[BEGIN: END : STEP]
RULE-1: Here BEGIN, END and STEP Values can be either +VE or -VE
RULE-2: If STEP Value is +VE then PVM Takes the Range of Characters
from BEGIN Index to END-1 -------- Index in FORWARD Direction With Step
Value provided BEGIN INDEX < END INDEX Otherwise we get Space or ' '
as a result
RULE-3: If STEP Value is -VE then PVM Takes the Range of Characters from
BEGIN Index to END+1 ------- Index in BACKWARD Direction with Step
Value provided BEGIN INDEX > END INDEX Otherwise we get Space or ' ' as
a result
RULE-4: In FORWARD Direction, If END Value is 0 Then we get Space or ' '
as a result
RULE-5: In BACKWARD Direction, If END Value is -1 Then we get Space
or ' ' as a result
------------------------------------------------------------------------------------------------
Examples: RULE-2
>>> s="PYTHON"
>>> print(s)-------------------------------PYTHON
>>> s[0:4]---------------------------------'PYTH'
>>> s[0:4:1]------------------------------'PYTH'
>>> s[2:6:1]------------------------------'THON'
>>> s[2:6:2]------------------------------'TO'
>>> s[0:6:2]-----------------------------'PTO'
>>> s[0:6:3]-----------------------------'PH'
>>> s[ : : ]--------------------------------'PYTHON'
>>> s[ : :2]--------------------------------'PTO'
>>> s[ : :3]--------------------------------'PH'
>>> s[-6:-1:2]----------------------------'PTO'
>>> s[-6: :]-------------------------------'PYTHON'
>>> s[:6:]---------------------------------'PYTHON'
>>> s[0:6:]-------------------------------'PYTHON'
>>> s[:-1:]-------------------------------'PYTHO'
------------------------------------------------------------------------------------------------
Examples: RULE-3
>>> s="PYTHON"
>>> print(s)-----------------------------PYTHON
>>> s[::]---------------------------------'PYTHON'
>>> s[::1]-------------------------------'PYTHON'
>>> s[::-1]-------------------------------'NOHTYP'
>>> s[::-2]--------------------------------'NHY'
>>> s[0:6:-2]-----------------------------''
>>> s[5:0:-1]----------------------------'NOHTY'
>>> s[::-3]-------------------------------'NT'
>>> s[4: :-2]----------------------------'OTP'
>>> s[: :2]------------------------------'PTO'
>>> s[: :2][::-1]------------------------'OTP'
>>> s[-1:-7:-1]-------------------------'NOHTYP'
>>> s[-1::-1]---------------------------'NOHTYP'
>>> s[-1::-2]---------------------------'NHY'
>>> s[-2::-2]---------------------------'OTP'
>>> s[2:6:][::-1]----------------------'NOHT'
>>> s[2:6:]----------------------------'THON'
------------------------------------------------------------------------------------------------
Examples: RULE-4
>>> s="PYTHON"
>>> print(s)-----------------------------PYTHON
>>> s[2:0:1]-----------------------------' '
>>> s[:0:1]------------------------------' '
>>> s="JAVA PROG"
>>> print(s)----------------------------JAVA PROG
>>> s[:0:1]-----------------------------''
>>> s[:0]------------------------------' '
>>> s[:0:3]--------------------------' '
------------------------------------------------------------------------------------------------
Examples: RULE-5
>>> s="PYTHON"
>>> print(s)--------------------PYTHON
>>> s[::-1]---------------------'NOHTYP'
>>> s[ :-1:-1]------------------' '
>>> s[ :-1:-3]-----------------' '
>>> s[ :-1:-4]------------------' '
------------------------------------------------------------------------------------------------
******************Special Points****************************
>>> "PYTHON"[::-1]----------------------'NOHTYP'
>>> "PYTHON"[::-1]=="PYTHON"-------------False
>>> "PYTHON"[::2]=="PYTHON"[::-1][::2]--------False
>>> "MOM"[::-1]=="MOM"----------------------True
>>> "MOM"[::-1]=="MOM"[::]-----------------True
>>> "LIRIL"[::-1]=="LIRIl"[::-1]--------------False
------------------------------------------------------------------------------------------------

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

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
0
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
assignment where bytearray object belongs to Mutable bcoz bytearray object
supports Item Assignment.

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
0
102
>>> for val in ba[::-1]:
... print(val)
102
0
255
100
56
34
10
>>> for val in ba:
... print(val)
10
34
56
100
255
0
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 assignment on bytearray object--allowed
>>> for val in ba:
... print(val)
123
34
56
100
255
0
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
0
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
0
102
>>> x[1]----------------------34
>>> x[1]=234
>>> for v in x:
... print(v)
123
234
56
100
255
0
102
>>> print(x,type(x),id(x))-----bytearray(b'{\xea8d\xff\x00f') <class 'bytearray'>
2149788291888
------------------------------------------------------------------------------------------------

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

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
1
2
3
4
5
>>> for val in range(6):
... print(val)
...
0
1
2
3
4
5
>>> for val in range(10):
... print(val)
...
0
1
2
3
4
5
6
7
8
9
------------------------------------------------------------------------------------------------

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
1
2
3
4
5
6
7
8
9
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
9
8
7
6
5
4
3
2
1

------------------------------------------------------------------------------------------------
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
0
------------------------------------------------------------------------------------------------
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
0
1
2
3
4
5
>>> for val in range(-5,6):
... print(val)
...
-5
-4
-3
-2
-1
0
1
2
3
4
5
------------------------------------------------------------------------------------------------
>>> 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==================================
====================================================

III. List Category Data Types (Collection or Data


Structures)
====================================================
=>The purpose of List Category Data Types is that "To store Multiple Values
either of same Type OR Different Type OR Both the Types in Single Object
with Unique and Duplicate Values".
------------------------------------------------------------------------------------------------
=>In Python Programming, we have 2 data types in List Category. They are
1. list (mutable)
2. tuple (immutable)
------------------------------------------------------------------------------------------------

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

1. list
==============================================
Index:
=>Properties of list
=>Types of list
a) emty list
b) non-empty list
=>Operations on list
=>Pre-defined functions in list
=>Nested List / Inner List
=>Programming Examples
=========================================================
Properties of list
=>'list' is one of the pre-defined class and treated as list data type.
------------------------------------------------------------------------------------------------
=>The purpose of list data type is that " To store Multiple Values either of same
Type OR Different Type OR Both the Types in Single Object with Unique and
Duplicate Values".
------------------------------------------------------------------------------------------------
=>The Values / Elements of list must be stored / Organized with Square
Brackets [ ] and Values must be separated by comma.
------------------------------------------------------------------------------------------------
=>An object of list maintains Insertion order.
------------------------------------------------------------------------------------------------
=>On object of list, we can perform Both Indexing and Slicing Operations.
------------------------------------------------------------------------------------------------
=>An object of list belongs to Mutable
------------------------------------------------------------------------------------------------
=>W.r.t list class, we can create 2 types of list objects. They are
a) Empty List
b) Non-Empty List
------------------------------------------------------------------------------------------------
a) Empty List:
=>An empty list is one, which does not contain any Elements and whose length
is 0.
=>Syntax: varname=[]
(OR)
varname=list()
------------------------------------------------------------------------------------------------
b) Non-Empty List:
=>An Non-Empty list is one, which contains Elements and whose length is >0.
=>Syntax: varname=[Val1,Val2.....Val-n]
(OR)
varname=list(object)
------------------------------------------------------------------------------------------------
Examples:
>>> l1=[10,20,30,10,40,50,20,10]
>>> print(l1,type(l1))----------------[10, 20, 30, 10, 40, 50, 20, 10] <class 'list'>
>>> l2=[20,"Rossum",34.56,True,2+3j]
>>> print(l2,type(l2))---------------------[20, 'Rossum', 34.56, True, (2+3j)]
<class 'list'>
>>> l2[0]----------------20
>>> l2[-1]-------------(2+3j)
>>> l2[1:4]-----------['Rossum', 34.56, True]
>>> l2[::2]--------------[20, 34.56, (2+3j)]
>>> l2[::-1]-----------[(2+3j), True, 34.56, 'Rossum', 20]
---------------------------
>>> l2=[20,"Rossum",34.56,True,2+3j]
>>> print(l2,type(l2),id(l2))--------[20, 'Rossum', 34.56, True, (2+3j)] <class
'list'> 2705927635008
>>> l2[0]=30
>>> print(l2,type(l2),id(l2))----[30, 'Rossum', 34.56, True, (2+3j)] <class 'list'>
2705927635008
>>> l2[2:4]=[44.55,False]
>>> print(l2,type(l2),id(l2))----[30, 'Rossum', 44.55, False, (2+3j)] <class 'list'>
2705927635008
---------------------------------------
>>> l1=[10,"Rossum",34.56]
>>> print(l1,type(l1),len(l1))----------------[10, 'Rossum', 34.56] <class 'list'>
3
>>> l2=[]
>>> print(l2,type(l2),len(l2))--------------- [] <class 'list' > 0
OR
>>> l3=list()
>>> print(l3,type(l3),len(l3))-------------- [] <class 'list'> 0
-------------------------------------
>>> l1=[10,20,30,40,50,60]
>>> print(l1,type(l1))-------------------------[10, 20, 30, 40, 50, 60] <class 'list'>
>>> b=bytes(l1)
>>> print(b,type(b))-------------------------b'\n\x14\x1e(2<' <class 'bytes'>
>>> l2=list(b)
>>> print(l2,type(l2))---------------------[10, 20, 30, 40, 50, 60] <class 'list'>
>>> s="MISSISSIPPI"
>>> l3=list(s)
>>> print(l3,type(l3))-----['M', 'I', 'S', 'S', 'I', 'S', 'S', 'I', 'P', 'P', 'I'] <class 'list'>
------------------------
>>> a=[10]
>>> print(a,type(a))------------------[10] <class 'list'>
>>> x=100
>>> b=list(x)------------------TypeError: 'int' object is not iterable
To solve the above Error, Use the following
>>> b=list([x])
>>> print(b,type(b))--------------------[100] <class 'list'>
>>> b=list(x,) ------------TypeError: 'int' object is not iterable

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

==================================================
Pre-defined functions in list
==================================================
=>We know that, on the object of list, we can perform Both Indexing and
Slicing Operations.
------------------------------------------------------------------------------------------------
=>Along with Indexing and Slicing Operations, we can Perform Various
Operations by using Pre-Defined Functions present in list object. They are
------------------------------------------------------------------------------------------------
1. append()
------------------------------------------------------------------------------------------------
Syntax: listobj.append(Value)
=>This Function is used for adding the values to list object at end.
Example
>>> lst=[10,"Rossum",34.56]
>>> print(lst,id(lst))----[10, 'Rossum', 34.56] 2705927639808
>>> lst.append("PYTHON")
>>> print(lst,id(lst))----[10, 'Rossum', 34.56, 'PYTHON'] 2705927639808
>>> lst.append(True)
>>> lst.append(1+2.5j)
>>> print(lst,id(lst))----[10, 'Rossum', 34.56, 'PYTHON', True, (1+2.5j)]
2705927639808
------------------------------------------------------------------------------------------------
2. insert()
Syntax: listobj.insert(Index,Value)
=>This Function is used for adding the value to list object at Specified Index

Note: -
=>When we enter Invalid Positive Index then the value inserted at Last/End of
List object
=>When we enter Invalid Negative Index then the value inserted at First of List
object
------------------------------------------------------------------------------------------------
Examples
>>> lst=[10,"Rossum",34.56]
>>> print(lst,id(lst))---------[10, 'Rossum', 34.56] 2705923376704
>>> lst.insert(2,"PYTHON")
>>> print(lst,id(lst))-----------[10, 'Rossum', 'PYTHON', 34.56] 2705923376704
>>> lst[-1]=44.56
>>> print(lst,id(lst))--------[10, 'Rossum', 'PYTHON', 44.56] 2705923376704
>>> lst.insert(2,True)
>>> print(lst,id(lst))-----[10, 'Rossum', True, 'PYTHON', 44.56]
2705923376704
>>> lst.insert(-1,2+3j)
>>> print(lst,id(lst))-----[10, 'Rossum', True, 'PYTHON', (2+3j), 44.56]
2705923376704
-------------------------------------
>>> lst=[10,"Rossum",34.56]
>>> print(lst,id(lst))-----------[10, 'Rossum', 34.56] 2705927639296
>>> lst.insert(10,"PYTHON")
>>> print(lst,id(lst))-----------[10, 'Rossum', 34.56, 'PYTHON'] 2705927639296
>>> lst.insert(-10,"HYD")
>>> print(lst,id(lst))----------['HYD', 10, 'Rossum', 34.56, 'PYTHON']
2705927639296
------------------------------------------------------------------------------------------------
2. clear()
Syntax: listobj.clear()
=>This Function is used for Removing all the elements of Non-Empty List
object
=>When we call clear() on empty list object then we get No Output / None
Examples:
------------------
>>> lst=[10,"Rossum",34.56]
>>> print(lst,id(lst),len(lst))---------------[10, 'Rossum', 34.56] 2705927639808 3
>>> lst.clear()
>>> print(lst,id(lst),len(lst))------------ [] 2705927639808 0
----------------------------------
>>> lst.clear() -------------- No Ouput
(OR)
>>> print(lst.clear())----------None
>>> [].clear()-------------------No Output
(OR)
>>> print([].clear())-------------None
>>> print(list().clear())----------None
------------------------------------------------------------------------------------------------
4. remove () ----Based on Value
------------------------------------------------------------------------------------------------
=>Syntax: listobj.remove(Value)
=>This Function is used for Removing the First Occurrence of Specified
Element of list object.
=>If the Specified Element does not exist in list object then we get ValueError.
------------------------------------------------------------------------------------------------
Examples:
>>> lst=[10,"Rossum",34.56,"Python"]
>>> print(lst,len(lst))--------------------[10, 'Rossum', 34.56, 'Python'] 4
>>> lst.remove("Rossum")
>>> print(lst,len(lst))------------------[10, 34.56, 'Python'] 3
>>> lst.remove(34.56)
>>> print(lst,len(lst))--------------------[10, 'Python'] 2
>>> lst.remove("Python")
>>> print(lst,len(lst))------------------[10] 1
>>> lst.remove(10)
>>> print(lst,len(lst))------------------[] 0
>>> lst.remove("Python")--------------ValueError: list.remove(x): x not in list
>>> list().remove(100)-------------------ValueError: list.remove(x): x not in list
---------------------------------------
>>> lst1=[10,20,30,10,30,"Python",True]
>>> print(lst1,len(lst1))---------------[10, 20, 30, 10, 30, 'Python', True] 7
>>> lst1.remove(10)
>>> print(lst1,len(lst1))--------------[20, 30, 10, 30, 'Python', True] 6
>>> lst1.remove(10)
>>> print(lst1,len(lst1))--------------[20, 30, 30, 'Python', True] 5
>>> lst1.remove(30)
>>> print(lst1,len(lst1))--------------[20, 30, 'Python', True] 4
>>> lst1.remove(30)
>>> print(lst1,len(lst1))--------------[20, 'Python', True] 3
------------------------------------------------------------------------------------------------
5. pop(index)----Index Based
------------------------------------------------------------------------------------------------
=>Syntax: listobj.pop(index)
=>This Function is used for Removing the Element of listobj based on Index.
=>If the Index is invalid then we get IndexError
Examples:
>>> lst1=[10,20,30,10,30,"Python",True]
>>> print(lst1)--------------------------[10, 20, 30, 10, 30, 'Python', True]
>>> lst1.pop(3)-------------------------10
>>> print(lst1)--------------------------[10, 20, 30, 30, 'Python', True]
>>> lst1.pop(-4)-----------------------30
>>> print(lst1)--------------------------[10, 20, 30, 'Python', True]
>>> lst1.pop(0)------------------------10
>>> print(lst1)--------------------------[20, 30, 'Python', True]
>>> lst1.pop(0)------------------------20
>>> print(lst1)------------------------[30, 'Python', True]
>>> lst1.pop(0)----------------------30
>>> print(lst1)-------------------------['Python', True]
>>> lst1.pop(0)-----------------------'Python'
>>> print(lst1)------------------------[True]
>>> lst1.pop(0)-----------------------True
>>> print(lst1)-------------------------[]
>>> lst1.pop(0)--------------------------IndexError: pop from empty list
>>> list().pop(-4)------------------------IndexError: pop from empty list
>>> list().pop(4)-------------------------IndexError: pop from empty list
------------------------------------------------------------------------------------------------
6. pop()
------------------------------------------------------------------------------------------------
Syntax: listobj.pop()
=>This Function is used for Removing always Last Element of List object.
=>If we call pop() on empty list object then we get IndexError
Examples:
>>> lst=[10,"Rossum",34.56,"Python"]
>>> print(lst)----------------------------[10, 'Rossum', 34.56, 'Python']
>>> lst.pop()--------------------------'Python'
>>> print(lst)---------------------------[10, 'Rossum', 34.56]
>>> lst.pop()-------------------------34.56
>>> print(lst)-------------------------[10, 'Rossum']
>>> lst.pop()--------------------------'Rossum'
>>> print(lst)-------------------------[10]
>>> lst.pop()--------------------------10
>>> print(lst)-------------------------[]
>>> lst.pop()--------------------------IndexError: pop from empty list
---------------------------------------------------------------------------
>>> list().pop()----------------IndexError: pop from empty list
>>> [].pop()---------------------IndexError: pop from empty list
------------------------------------------------------------------------------------------------
NOTE: del operator--Most Imp
Syntax1: del objname[Index]---->Removing the element based on Index
Syntax2: del objname[Begin:End:Step]--->Removing the Elements Based in
Slicing Operations
Syntax3: del objname--------->Removing the entire object
Examples
>>> lst=[10,"Sagatika",66.66,"OUCET","HYD"]
>>> print(lst,type(lst),id(lst))-----------[10, 'Sagatika', 66.66, 'OUCET', 'HYD']
<class 'list'> 2302481486656
>>> del lst[-2]
>>> print(lst,type(lst),id(lst))----------[10, 'Sagatika', 66.66, 'HYD'] <class 'list'>
2302481486656
>>> del lst[0:2]
>>> print(lst,type(lst),id(lst))---------[66.66, 'HYD'] <class 'list'>
2302481486656
>>> lst=[10,"Sagatika",66.66,"OUCET","HYD"]
>>> del lst[::2]
>>> print(lst,type(lst),id(lst))--------['Sagatika', 'OUCET'] <class 'list'>
2302485776384
>>> del lst
>>> print(lst,type(lst),id(lst))-----NameError: name 'lst' is not defined.
------------------------------------------------------------------------------------------------
7) index()
------------------------------------------------------------------------------------------------
Syntax: listobj.index(Value)
=>This Function is used for Finding Index of First Occurrence of Specified
Element in List object.
=>if the Specified Element not present in List object, then we get ValueError.
Examples:
>>> lst=[10,20,30,40,10,60,70,10,30]
>>> print(lst)-------------[10, 20, 30, 40, 10, 60, 70, 10, 30]
>>> lst.index(10)---------0
>>> lst.index(20)---------1
>>> lst.index(30)----------2
>>> lst.index(300)----------ValueError: 300 is not in list
>>> list().index(10)---------ValueError: 10 is not in list
>>> [].index(-12)-------------ValueError: -12 is not in list
------------------------------------------------------------------------------------------------
8) copy()----Shallow Copy
------------------------------------------------------------------------------------------------
=>This Function is Used for Copying the content of One Object into another
Object ( Implements Shallow Copy).
=>Syntax: Listobject2=listobj1.copy()
Examples:
>>> lst1=[10,"Rossum",34.56]
>>> print(lst1,type(lst1),id(lst1))------[10, 'Rossum', 34.56] <class 'list'>
2302481827584
>>> lst2=lst1.copy() # Shallow Copy
>>> print(lst2,type(lst2),id(lst2))----[10, 'Rossum', 34.56] <class 'list'>
2302481486656
>>> lst1.append("PYTHON")
>>> print(lst1,type(lst1),id(lst1))---[10, 'Rossum', 34.56, 'PYTHON'] <class
'list'> 2302481827584
>>> print(lst2,type(lst2),id(lst2))---[10, 'Rossum', 34.56] <class 'list'>
2302481486656
>>> lst2.append("NL")
>>> print(lst2,type(lst2),id(lst2))--[10, 'Rossum', 34.56, 'NL'] <class 'list'>
2302481486656
>>> print(lst1,type(lst1),id(lst1))--[10, 'Rossum', 34.56, 'PYTHON'] <class
'list'> 2302481827584
Deep Copy--Examples
>>> lst1=[10,"Rossum",34.56]
>>> print(lst1,type(lst1),id(lst1))---------[10, 'Rossum', 34.56] <class 'list'>
2302481517760
>>> lst2=lst1 # Deep Copy
>>> print(lst2,type(lst2),id(lst2))---[10, 'Rossum', 34.56] <class 'list'>
2302481517760
>>> lst1.append("HYD")
>>> print(lst1,type(lst1),id(lst1))---[10, 'Rossum', 34.56, 'HYD'] <class 'list'>
2302481517760
>>> print(lst2,type(lst2),id(lst2))---[10, 'Rossum', 34.56, 'HYD'] <class 'list'>
2302481517760
>>> lst2.remove("Rossum")
>>> print(lst1,type(lst1),id(lst1))---[10, 34.56, 'HYD'] <class 'list'>
2302481517760
>>> print(lst2,type(lst2),id(lst2))---[10, 34.56, 'HYD'] <class 'list'>
2302481517760
------------------------------------------------------------------------------------------------
9. count()
------------------------------------------------------------------------------------------------
=>Syntax: listobj1.count(Value)
=>This Function is used for finding / Counting Number of occurrences of
Specified Value of List object.
=>If the Specified Value does not exist in list object, then we get 0 as a Result.
Examples
>>> lst=[10,20,30,10,20,50,60,70,10]
>>> print(lst)-----------[10, 20, 30, 10, 20, 50, 60, 70, 10]
>>> lst.count(10)-------3
>>> lst.count(20)-------2
>>> lst.count(30)-------1
>>> lst.count(40)-------0
>>> lst.count("PYTHON" )-----0
>>> list().count(10)----------0
>>> [].count(10)-------------0
------------------------------------------------------------------------------------------------
10. reverse()
------------------------------------------------------------------------------------------------
Syntax: listobj.reverse()
=>This Function is used for reversing(Front elements to back and back elements
to Front) the elements of list object in same list itself.
------------------------------------------------------------------------------------------------
Examples:
>>> lst1=[10,"Rossum",34.56,"PYTHON"]
>>> print(lst1,id(lst1))-----------[10, 'Rossum', 34.56, 'PYTHON']
2302485781376
>>> lst1.reverse()
>>> print(lst1,id(lst1))-----------['PYTHON', 34.56, 'Rossum', 10]
2302485781376
-----------------------
>>> lst2=[10,20,30,100,200,300]
>>> print(lst2,id(lst2))--------[10, 20, 30, 100, 200, 300] 2302485776384
>>> lst2.reverse()
>>> print(lst2,id(lst2))-------[300, 200, 100, 30, 20, 10] 2302485776384
---------------------------------------
>>> lst2=[10,20,30,100,200,300]
>>> lst3=lst2.reverse()
>>> print(lst2,id(lst2))---------[300, 200, 100, 30, 20, 10] 2302481517760
>>> print(lst3)--------------None
>>> list().reverse() # No Output
(OR)
>>> print(list().reverse())--------None

------------------------------------------------------------------------------------------------
11) extend()
------------------------------------------------------------------------------------------------
Syntax: listobj1.extend(listobj2)
=>This Function is used for Merging / Combining The values of listobj2 with
ListObj1. Hence Listobj1 contains Its Own Elements and Elements of listobj2.
Syntax: listobj1=listobj1+ listobj2+ ........+listobj-n
=>By using + Operator also we can Merge OR Combine Multiple elements of
list objects
Examples:
>>> lst1=[10,20,30,40]
>>> lst2=["Python","Java"]
>>> lst3=["Rossum","Gosling"]
>>> lst1.extend(lst2,lst3)------------TypeError: list.extend() takes exactly one
argument (2 given)
#### TO Solve the above Error
>>> lst1.extend(lst2)
>>> lst1.extend(lst3)
>>> print(lst1)-------[10, 20, 30, 40, 'Python', 'Java', 'Rossum', 'Gosling']
------------------------------
OR
------------------------------
>>> lst1=[10,20,30,40]
>>> lst2=["Python","Java"]
>>> lst3=["Rossum","Gosling"]
>>> lst1=lst1+lst2+lst3 # Used + Operartor for Merging
>>> print(lst1)-----[10, 20, 30, 40, 'Python', 'Java', 'Rossum', 'Gosling']
------------------------------------------------------------------------------------------------
12) sort()
------------------------------------------------------------------------------------------------
Syntax1: listobj.sort()----->Sorts the given List data in Ascending Order
Syntax2: listobj.sort(revserse=False)----->Sorts the given List data in
Ascending Order
Syntax3: listobj.sort(reverse=True)--->Sort the given List data in Descending
Order
Examples:
>>> lst=[10,-2,12,56,13,-7,45,6]
>>> print(lst,id(lst))--------[10, -2, 12, 56, 13, -7, 45, 6] 2302485781696
>>> lst.sort()
>>> print(lst,id(lst))------[-7, -2, 6, 10, 12, 13, 45, 56] 2302485781696
>>> #------------------------------------
>>> lst=[10,-2,12,56,13,-7,45,6]
>>> print(lst,id(lst))------------[10, -2, 12, 56, 13, -7, 45, 6] 2302481486656
>>> lst.sort()
>>> print(lst,id(lst))---------[-7, -2, 6, 10, 12, 13, 45, 56] 2302481486656
>>> lst.reverse()
>>> print(lst,id(lst))----------[56, 45, 13, 12, 10, 6, -2, -7] 2302481486656
>>> #-----------------------------------------
>>> lst=[10,-2,12,56,13,-7,45,6]
>>> print(lst,id(lst))----------[10, -2, 12, 56, 13, -7, 45, 6] 2302485781696
>>> lst.sort(reverse=True)
>>> print(lst,id(lst))--------[56, 45, 13, 12, 10, 6, -2, -7] 2302485781696
>>> #------------------------------------------
>>> lst=[10,-2,12,56,13,-7,45,6]
>>> print(lst,id(lst))----------[10, -2, 12, 56, 13, -7, 45, 6] 2302481486656
>>> lst.sort(reverse=False)
>>> print(lst,id(lst))-----------[-7, -2, 6, 10, 12, 13, 45, 56] 2302481486656
>>> #-----------------------------------------------
>>> lst=["Trump","Zaki","Biden","Putin","Rossum","Alen"]
>>> print(lst)----------['Trump', 'Zaki', 'Biden', 'Putin', 'Rossum', 'Alen']
>>> lst.sort(reverse=True)
>>> print(lst)------------['Zaki', 'Trump', 'Rossum', 'Putin', 'Biden', 'Alen']
>>> #-------------------------------------------------
>>> lst=["Trump","Zaki","Biden","Putin","Rossum","Alen"]
>>> print(lst)----------['Trump', 'Zaki', 'Biden', 'Putin', 'Rossum', 'Alen']
>>> lst.sort()
>>> print(lst)-------['Alen', 'Biden', 'Putin', 'Rossum', 'Trump', 'Zaki']
>>> #---------------------------------------------------
>>> lst=[10,"Trump",33.33,2+3j,True]
>>> print(lst)---------[10, 'Trump', 33.33, (2+3j), True]
>>> lst.sort()----------TypeError: '<' not supported between instances of 'str'
and 'int'
==================================x======================
===============================================
Inner or Nested List
===============================================
=>The Process of Defining one list inside of another list is called Inner or
Nested List
=>Syntax:- listobj=[ Val1, Val2....[Val11,Val12...Val1n],
[Val21,Val22..Val2n...], Val-n ]
=>Here Val1,Val2...Val-n are called Values of Outer List
=>Here Val11,Val12...Val-1n are called Values of one Inner List
=>Here Val21,Val22...Val-2n are called Values of another Inner List
=>In inner list we can perform Both Indexing and Slicing Operations
=>On Inner List, we can apply all pre-defined function of list.
Examples:
>>> lst=[100,"Karthik",[18,16,20],[76,75,66],"OUCET"]
>>> print(lst)-----------------[100, 'Karthik', [18, 16, 20], [76, 75, 66], 'OUCET']
>>> lst[-1]--------------------'OUCET'
>>> lst[-2]------------------------[76, 75, 66]
>>> lst[-3]--------------------------[18, 16, 20]
>>> lst[1]-----------------------------------'Karthik'
>>> lst[0]-----------------------------------100
>>> print(lst[2],type(lst[2]))----------------[18, 16, 20] <class 'list'>
>>> print(lst[-2],type(lst[-2]))-------------[76, 75, 66] <class 'list'>
>>> lst[2][0]------------------------------18
>>> lst[2][1]------------------------------16
>>> lst[2][1]=17
>>> print(lst)-----------------[100, 'Karthik', [18, 17, 20], [76, 75, 66], 'OUCET']
>>> lst[2].append(16)
>>> print(lst)---------------[100, 'Karthik', [18, 17, 20, 16], [76, 75, 66],
'OUCET']
>>> lst[-2].insert(-2,80)
>>> print(lst)--------------[100, 'Karthik', [18, 17, 20, 16], [76, 80, 75, 66],
'OUCET']
>>> del lst[-2]
>>> print(lst)--------------[100, 'Karthik', [18, 17, 20, 16], 'OUCET']
>>> lst[2].clear()
>>> print(lst)----------------[100, 'Karthik', [], 'OUCET']
>>> del lst[2]
>>> print(lst)---------------------[100, 'Karthik', 'OUCET']
>>> lst.insert(2,[16,15,18,17])
>>> print(lst)--------------------------[100, 'Karthik', [16, 15, 18, 17], 'OUCET']
>>> lst.insert(-1,[77,66,78,55])
>>> print(lst)-----------------[100, 'Karthik', [16, 15, 18, 17], [77, 66, 78, 55],
'OUCET']
>>> lst[2].sort()
>>> print(lst)-------------------[100, 'Karthik', [15, 16, 17, 18], [77, 66, 78, 55],
'OUCET']
>>> lst[-2].sort(reverse=True)
>>> print(lst)--------------------[100, 'Karthik', [15, 16, 17, 18], [78, 77, 66, 55],
'OUCET']
Examples:
>>> studlist=[100,"DLPrince",[18,16,19],[78,66,79],"OUCET"]
>>> print(studlist,type(studlist))--[100, 'DLPrince', [18, 16, 19], [78, 66, 79],
'OUCET'] <class 'list'>
>>> print(studlist[2],type(studlist[2]))---[18, 16, 19] <class 'list'>
>>> print(studlist[-2],type(studlist[-2]))---[78, 66, 79] <class 'list'>
>>> studlist[-3].append(17)
>>> print(studlist,type(studlist))--[100, 'DLPrince', [18, 16, 19, 17], [78, 66,
79],] <class 'list'>
>>> studlist[3].append(68)
>>> print(studlist,type(studlist))---[100, 'DLPrince', [18, 16, 19, 17], [78, 66,
79, 68], 'OUCET'] <class 'list'>
>>> studlist[2].sort()
>>> print(studlist,type(studlist))---[100, 'DLPrince', [16, 17, 18, 19], [78, 66,
79, 68], 'OUCET'] <class 'list'>
>>> studlist[3].sort(reverse=True)
>>> print(studlist,type(studlist))----[100, 'DLPrince', [16, 17, 18, 19], [79, 78,
68, 66],'OUCET'] <class 'list'>
>>> studlist[2].pop(-2)----18
>>> studlist[-2].remove(68)
>>> print(studlist,type(studlist))--[100, 'DLPrince', [16, 17, 19], [79, 78, 66],
'OUCET'] <class 'list'>
>>> studlist[2].clear()
>>> print(studlist,type(studlist))--[100, 'DLPrince', [], [79, 78, 66], 'OUCET']
<class 'list'>
>>> studlist[2].append(14)
>>> print(studlist,type(studlist))---[100, 'DLPrince', [14], [79, 78, 66],
'OUCET'] <class 'list'>
>>> del studlist[2]
>>> del studlist[-2]
>>> print(studlist,type(studlist))---[100, 'DLPrince', 'OUCET'] <class 'list'>
>>> studlist.insert(2,[17,15,14,19])
>>> print(studlist,type(studlist))--[100, 'DLPrince', [17, 15, 14, 19], 'OUCET']
<class 'list'>
>>> studlist.append([66,78,65,76])
>>> print(studlist,type(studlist))---[100, 'DLPrince', [17, 15, 14, 19], 'OUCET',
[66, 78, 65, 76]] <class 'list'>
>>> studlist[-2]---'OUCET'
>>> len(studlist)----5
>>> len(studlist[2])---4
>>> len(studlist[-1])---4
===========================X==========================
=====================================================
II. TUPLE ()
=====================================================
Properties
=>'tuple' is one of the pre-defined class and treated as list data type
=>The purpose of tuple data type is that " To store Multiple Values either of
same Type OR Different Type OR Both the Types in Single Object with Unique
and Duplicate Values".
=>The Values / Elements of tuple must be stored / Organized with Braces ( )
and Values must be separated by comma.
=>An object of tuple maintains Insertion order.
=>On object of tuple, we can perform Both Indexing and Slicing Operations.
=>An object of tuple belongs to Immutable
=>W.r.t tuple class, we can create 2 types of tuple objects. They are
a) Empty tuple
b) Non-Empty tuple

a) Empty tuple:
=>An empty tuple is one, which does not contain any Elements and whose
length is 0.
=>Syntax: varname=()
(OR)
varname=tuple()
b) Non-Empty tuple:
=>A non-empty tuple is one, which contains Elements and whose length is >0.
=>Syntax: varname=(Val1,Val2.....Val-n)
(OR)
=>Syntax: varname=Val1,Val2.....Val-n
(OR)
varname=tuple(object)

------------------------------------------------------------------------------------------------
NOTE: The Functionality of tuple is exactly similar to Functionality of list but
list object belongs to Mutable and tuple object belongs to Immutable.
------------------------------------------------------------------------------------------------
Examples:
>>> t1= (10,20,30,10,50,60,70)
>>> print (t1, type(t1)) ----------------(10, 20, 30, 10, 50, 60, 70) <class 'tuple'>
>>> t2=(100,"Travis",33.33,"HYD")
>>> print (t2, type(t2)) -----------(100, 'Travis', 33.33, 'HYD') <class 'tuple'>
>>> len(t1) -----------7
>>> len(t2) ----------4
>>> t3=100,"Rossum",44.44,"PYTHON"
>>> print (t3, type(t3)) -----(100, 'Rossum', 44.44, 'PYTHON') <class 'tuple'>
-----------------------------------------------------------------------------------------------
>>> t1= ()
>>> print (t1, type(t1)) ------------() <class 'tuple'>
>>> len(t1) -------------------------0
>>> t2=tuple ()
>>> print (t2, type(t2)) -----------() <class 'tuple'>
>>> len(t2) ------------------------0
-----------------------------------------------------------------------------------------------
>>> t=(100,"Rossum",44.44,"PYTHON")
>>> print(type(t),id(t))--------(100, 'Rossum', 44.44, 'PYTHON') <class 'tuple'>
2605398318096
>>> t[0]--------------100
>>> t[1]--------------'Rossum'
>>> t[-1]------------'PYTHON'
>>> t[0]=200-----------Type Error: 'tuple' object does not support item
assignment
------------------------------------------------------------------------------------------------
>>> l1=[100,"Rossum",44.44,"PYTHON"]
>>> print(l1,type(l1))-----------[100, 'Rossum', 44.44, 'PYTHON'] <class 'list'>
>>> t1=tuple(l1)
>>> print(t1,type(t1))----------(100, 'Rossum', 44.44, 'PYTHON') <class 'tuple'>
>>> l2=list(t1)
>>> print(l2,type(l2))----------[100, 'Rossum', 44.44, 'PYTHON'] <class 'list'>
-----------------------------------------------------------------------------------------------
>>> t=(100,"Rossum",44.44,"PYTHON")
>>> print(t,type(t))-----------(100, 'Rossum', 44.44, 'PYTHON') <class 'tuple'>
>>> t[0:3]-----------(100, 'Rossum', 44.44)
>>> t[::-1]-------------('PYTHON', 44.44, 'Rossum', 100)
Special Points
(special Conversions)
>>> a=10
>>> t=tuple(a)--------------TypeError: 'int' object is not iterable
>>> t=tuple(a,)-------------TypeError: 'int' object is not iterable
>>> t=tuple((a))-------------TypeError: 'int' object is not iterable
>>> t=tuple([a])
>>> print(t,type(t))--------------(10,) <class 'tuple'>
--------OR-------------
>>> b=100
>>> t=(b,)
>>> print(t,type(t))-----------(100,) <class 'tuple'>
-----------------------------------------------------------------------------------------------
>>> b=12.34
>>> t=(b)
>>> print(t,type(t))------------12.34 <class 'float'>
>>> t=(b,)
>>> print(t,type(t))------------(12.34,) <class 'tuple'>
*----------------------------------------------X---------------------------------------------*
When we should go for tuple?
To
=========================================================

Pre-defined Function in tuple


=========================================================
=>We know that on the object of tuple we can perform Both Indexing and
Slicing Operations.
=>Along with these operations, we can also perform other operations by using
the following pre-defined Functions.
1)index ()
2)count ()
Examples:
>>> t1=(10,"RS",45.67)
>>> print(t1,type(t1))------------(10, 'RS', 45.67) <class 'tuple'>
>>> t1.index(10)---------0
>>> t1.index("RS")------1
>>> t1=(10,"RS",45.67)
>>> print(t1,type(t1))-------(10, 'RS', 45.67) <class 'tuple'>
>>> t1.count(10)-------1
>>> t1.count(100)------0
>>> t1=(10,0,10,10,20,0,10)
>>> print(t1,type(t1))---------(10, 0, 10, 10, 20, 0, 10) <class 'tuple'>
>>> t1.count(10)---------------4
>>> t1.count(0)-----------------2
>>> t1.count(100)--------------0

Below Functions not present in tuple: -


append()
insert()
remove()
clear()
pop(index)
pop()
reverse()
sort()
copy()
extend()
Note: If we try above methods, it will throw attribute error (AttributeError:
'tuple' object has no attribute 'append')
------------------------------------------------------------------------------------------------
NOTE: - By Using del Operator, we can’t delete values of tuple object by
using Indexing and slicing but we can delete entire object.
------------------------------------------------------------------------------------------------
Examples:
>>> t1=(10,-34,0,10,23,56,76,21)
>>> print(t1,type(t1))--------------(10, -34, 0, 10, 23, 56, 76, 21) <class 'tuple'>
>>> del t1[0]------TypeError: 'tuple' object doesn't support item deletion
>>> del t1[0:4]----TypeError: 'tuple' object does not support item deletion
>>> del t1 # Here we are removing complete object.
>>> print(t1,type(t1))-----NameError: name 't1' is not defined.

------------------------------------------------------------------------------------------------
MOST IMP:
sorted(): This Function is used for Sorting the data of immutable object tuple
and gives the sorted data in the form of list.
=>Syntax: listobj=sorted(tuple object)
------------------------------------------------------------------------------------------------
Examples:
>>> t1=(10,23,-56,-1,13,15,6,-2)
>>> print(t1,type(t1))------------(10, 23, -56, -1, 13, 15, 6, -2) <class 'tuple'>
>>> t1.sort()----------------------AttributeError: 'tuple' object has no attribute
'sort'
>>> x=sorted(t1)
>>> print(x,type(x))-----------[-56, -2, -1, 6, 10, 13, 15, 23] <class 'list'>
>>> print(t1,type(t1))----------(10, 23, -56, -1, 13, 15, 6, -2) <class 'tuple'>
>>> t1=tuple(x) # Converted sorted list into tuple
>>> print(t1,type(t1))---------(-56, -2, -1, 6, 10, 13, 15, 23) <class 'tuple'>
>>> t2=t1[::-1]
>>> print(t2,type(t2))------(23, 15, 13, 10, 6, -1, -2, -56) <class 'tuple'>
OR
>>> t1=(10,-4,12,34,16,-6,0,15)
>>> print(t1,type(t1))---------------------(10, -4, 12, 34, 16, -6, 0, 15) <class
'tuple'>
>>> l1=list(t1)
>>> print(l1,type(l1))-----------------[10, -4, 12, 34, 16, -6, 0, 15] <class 'list'>
>>> l1.sort()
>>> print(l1,type(l1))-------------------[-6, -4, 0, 10, 12, 15, 16, 34] <class 'list'>
>>> t1=tuple(l1)
>>> print(t1,type(t1))---------------(-6, -4, 0, 10, 12, 15, 16, 34) <class 'tuple'>
>>>t1=t1[::-1]
>>> print(t1,type(t1))----------------(34, 16, 15, 12, 10, 0, -4, -6) <class 'tuple'>
============================ x =======================
==================================================
Inner (OR) Nested tuple
==================================================
=>The Process of Defining One tuple in another tuple is called Inner or Nested
tuple
=>Syntax:- tplobj1=( Val1,Val2....(Val11,Val12....Val1n).....
(Val21,Val22...Val2n)..........Val-n )
=>Here (Val11,Val12....Val1n) is called One Inner OR Nested tuple
(Val21,Val22...Val2n) is called another Inner OR Nested tuple
=> ( Val1,Val2....(Val11,Val12....Val1n).....(Val21,Val22...Val2n)....Val-n ) is
called Outer tuple
=>All the pre-defined Functions of tuple can be applied on Inner or Nested
tuple.
=>On Inner or Nested tuple we can perform Index and Slicing Operations.
------------------------------------------------------------------------------------------------
Examples:
>>> sf=(10,"RS",(17,18,16),(78,66,79),"OUCET")
>>> print(sf,type(sf))------------(10, 'RS', (17, 18, 16), (78, 66, 79), 'OUCET')
<class 'tuple'>
>>> print(sf[0])-----10
>>> print(sf[2],type(sf[2]),type(sf))---------(17, 18, 16) <class 'tuple'> <class
'tuple'>
>>> print(sf[0:3])------------(10, 'RS', (17, 18, 16))
------------------------------------------------------------------------------------------------
>>> sf=(10,"RS",[17,18,16],(78,66,79),"OUCET")
>>> print(sf,type(sf))----------------(10, 'RS', [17, 18, 16], (78, 66, 79), 'OUCET')
<class 'tuple'>
>>> print(sf[2],type(sf[2]),type(sf))------------[17, 18, 16] <class 'list'> <class
'tuple'>
>>> sf[2].append(12)
>>> print(sf[2],type(sf[2]),type(sf))------------[17, 18, 16, 12] <class 'list'>
<class 'tuple'>
>>> print(sf,type(sf))---------(10, 'RS', [17, 18, 16, 12], (78, 66, 79), 'OUCET')
<class 'tuple'>
>>> sf[3].append(12)---AttributeError: 'tuple' object has no attribute 'append'
------------------------------------------------------------------------------------------------
>>> sf=[10,"RS",[17,18,16],(78,66,79),"OUCET"]
>>> print(sf,type(sf))-------------[10, 'RS', [17, 18, 16], (78, 66, 79), 'OUCET']
<class 'list'>
>>> print(sf[2],type(sf[2]),type(sf))--------[17, 18, 16] <class 'list'> <class 'list'>
>>> print(sf[3],type(sf[3]),type(sf))-------(78, 66, 79) <class 'tuple'> <class
'list'>
------------------------------------------------------------------------------------------------
NOTE:
=>One can define One List in another List
=>One can define One Tuple in another Tuple
=>One can define One List in another Tuple ( tuple of lists)
=>One can define One tuple in another List (list of tuples)
------------------------------------------------------------------------------------------------
>>> print(t1,type(t1))
(10, 'Rossum', [16, 18, 17], ('CSE', 'AI', 'DS'), 'OUCET') <class 'tuple'>
>>> print(t1[2],type(t1[2]))-------[16, 18, 17] <class 'list'>
>>> print(t1[3],type(t1[3]))------('CSE', 'AI', 'DS') <class 'tuple'>
>>> t1[2].append("KVR")
>>> print(t1,type(t1))--(10, 'Rossum', [16, 18, 17, 'KVR'], ('CSE', 'AI', 'DS'),
'OUCET') <class 'tuple'>
>>> t1[3].append("Python")-----AttributeError: 'tuple' object has no attribute
'append'
>>> t1[2][1]--------18
>>> t1[2][-2]------17
>>> t1[2][-3]------18
>>> k=sorted(t1[-2])
>>> k------------['AI', 'CSE', 'DS']
>>> t1[3]=k---------TypeError: 'tuple' object does not support item assignment
>>> l1=list(t1)----
>>> l1----------[10, 'Rossum', [16, 18, 17, 'KVR'], ('CSE', 'AI', 'DS'), 'OUCET']
>>> l1[3]=k
>>> l1-----[10, 'Rossum', [16, 18, 17, 'KVR'], ['AI', 'CSE', 'DS'], 'OUCET']
>>> y=tuple(l1[3])
>>> l1[3]=y
>>> l1-----[10, 'Rossum', [16, 18, 17, 'KVR'], ('AI', 'CSE', 'DS'), 'OUCET']
>>> t2=tuple(l1)
>>> t2-----(10, 'Rossum', [16, 18, 17, 'KVR'], ('AI', 'CSE', 'DS'), 'OUCET')
------------------------------------------------------------------------------------------------
>>> t1=('AI', 'cSE', 'DS')
>>> k=sorted(t1)
>>> k-------['AI', 'DS', 'cSE']

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

Set Category Data Types (Collection or Data Structures)

====================================================
=>The purpose of Set Category Data Types is that " To store Multiple Values
either of Same Type or Different Type or Both the Types in Single Object with
Unique Values (No Duplicates are allowed)."
=>We have 2 data type in Set Category. They are

1. set (mutable and immutable)


2. frozenset (immutable)
================================x========================

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

1. set
==========================================
Properties
=>'set' is one of the pre-defined class and treated as set data type.
=>The purpose of set data type is that " To store Multiple Values either of Same
Type or Different Type or Both the Types in Single Object with Unique Values
(No Duplicates are allowed).
=>The Elements of set must be stored within Curly Braces { } and the elements
must be separated by comma.
=>An object of set does not maintain Insertion Order bcoz PVM displays any
possibility of set elements.
=>On the object set, we can't perform Both Indexing and Slicing Operations
bcoz set never maintains Insertion order.
=>An object of set belongs to Both Mutable in the case of Add and immutable
in the case of Item Assigment.
=>w.r.t set , we can create Two Types of set objects. They are
a) Empty Set
b) Non-Empty Set
------------------------------------------------------------------------------------------------
a) Empty Set
=>An Empty Set is One, which does not contain any Elements and whose
length is 0
=>Syntax: varname=set()
------------------------------------------------------------------------------------------------
b) Non-Empty Set
=>An Nn-Empty Set is One, which contains Elements and whose length is>0
=>Syntax1: varname={Val1,Val2,...Val-n}
=>Syntax2: varname=set(object)
------------------------------------------------------------------------------------------------
Examples
>>> s1={10,20,30,40,10,50,60,20}
>>> print(s1,type(s1))---------------{50, 20, 40, 10, 60, 30} <class 'set'>
>>> print(s1,type(s1))--------------{50, 20, 40, 10, 60, 30} <class 'set'>
>>> s2={10,"Travis",44.44,True,"Numpy"}
>>> print(s2,type(s2))-----------{'Numpy', True, 'Travis', 10, 44.44} <class 'set'>
----------------------
>>> s2={10,"Travis",44.44,True,"Numpy"}
>>> print(s2,type(s2))-----------{'Numpy', True, 'Travis', 10, 44.44} <class 'set'>
>>> s2[0]----------------TypeError: 'set' object is not subscriptable
>>> s2[0:3]---------------TypeError: 'set' object is not subscriptable
----------------------------------------
>>> s2={10,"Travis",44.44,True,"Numpy"}
>>> print(s2,type(s2))----------{'Numpy', True, 'Travis', 10, 44.44} <class 'set'>
>>> s2[0]=100-------------TypeError: 'set' object does not support item
assignment--IMMUTABLE
------------------------------------------------
>>> s2={10,"Travis",44.44,True,"Numpy"}
>>> print(s2,type(s2),id(s2))-------------{'Numpy', True, 'Travis', 10, 44.44}
<class 'set'> 2485370854016
>>> s2.add("HYD") # MUTABLE
>>> print(s2,type(s2),id(s2))----{'Numpy', True, 'Travis', 10, 44.44, 'HYD'}
<class 'set'> 2485370854016
------------------------------
>>> x={}
>>> print(x,type(x))--------------{} <class 'dict'>
>>> s=set()
>>> print(s,type(s),len(s))---------- set() <class 'set'> 0
------------------------------------
>>> l1=[10,20,30,10]
>>> print(l1,type(l1))-------------[10, 20, 30, 10] <class 'list'>
>>> s1=set(l1)
>>> print(s1,type(s1))------------{10, 20, 30} <class 'set'>
-----------------------------------
>>> s="MISSISSIPPI"
>>> s1=set(s)
>>> print(s1)-------------------{'M', 'S', 'P', 'I'}
=========================X==============================
===================================================
Pre-Functions in set
===================================================
=>On the object of set, we can perform different type of Operations by using
Functions present in ste object. They are
------------------------------------------------------------------------------------------------
1) clear()
------------------------------------------------------------------------------------------------
=>Syntax: setobj.clear()
=>This Function is used for Removing all the values of set object.
Examples:
>>> s1={10,20,30,40,50,10}
>>> print(s1,type(s1),id(s1))----------{50, 20, 40, 10, 30} <class 'set'>
2485370854240
>>> s1.clear()
>>> print(s1,type(s1),id(s1))--------set() <class 'set'> 2485370854240
------------------------------------
>>> print(s1,len(s1))----------set() 0
>>> s1.clear()---------------- No Output
>>> print(s1.clear())----------None
------------------------------------------------------------------------------------------------
2) add()
------------------------------------------------------------------------------------------------
Syntax: setobj.add(Value)
=>This Function is used for adding the values to set object.
Examples:
>>> s1={10,"Rossum",34.56,"HYD"}
>>> print(s1,type(s1),id(s1))--------{10, 'Rossum', 34.56, 'HYD'} <class 'set'>
2485370853568
>>> s1.add("NL")
>>> print(s1,type(s1),id(s1))-------{34.56, 'Rossum', 'NL', 10, 'HYD'} <class
'set'> 2485370853568
>>> s1.add(True)
>>> s1.add(2+3j)
>>> print(s1,type(s1),id(s1))-----{True, 34.56, 'Rossum', 'NL', 10, (2+3j),
'HYD'} <class 'set'> 2485370853568
--------------------------------------
>>> s1=set()
>>> print(s1,type(s1),id(s1))------------set() <class 'set'> 2485370854240
>>> s1.add(100)
>>> s1.add("Naresh")
>>> s1.add(12345)
>>> s1.add("Python")
>>> print(s1,type(s1),id(s1))-----{'Naresh', 12345, 'Python', 100} <class 'set'>
2485370854240
------------------------------------------------------------------------------------------------
3) remove()
------------------------------------------------------------------------------------------------
Syntax: setobj.remove(Value)
=>This Function is used for removing the values of setobject.
=>if the specified Values does not exist in set object then we get KeyError
Examples:
>>> s1={10,"Rossum",34.56,"HYD"}
>>> print(s1,id(s1))----------{10, 'Rossum', 34.56, 'HYD'} 2485370853568
>>> s1.remove(10)
>>> print(s1,id(s1))-----------{'Rossum', 34.56, 'HYD'} 2485370853568
>>> s1.remove("HYD")
>>> print(s1,id(s1))------------{'Rossum', 34.56} 2485370853568
>>> s1.remove("BANG")-------------KeyError: 'BANG'
>>> set().remove(100)---------------KeyError: 100
------------------------------------------------------------------------------------------------
4) discard()
------------------------------------------------------------------------------------------------
Syntax: setobj.discard(Value)
=>This Function is used for Removing the Vaue from setobject.
=>If the Specified Value does not exist then we never get any Error
Examples:
>>> s1={10,"Ankit",34.56,"Python","HYD"}
>>> print(s1,type(s1),id(s1))-----------{'Ankit', 34.56, 'HYD', 10, 'Python'}
<class 'set'> 2705097791904
>>> s1.discard(10)
>>> print(s1,type(s1),id(s1))-------{'Ankit', 34.56, 'HYD', 'Python'} <class 'set'>
2705097791904
>>> s1.discard("HYD")
>>> print(s1,type(s1),id(s1))-------{'Ankit', 34.56, 'Python'} <class 'set'>
2705097791904
>>> s1.discard("Python")
>>> print(s1,type(s1),id(s1))---------{'Ankit', 34.56} <class 'set'>
2705097791904
>>> s1.discard(1000) # we wont' get KeyError
>>> print(s1,type(s1),id(s1))--------{'Ankit', 34.56} <class 'set'>
2705097791904
>>> s1.remove(1000)KeyError: 1000
------------------------------------------------------------------------------------------------
5) pop()
------------------------------------------------------------------------------------------------
Syntax: setobj.pop()
=>This Function is used for Removing Any Arbitrary Element from non-empty
set object
=>When we call pop() upon empty set object then we get KeyError
Example-1 ---set elemements are given and order of display not shown and
pop() removes Arbitrary Element always
>>> s1={10,"Ankit",34.56,"Python","HYD"}
>>> s1.pop()--------------'Ankit'
>>> s1.pop()-------------34.56
>>> s1.pop()-------------'HYD'
>>> s1.pop()-------------10
>>> s1.pop()------------'Python'
>>> print(s1)------------set()
>>> s1.pop()----------KeyError: 'pop from an empty set'
>>> set().pop()-------KeyError: 'pop from an empty set'
--------------------------------------------------------------------------------
Example-2: set elemements are given and order of disply shown and pop()
removes First Element always
--------------------------------------------------------------------------------
>>> s1={100,200,300,150,450,-120}
>>> print(s1,id(s1))--------------{450, 100, -120, 150, 200, 300} 2705097791232
>>> s1.pop()-----------450
>>> s1.pop()-----------100
>>> s1.pop()------------120
>>> s1.pop()-----------150
>>> s1.pop()-----------200
>>> s1.pop()-----------300
>>> print(s1,id(s1))--------set() 2705097791232
>>> s1.pop()---------------------KeyError: 'pop from an empty set'
------------------------------------------------------------------------------------------------
6) isdisjoint()
------------------------------------------------------------------------------------------------
=>Syntax: setobj1.isdisjoint(setobj2)
=>This Function Returns True Provided There is No Common Element in
setobj1 and setobj2.
=>This Function Returns False Provided There is atleast one Common Element
in setobj1 and setobj2.
Examples:
>>> s1={10,20,30,40}
>>> s2={15,25,35}
>>> s3={10,100,200,300}
>>> s1.isdisjoint(s2)---------------True
>>> s1.isdisjoint(s3)---------------False
>>> s2.isdisjoint(s3)---------------True
------------------------------------------------------------------------------------------------
7) issubset()
------------------------------------------------------------------------------------------------
=>Syntax: setobj1.issubset(setobj2)
=>This function returns True Provided All the elements of setobj1 present in
setobj2 (OR) setobj2 contains all the elements of setobj1
=>This function returns False Provided at least one element of setobj1 not
present in setobj2 (OR) setobj2 not containing at least one element of setobj1.
Examples:
>>> s1={0,1,2,3,4,5,6,7,8,9}
>>> s2={3,4,5}
>>> s3={10,20,30}
>>> s2.issubset(s1)-----------True
>>> s3.issubset(s1)-----------False
>>> s1.issubset(s3)------------False
>>> s4={1,15,25}
>>> s4.issubset(s1)--------False
>>> s4={1,2,3,10}
>>> s4.issubset(s1)-------False
------------------------------------------------------------------------------------------------
8) issuperset()
------------------------------------------------------------------------------------------------
=>Syntax: setobj1.issuperset(setobj2)
=>This function returns True Provided setobj1 contains all the elements of
setobj2 (OR) all elements of setobj2 present in setobj1
=>This function returns False Provided at least one element of setobj2 not
present in setobj1 (OR) setobj1 not containing at least one element of setobj2.
Examples:
>>> s1={0,1,2,3,4,5,6,7,8,9}
>>> s2={3,4,5}
>>> s3={10,20,30}
>>> s1.issuperset(s2)----------True
>>> s1.issuperset(s3)----------False
>>> s4={1,2,3,10}
>>> s1.issuperset(s4)----------False
------------------------------------------------------------------------------------------------
9) union()
------------------------------------------------------------------------------------------------
=>Syntax: setobj3=setobj1.union(setobj2)
(OR)
=>Syntax: setobj3=setobj2.union(setobj1)
=>This Function is used for Taking all Unique Elements of setobj1 and setobj2
and place them in setobj3.
Examples:
>>> cp={"Sachin","Rohit","Kohli"}
>>> tp={"Kohli","Rossum","Travis"}
>>> print(cp,type(cp))------------{'Sachin', 'Kohli', 'Rohit'} <class 'set'>
>>> print(tp,type(tp))-------------{'Rossum', 'Travis', 'Kohli'} <class 'set'>
>>> allcptp=cp.union(tp)
>>> print(allcptp,type(allcptp))----{'Sachin', 'Kohli', 'Rohit', 'Rossum', 'Travis'}
<class 'set'>
(OR)
>>> allcptp=tp.union(cp)
>>> print(allcptp,type(allcptp))---{'Sachin', 'Kohli', 'Rohit', 'Rossum', 'Travis'}
<class 'set'>
------------------------------------------------------------------------------------------------
10) intersection()
------------------------------------------------------------------------------------------------
=>Syntax: setobj3=setobj1.intersection(setobj2)
(OR)
=>Syntax: setobj3=setobj2.intersection(setobj1)
=>This Function is used for Taking Common Elements of setobj1 and setobj2
and place them in setobj3.
Examples
>>> cp={"Sachin","Rohit","Kohli"}
>>> tp={"Kohli","Rossum","Travis"}
>>> print(cp,type(cp))-----------------{'Sachin', 'Kohli', 'Rohit'} <class 'set'>
>>> print(tp,type(tp))------------------{'Rossum', 'Travis', 'Kohli'} <class 'set'>
>>> bothcptp=cp.intersection(tp)
>>> print(bothcptp,type(bothcptp))--------{'Kohli'} <class 'set'>
OR
>>> bothcptp=tp.intersection(cp)
>>> print(bothcptp,type(bothcptp))-------{'Kohli'} <class 'set'>
--------------------------
>>> a={10,20}
>>> b={30,40}
>>> a.intersection(b)----------set()
>>> {10,20,30}.intersection({'a','e','i','o','u'})-----------set()
>>> {10,20,30}.intersection({'a','e','i','o','u',10})---------{10}
------------------------------------------------------------------------------------------------
11) difference()
------------------------------------------------------------------------------------------------
Syntax1: setobj3=setobj1.difference(setobj2)
=>This Function Removes the common Elements from setobj1 and setobj2 and
It takes Remaining Elements from setobj1 and placed in setobj3.
Examples:
>>> cp={"Sachin","Rohit","Kohli"}
>>> tp={"Kohli","Rossum","Travis"}
>>> print(cp,type(cp))-----------------{'Sachin', 'Kohli', 'Rohit'} <class 'set'>
>>> print(tp,type(tp))------------------{'Rossum', 'Travis', 'Kohli'} <class 'set'>
>>> onlycp=cp.difference(tp)
>>> print(onlycp,type(onlycp))-------{'Sachin', 'Rohit'} <class 'set'>
------------------------------------------
Syntax2: setobj3=setobj2.difference(setobj1)
=>This Function Removes the common Elements from setobj2 and setobj1 and
It takes Remaining Elements from setobj2 and placed in setobj3.
Examples:
>>> cp={"Sachin","Rohit","Kohli"}
>>> tp={"Kohli","Rossum","Travis"}
>>> print(cp,type(cp))-----------------{'Sachin', 'Kohli', 'Rohit'} <class 'set'>
>>> print(tp,type(tp))------------------{'Rossum', 'Travis', 'Kohli'} <class 'set'>
>>> onlytp=tp.difference(cp)
>>> print(onlytp,type(onlytp))-----------{'Rossum', 'Travis'} <class 'set'>
------------------
>>> a={10,20}
>>> b={10,30,40,20}
>>> a.difference(b)-----------set()
>>> b.difference(a)-----------{40, 30}
------------------------------------------------------------------------------------------------
12) symmetric_difference()
------------------------------------------------------------------------------------------------
Syntax: setobj3=setobj1.symmetric_difference(setobj2)
(OR)
Syntax: setobj3=setobj2.symmetric_difference(setobj1)
=>This Function Removes the common Elements from setobj1 and setobj2 and
It takes Remaining Elements from both setobj1 and setobj2 and place them in
setobj3.
Examples
>>> cp={"Sachin","Rohit","Kohli"}
>>> tp={"Kohli","Rossum","Travis"}
>>> print(cp,type(cp))--------------{'Sachin', 'Kohli', 'Rohit'} <class 'set'>
>>> print(tp,type(tp))---------------{'Rossum', 'Travis', 'Kohli'} <class 'set'>
>>> exclcptp=cp.symmetric_difference(tp)
>>> print(exclcptp,type(exclcptp))---------{'Rohit', 'Travis', 'Sachin', 'Rossum'}
<class 'set'>
>>> exclcptp=tp.symmetric_difference(cp)
>>> print(exclcptp,type(exclcptp))----------{'Rohit', 'Travis', 'Sachin', 'Rossum'}
<class 'set'>
OR
>>> exclcptp=tp.symmetric_difference(cp)
>>> print(exclcptp)------------{'Rohit', 'Travis', 'Sachin', 'Rossum'}
(OR)
>>> exclcptp=cp.union(tp).difference(cp.intersection(tp))
>>> print(exclcptp)-------{'Rossum', 'Sachin', 'Travis', 'Rohit'}

Special Points
>>> s1={10,20,30,40}
>>> s2={10,20,50,60}
>>> s3=s1.difference(s2)
>>> print(s3)------------{40, 30}
>>> s4=s2.difference(s1)
>>> print(s4)---------{50, 60}
>>> s5=s1.symmetric_difference(s2)
>>> print(s5)---------{40, 50, 60, 30}
(OR)
>>> s6=s1.union(s2).difference(s1.intersection(s2))
>>> print(s6)-----------{40, 50, 60, 30}
(OR)
>>> s7=s1.difference(s2).union(s2.difference(s1))
>>> print(s7)------------{40, 50, 60, 30}
=========================================================
MOST IMP---Perform the above Set Operations without set Functions by
Using Bitwise Operators
>>> cp={"Sachin","Rohit","Kohli"}
>>> tp={"Kohli","Rossum","Travis"}
>>> print(cp,type(cp))--------------{'Sachin', 'Kohli', 'Rohit'} <class 'set'>
>>> print(tp,type(tp))---------------{'Rossum', 'Travis', 'Kohli'} <class 'set'>
>>> allcptp=cp|tp # Bitwise OR Operator OR union()
>>> print(allcptp)-----------{'Sachin', 'Kohli', 'Rohit', 'Rossum', 'Travis'}
>>> botcptp=cp&tp # Bitwise AND Operator OR intersection()
>>> print(bothcptp)-----{'Kohli'}
>>> onlycp=cp-tp # - Operator OR difference()
>>> print(onlycp,type(onlycp))------------{'Sachin', 'Rohit'} <class 'set'>
>>> onlytp=tp-cp
>>> print(onlytp,type(onlytp))-------------{'Rossum', 'Travis'} <class 'set'>
>>> exclcptp=cp^tp # Bitwise XOR Operator OR symmetric_difference()
>>> print(exclcptp,type(exclcptp))-------{'Rohit', 'Travis', 'Sachin', 'Rossum'}
<class 'set'>
------------------------------------------------------------------------------------------------
13) update()
------------------------------------------------------------------------------------------------
=>Syntax: setobj1.update(setobj2)
=>This Function is used Merging setobj2 values with setobj1
Examples:
>>> s1={10,20,30,40}
>>> s2={"Python","Java"}
>>> print(s1,type(s1))--------{40, 10, 20, 30} <class 'set'>
>>> print(s2,type(s2))--------{'Python', 'Java'} <class 'set'>
>>> s1.update(s2)
>>> print(s1)-----------------{20, 40, 10, 'Python', 'Java', 30}
--------------------------
>>> s1={10,20,30,40}
>>> s2={"Python","Java",10}
>>> s1.update(s2)
>>> print(s1)------------------{20, 40, 10, 'Python', 'Java', 30}
=================================x=======================
==============================================
Nested or Inner Properties of Set
===============================================
=>We can define

a) Defining set inside of another set NOT POSSIBLE


b) Defining set inside of another Tuple POSSIBLE
c) Defining set inside of another List POSSIBLE
d) Defining tuple inside of Another Set POSSIBLE
e) Defining list inside of another set NOT POSSIBLE
Examples:
>>> s1={10,"Rossum",{10,15,16},"OUCET"}-----TypeError: unhashable type:
'set'
>>> s1={10,"Rossum",[10,15,16],"OUCET"}----TypeError: unhashable type:
'list'
>>> s1={10,"Rossum",(10,15,16),"OUCET"}-----Valid
>>> print(s1,type(s1))----{'Rossum', 10, 'OUCET', (10, 15, 16)} <class 'set'>
--------------------
>>> t1=(10,"Rossum",{10,15,16},"OUCET")
>>> print(t1[2],type(t1[2]),type(t1))----{16, 10, 15} <class 'set'> <class 'tuple'>
>>> l1=[10,"Rossum",{10,15,16},"OUCET"]
>>> print(l1[2],type(l1[2]),type(l1))----{16, 10, 15} <class 'set'> <class 'list'>
====================================x====================

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

frozenset

==================================================
Properties
=>'frozenset' is one of the Pre-defined class and treated as set data type.
=>The purpose of frozenset data type is that " To store Multiple Values either of
Same Type or Different Type or Both the Types in Single Object with Unique
Values (No Duplicates are allowed).
=>The Elements of frozenset set must be obtained from list,tuple and set by
using a Type casting Function frozenset()
=>An object of frozenset does not maintain Insertion Order bcoz PVM displays
any possibility of frozenset elements.
=>On the object frozenset, we can't perform Both Indexing and Slicing
Operations bcoz frozenset never maintains Insertion order.
=>An object of frozenset belongs to immutable in the case of Item Assigment.
and no functions are allowed to modify/ update the values of frozenset object.
=>w.r.t frozenset , we can create Two Types of frozenset objects. They are
a) Empty frozenset
b) Non-Empty frozenset
------------------------------------------------------------------------------------------------
a) Empty frozenset
=>An Empty frozenset is One, which does not contain any Elements and whose
length is 0
=>Syntax: varname=frozenset()
------------------------------------------------------------------------------------------------
b) Non-Empty frozenSet
=>An Nn-Empty Set is One, which contains Elements and whose length is > 0
=>Syntax1: varname=frozenset( [val1,val2...val-n])
(OR)
=>Syntax2: varname=frozenset( (val1,val2...val-n) )
(OR)
=>Syntax3: varname=frozenset( {val1,val2...val-n})
------------------------------------------------------------------------------------------------
NOTE:- The functionality of frozenset is exactly similar to set type but an
object of set belongs to Both Mutable(in the case add() and other Functions) and
Immutable (in the case of Item Assignment), where as an object of frozenset
belongs to Immutable bcoz Item Assigment does not support and No Functions
are allowed which are modifying the values of frozenset.
------------------------------------------------------------------------------------------------
Examples:
>>> fs=frozenset()
>>> print(fs,type(fs),len(fs))---------frozenset() <class 'frozenset'> 0
>>> fs=frozenset([10,20,10,10,10,10])
>>> print(fs,type(fs),len(fs))---------frozenset({10, 20}) <class 'frozenset'> 2
>>> fs=frozenset((10,20,10,10,10,10))
>>> print(fs,type(fs),len(fs))-------------frozenset({10, 20}) <class 'frozenset'> 2
>>> fs=frozenset({10,20,10,10,10,10})
>>> print(fs,type(fs),len(fs))--------------frozenset({10, 20}) <class 'frozenset'> 2
--------------------------------------------------------
>>> fs=frozenset([10,"Travis",45.67,True])
>>> print(fs,type(fs))----------------frozenset({True, 10, 45.67, 'Travis'}) <class
'frozenset'>
>>> fs[0]------------------------------------TypeError: 'frozenset' object is not
subscriptable
>>> fs[0:2]----------------------------------TypeError: 'frozenset' object is not
subscriptable
>>> fs[0]=100-----------------TypeError: 'frozenset' object does not support item
assignment
>>> fs.add(100)--------------AttributeError: 'frozenset' object has no attribute
'add'
================================x========================
=====================================
Pre-Defined Functions in frozenset
=====================================
=>frozenset contains the following Functions
a) copy()
b) isdisjoint()
c) issuperset()
d) issubset()
e) union()
f) intersection()
g) difference()
h) symmertic_difference()
NOTE:
>>> fs1=frozenset({10,20,30,409})
>>> print(fs1,type(fs1),id(fs1))--------frozenset({409, 10, 20, 30}) <class
'frozenset'>
2068835340960
>>> fs2=fs1.copy()
>>> print(fs2,type(fs2),id(fs2))-----frozenset({409, 10, 20, 30}) <class
'frozenset'>
2068835340960

=>In General, Immutable Object content is Not Possible to copy( in the case of
tuple). Where as in the case of frozenset, we are able to copy its content to
another frozenset object. Here Original frozenset object and copied frozenset
object contains Same Address and Not at all possible to Modify / Change their
content.
Examples:
>>> s1={10,"RS",3.4,"PYTHON"}
>>> fs1=frozenset(s1)
>>> print(fs1,type(fs1),id(fs1))---------frozenset({10, 3.4, 'RS', 'PYTHON'})
<class 'frozenset'> 1774317640096
>>> fs2=fs1.copy()
>>> print(fs2,type(fs2),id(fs2))-------frozenset({10, 3.4, 'RS', 'PYTHON'})
<class 'frozenset'> 1774317640096

=>frozenset does not contain the following Functions


a) clear()
b) add()
c) remove()
d) discard()
e) pop()
f) update()
=========================================x===============

=====================================================
V) Dict Category Data Types (Collection Data Types or Data Structures)
===================================================
Properties
=>'dict' is one of the pre-defined class and treated as Dict Data Type.
=>The purpose of dict data type is that "To store (Key, value) in single variable"
=>In (Key, Value), the values of Key is Unique and Values of Value may or may
not be unique.
=>The (Key, value) must be organized or stored in the object of dict within
Curly Braces {} and they separated by comma.
=>An object of dict does not support Indexing and Slicing bcoz Values of Key
itself considered as Indices.
=>In the object of dict, Values of Key are treated as Immutable and Values of
Value are treated as mutable.
=>Originally an object of dict is mutable bcoz we can add (Key, Value)
externally.
=>We have two types of dict objects. They are
a) Empty dict
b) Non-empty dict
------------------------------------------------------------------------------------------------
a) Empty dict
=>Empty dict is one, which does not contain any (Key, Value) and whose length
is 0
=>Syntax:- dictobj1= { }
or
dictobj=dict()

=>Syntax for adding (Key, Value) to empty dict:


----------------------------------------------------------------
dictobj[Key1]=Val1
dictobj[Key2]=Val2
---------------------------
dictobj[Key-n]=Val-n
Here Key1, Key2...Key-n are called Values of Key and They must be Unique
Here Val1, Val2...Val-n are called Values of Value and They may or may not be
unique.
------------------------------------------------------------------------------------------------
b) Non-Empty dict
=>Non-Empty dict is one, which contains (Key, Value) and whose length is >0
=>Syntax: - dictobj1= {Key1:Val1, Key2:Val2......Key-n:Valn}

Here Key1, Key2...Key-n are called Values of Key and They must be Unique
Here Val1, Val2...Val-n are called Values of Value and They may or may not be
unique.
=========================================================
Examples:
>>> d1= {10:"Python",20:"Data Sci",30:"Django"}
>>> print (d1, type(d1)) -----{10: 'Python', 20: 'Data Sci', 30: 'Django'} <class
'dict'>
>>> d2= {10:3.4,20:4.5,30:5.6,40:3.4}
>>> print (d2, type(d2)) --------{10: 3.4, 20: 4.5, 30: 5.6, 40: 3.4} <class 'dict'>
>>> len(d1) --------------3
>>> len(d2) ------------4
------------------------------------------------------------------------------------------------
>>> d3={}
>>> print (d3, type(d3)) ------------{} <class 'dict'>
>>> len(d3) -------------0
>>> d4=dict()
>>> print(d4,type(d4))-------------{} <class 'dict'>
>>> len(d4) ---------------0
------------------------------------------------------------------------------------------------
>>> d2={10:3.4,20:4.5,30:5.6,40:3.4}
>>> print(d2) ----------------------------------{10: 3.4, 20: 4.5, 30: 5.6, 40: 3.4}
#Indexing is not allowed
>>> d2[0] ----------------------------------------KeyError: 0
>>> d2[10] -------------------------------------3.4
>>> d2[10] =10.44
>>> print(d2) -------------------------------{10: 10.44, 20: 4.5, 30: 5.6, 40: 3.4}
-----------------------------------------------------------------------------------------------
>>> d2= {10:3.4,20:4.5,30:5.6,40:3.4}
>>> print (d2, type(d2), id(d2)) ----{10: 3.4, 20: 4.5, 30: 5.6, 40: 3.4} <class
'dict'> 2090750380736
>>> d2[50] =5.5
>>> print (d2, type(d2), id(d2)) ---{10: 3.4, 20: 4.5, 30: 5.6, 40: 3.4, 50: 5.5}
<class 'dict'> 2090750380736
------------------------------------------------------------------------------------------------
>>> d3= {}
>>> print (d3, type(d3), id(d3)) -------------------{} <class 'dict'>2090750332992
>>> d3["Python”] =1
>>> d3["Java”] =3
>>> d3["C”] =2
>>> d3["GO”] =1
>>> print (d3, type(d3), id(d3)) -----{'Python': 1, 'Java': 3, 'C': 2, 'GO': 1} <class
'dict'> 2090750332992
------------------------------------------------------------------------------------------------
>>> d4=dict ()
>>> print (d4, type(d4), id(d4)) ---------------{} <class 'dict'> 2090754532032
>>> d4[10] ="Apple"
>>> d4[20] ="Mango"
>>> d4[30] ="Kiwi"
>>> d4[40] ="Sberry"
>>> d4[50] ="Orange"
>>> print(d4,type(d4),id(d4))---{10: 'Apple', 20: 'Mango', 30: 'Kiwi', 40:
'Sberry', 50: 'Orange'}<class 'dict'> 2090754532032
>>> d4[10]="Guava"
>>> print(d4,type(d4),id(d4))----{10: 'Guava', 20: 'Mango', 30: 'Kiwi', 40:
'Sberry', 50: 'Orange'} <class 'dict'> 2090754532032
--------------------------------------------------------------------------------------
>>> d2={10:3.4,20:4.5,30:5.6,40:3.4}
>>> print(d2, type(d2),id(d2))---{10: 3.4, 20: 4.5, 30: 5.6, 40: 3.4} <class 'dict'>
2090754531520
>>> d2[50]=1.2
>>> print(d2, type(d2),id(d2))---{10: 3.4, 20: 4.5, 30: 5.6, 40: 3.4, 50: 1.2}
<class 'dict'> 2090754531520
====================================================
Pre-Defined functions in dict
====================================================
=>In dict object, we have the following function for performing Various
Operations.
------------------------------------------------------------------------------------------------
1. clear ()
------------------------------------------------------------------------------------------------
Syntax: dictobj.clear()
=>This Function is used for Removing all the elements of non-empty dict
object.
=>If we call this function upon empty dict object then we get None / No
Output.
Examples
>>> d1={10:"Python",20:"Java",30:"C++",40:"C"}
>>> print (d1, type(d1), id(d1)) ---{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
<class 'dict'> 2364664861568
>>> d1. clear ()
>>> print (d1, type(d1), id(d1)) ----{} <class 'dict'> 2364664861568
>>> print (d1. clear ()) ----------None
>>> print (dict (). clear ()) ---------None
>>> print ({}. clear ()) --------------None
------------------------------------------------------------------------------------------------
2. pop ()
------------------------------------------------------------------------------------------------
=>Syntax: dictobj.pop(Key)

=>This Function is used for Removing (Key, value) from non-empty dictobject
by passing Value of Key.
=>If the Value of Key does not exist then we get KeyError.
=>If we call this pop (), upon empty dict object then we get KeyError.
Examples:
>>> d1={10:"Python",20:"Java",30:"C++",40:"C"}
>>> print(d1,id(d1))---------------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
2364669155776
>>> d1.pop(20)--------------------'Java'
>>> print(d1,id(d1))--------------{10: 'Python', 30: 'C++', 40: 'C'}
2364669155776
>>> d1.pop(30)------------------'C++'
>>> d1.pop(40)0---------------'C'
>>> print(d1,id(d1))--------------{10: 'Python'} 2364669155776
>>> d1.pop(50)------------------------KeyError: 50
>>> print(d1,id(d1))--------{10: 'Python'} 2364669155776
>>> d1.pop(10)-------------'Python'
>>> print(d1,id(d1))--------{} 2364669155776
>>> d1.pop(10)-----------KeyError: 10
>>> dict().pop(10)----------------KeyError: 10
>>> {}.pop("Python")-----KeyError: 'Python'
------------------------------------------------------------------------------------------------
3. popitem()
------------------------------------------------------------------------------------------------
Syntax: dictobj.popitem()
=>This Function is used for Removing Last (Key,value) entry from non-empty
dictobject.
=>If we call this popitem(), upon empty dict object then we get KeyError.
Examples:
>>> d1={10:"Python",20:"Java",30:"C++",40:"C"}
>>> print(d1,id(d1))-------------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
2364669156096
>>> d1.popitem()---------------(40, 'C')
>>> print(d1,id(d1))------------{10: 'Python', 20: 'Java', 30: 'C++'}
2364669156096
>>> d1.popitem()--------------(30, 'C++')
>>> print(d1,id(d1))----------{10: 'Python', 20: 'Java'} 2364669156096
>>> d1.popitem()-----------(20, 'Java')
>>> print(d1,id(d1))-------------{10: 'Python'} 2364669156096
>>> d1.popitem()-------------(10, 'Python')
>>> print(d1,id(d1))--------------{} 2364669156096
>>> d1.popitem()-----------KeyError: 'popitem(): dictionary is empty'
>>> dict().popitem()----------KeyError: 'popitem(): dictionary is empty'
>>> {}.popitem()-----------KeyError: 'popitem(): dictionary is empty'
------------------------------------------------------------------------------------------------
4. copy ()
------------------------------------------------------------------------------------------------
Syntax: dictobj2=dictobj1.copy()
=>This function is used for Copying the content of One dict object into another
dict object.
Examples:
>>> d1={10:"Python",20:"Java",30:"C++",40:"C"}
>>> print(d1,id(d1))---------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
2364669156416
>>> d2=d1.copy() # Shallow Copy
>>> print(d2,id(d2))----{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
2364669156096
>>> d1[50]="Dsc"
>>> d2[60]="Django"
>>> print(d1,id(d1))----{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C', 50: 'Dsc'}
2364669156416
>>> print(d2,id(d2))---{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C', 60: 'Django'}
2364669156096
------------------------------------------------------------------------------------------------
Deep Copy Process of dict objects
------------------------------------------------------------------------------------------------
>>> d1={10:"Python",20:"Java",30:"C++",40:"C"}
>>> print(d1,id(d1))---------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
2364669155776
>>> d2=d1 # Deep Coy
>>> print(d2,id(d2))---------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
2364669155776
>>> d1[50]="Dsc"
>>> print(d1,id(d1))---------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C', 50: 'Dsc'}
2364669155776
>>> print(d2,id(d2))---------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C', 50: 'Dsc'}
2364669155776
------------------------------------------------------------------------------------------------
5. get ()
------------------------------------------------------------------------------------------------
Syntax: This Function is used for obtaining Value of Value by Passing Value
of Key.
=>if the value of Key does not exist then we get None as a result
(OR)
Syntax: dictobj[Key]---->Will give Value of Value if Key Present otherwise
get KeyError
Examples:
>>> d1={10:"Python",20:"Java",30:"C++",40:"C"}
>>> print(d1,id(d1))-------------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
2364669156096
>>> v=d1.get(10)
>>> print(v)------------Python
>>> v=d1.get(20)
>>> print(v)-----------Java
#OR
>>> d1.get(10)--------'Python'
>>> d1.get(20)--------'Java'
>>> print(d1.get(200))-------None
>>> v=d1.get(200)
>>> print(v)--------------None
>>> print(dict().get(10))---------None
>>> print({}.get(10))------------None
-------------------------OR----------------------------
>>> d1={10:"Python",20:"Java",30:"C++",40:"C"}
>>> print(d1,id(d1))-----------------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
2364669156288
>>> d1.get(10)--------------'Python'

>>> d1[10]--------------------'Python'
>>> d1[20]---------------------'Java'
>>> d1[40]--------------------'C'
>>> d1[400]-------------------KeyError: 400
>>> print(d1.get(400))--------None
------------------------------------------------------------------------------------------------
6. keys()
------------------------------------------------------------------------------------------------
Syntax: varname=dictobj.keys()
(OR)
dictobj.keys()
=>This Function is used for obtaining set of Keys in the form of dict_keys
object.
Examples:
>>> d1={10:"Python",20:"Java",30:"C++",40:"C"}
>>> print(d1,id(d1))----------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
2364669156096
>>> ks=d1.keys()
>>> print(ks,type(ks))---------dict_keys([10, 20, 30, 40]) <class 'dict_keys'>
>>> for k in ks:
... print(k)
...
10
20
30
40
>>> for k in d1.keys():
... print(k)
...
10
20
30
40
>>> for k in d1.keys():
... print(k,"-->",d1.get(k))
...
10 --> Python
20 --> Java
30 --> C++
40 --> C
>>> for k in d1.keys():
... print(k,"-->",d1[k])
...
10 --> Python
20 --> Java
30 --> C++
40 --> C
------------------------------------------------------------------------------------------------
7. values()
------------------------------------------------------------------------------------------------
Syntax: varname=dictobj.values()
(OR)
dictobj.values()

=>This Function is used for obtaining set of Values in the form of dict_values
object.
Examples
>>> d1={10:"Python",20:"Java",30:"C++",40:"C"}
>>> print(d1,id(d1))---------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
2364669156288
>>> vs=d1.values()
>>> print(vs)---------dict_values(['Python', 'Java', 'C++', 'C'])
>>> for v in vs:
... print(v)
Python
Java
C++
C
>>> for v in d1.values():
... print(v)
Python
Java
C++
C
---------------------------------
MOST IMP
---------------------------------
>>> for x in d1:
... print(x)
10
20
30
40

>>> for x in d1:


... print(x,"-->",d1[x])
10 --> Python
20 --> Java
30 --> C++
40 --> C
------------------------------------------------------------------------------------------------
8. items()
------------------------------------------------------------------------------------------------
Syntax: varname=d1.items()
This Function is used for obtaining (Key,value) in the form of tuples of list in
the object of dict_items.
Examples:
>>> d1={10:"Python",20:"Java",30:"C++",40:"C"}
>>> kvs=d1.items()
>>> print(kvs)-------dict_items([(10, 'Python'), (20, 'Java'), (30, 'C++'), (40,
'C')])
>>> for kv in kvs:
... print(kv)
...
(10, 'Python')
(20, 'Java')
(30, 'C++')
(40, 'C')
>>> for kv in d1.items():
... print(kv)
...
(10, 'Python')
(20, 'Java')
(30, 'C++')
(40, 'C')
>>> for k,v in d1.items():
... print(k,"--->",v)
...
10 ---> Python
20 ---> Java
30 ---> C++
40 ---> C
------------------------------------------------------------------------------------------------
9. update()
------------------------------------------------------------------------------------------------
Syntax: dictobj1.update(dictobj2)
=>This Function isd used for Joining / Updating / Modifying the dictobj1
content with DictObj2 content.
Exmaples:
>>> d1={10:1.2,20:3.4,30:4.5}
>>> d2={100:"Apple",200:"Kiwi"}
>>> print(d1,type(d1))------------{10: 1.2, 20: 3.4, 30: 4.5} <class 'dict'>
>>> print(d2,type(d2))--------------{100: 'Apple', 200: 'Kiwi'} <class 'dict'>
>>> d1.update(d2)
>>> print(d1,type(d1))---------{10: 1.2, 20: 3.4, 30: 4.5, 100: 'Apple', 200:
'Kiwi'} <class 'dict'>
>>> print(d2,type(d2))------------{100: 'Apple', 200: 'Kiwi'} <class 'dict'>
---------------------------------------------------
>>> d1={10:1.2,20:3.4,30:4.5}
>>> d2={100:"Apple",20:13.4}
>>> print(d1,type(d1))-----------{10: 1.2, 20: 3.4, 30: 4.5} <class 'dict'>
>>> print(d2,type(d2))-----------{100: 'Apple', 20: 13.4} <class 'dict'>
>>> d1.update(d2)
>>> print(d1,type(d1))--------{10: 1.2, 20: 13.4, 30: 4.5, 100: 'Apple'} <class
'dict'>
>>> print(d2,type(d2))--------{100: 'Apple', 20: 13.4} <class 'dict'>
-------------------------------------------
>>> d1={10:1.2,20:3.4,30:4.5}
>>> d2={10:11.2,20:13.4,30:14.5}
>>> print(d1,type(d1))-----------{10: 1.2, 20: 3.4, 30: 4.5} <class 'dict'>
>>> print(d2,type(d2))-----------{10: 11.2, 20: 13.4, 30: 14.5} <class 'dict'>
>>> d1.update(d2)
>>> print(d1,type(d1))------------{10: 11.2, 20: 13.4, 30: 14.5} <class 'dict'>
>>> print(d2,type(d2))------------{10: 11.2, 20: 13.4, 30: 14.5} <class 'dict'>
==========================x==============================
Most Imp Points
------------------------------------------------------------------------------------------------
=>Dict in Dict--->Possible to write
------------------------------------------------------------------------------------------------
>>> d1={"sno":10,"sname":"Rossum","imarks":
{"cm":17,"cppm":16,"pym":18},"emarks":
{"cm":67,"cppm":78,"pym":70},"cname":"OUCET"}
>>> print(d1,type(d1))
{'sno': 10, 'sname': 'Rossum', 'imarks': {'cm': 17, 'cppm': 16, 'pym': 18},
'emarks': {'cm': 67, 'cppm': 78, 'pym': 70}, 'cname': 'OUCET'} <class 'dict'>
>>> for k in d1.keys():
... print(k)
...
sno
sname
imarks
emarks
cname
>>> for v in d1.values():
... print(v)
...
10
Rossum
{'cm': 17, 'cppm': 16, 'pym': 18}
{'cm': 67, 'cppm': 78, 'pym': 70}
OUCET
>>> for v in d1.values():
... print(v,type(v))
...
10 <class 'int'>
Rossum <class 'str'>
{'cm': 17, 'cppm': 16, 'pym': 18} <class 'dict'>
{'cm': 67, 'cppm': 78, 'pym': 70} <class 'dict'>
OUCET <class 'str'>
>>> for kv in d1.items():
... print(kv)
...
('sno', 10)
('sname', 'Rossum')
('imarks', {'cm': 17, 'cppm': 16, 'pym': 18})
('emarks', {'cm': 67, 'cppm': 78, 'pym': 70})
('cname', 'OUCET')
>>> for k in d1.keys():
... print(k)
...
sno
sname
imarks
emarks
cname
>>> for k in d1.keys():
... print(k,"--->",d1.get(k))
...
sno ---> 10
sname ---> Rossum
imarks ---> {'cm': 17, 'cppm': 16, 'pym': 18}
emarks ---> {'cm': 67, 'cppm': 78, 'pym': 70}
cname ---> OUCET
>>> for v in d1.get("imarks"):
... print(v)
...
cm
cppm
pym
>>> for v in d1.get("imarks"):
... print(v,"-->",d1.get("imarks").get(v))
...
cm --> 17
cppm --> 16
pym --> 18
>>> for v in d1.get("imarks"):
... print(v,"-->",d1["imarks"][v])
...
cm --> 17
cppm --> 16
pym --> 18
=========================================================
 In side of dict, we define list,tuple and set also
------------------------------------------------------------------------------------------------
Examples:
>>> d1={"sno":10,"sname":"Rossum","imarks":[16,18,17],"emarks":
(60,77,67),"crs":{"B.Tech(cse)", "M.Tech(AI)"}, "cname":"OUECT"}
>>> for k,v in d1.items():
... print(k,"-->",v,"--->",type(v))
...
sno --> 10 ---> <class 'int'>
sname --> Rossum ---> <class 'str'>
imarks --> [16, 18, 17] ---> <class 'list'>
emarks --> (60, 77, 67) ---> <class 'tuple'>
crs --> {'B.Tech(cse)', 'M.Tech(AI)'} ---> <class 'set'>
cname --> OUECT ---> <class 'str'>
=========================================================
dict in list is Possible
------------------------------------------------------------------------------------------------
Examples
>>> l1=[10,"Rossum",{"cm":16,"cppm":17,"pym":18},"OUCET"]
>>> print(l1,type(l1))---[10, 'Rossum', {'cm': 16, 'cppm': 17, 'pym': 18},
'OUCET'] <class 'list'>
>>> for val in l1:
... print(val,type(val))
...
10 <class 'int'>
Rossum <class 'str'>
{'cm': 16, 'cppm': 17, 'pym': 18} <class 'dict'>
OUCET <class 'str'>
>>> for k,v in l1[2].items():
... print(k,"--->",v)
...
cm ---> 16
cppm ---> 17
pym ---> 1
 dict in tuple is Possible
------------------------------------------------------------------------------------------------

Examples:
>>> t1=(10,"Rossum",{"cm":16,"cppm":17,"pym":18},"OUCET")
>>> print(t1,type(t1))----------(10, 'Rossum', {'cm': 16, 'cppm': 17, 'pym': 18},
'OUCET') <class 'tuple'>
>>> for val in t1:
... print(val,type(val))
...
10 <class 'int'>
Rossum <class 'str'>
{'cm': 16, 'cppm': 17, 'pym': 18} <class 'dict'>
OUCET <class 'str'>
>>> for k,v in t1[-2].items():
... print(k,"-->",v)
...
cm --> 16
cppm --> 17
pym --> 18
 dict in set not possible
------------------------------------------------------------------------------------------------
>>> s1={10,"Rossum",{"cm":16,"cppm":17,"pym":18},"OUCET"}--
TypeError: unhashable type: 'dict'
====================================================

Displaying the Result of Python Program on the Console


====================================================
=>To Display the Result of Python Program on the Console, we use a pre-
defined Function called print ().
=>In Otherwards, print () is used for Display the Result of Python Program on
the Console.
=>we can print () with the following Syntaxes
=========================================================
Syntax-1 : print(Variable)
OR
print(Var1,Var2,....Var-n)
=========================================================
=>This Syntax generates either Single Variable Val or Multiple variable values.
=>Examples:
>>> a=10
>>> print(a)------10
>>> b=20
>>> print(b)------20
>>> c=a+b
>>> print(a,b,c)-----10 20 30
=========================================================
Syntax-2: print(Message)
(OR)
print(msg1,msg2,...msg-n)
=========================================================
=>Here Message, msg1,msg2....msg-n are called str type
=>This Syntax display the Message(s)
Examples
>>> print("hello Python world")----------hello Python world
>>> print('hello Python world')-----------hello Python world
>>> print('''hello Python world''')-------hello Python world
>>> print("""hello Python world""")-----hello Python world
>>> print("Hello","Python","World")-------Hello Python World
>>> print("Hello" + "Python" + "World")---HelloPythonWorld
>>> print("Hello"+" "+"Python"+" "+"World")---Hello Python World
NOTE: Here + operator is used for concatenation of str values
>>> print("Python"+3.11)---TypeError: can only concatenate str (not
"float") to str
>>> print("Python"+str(3.11))---------Python3.11
=========================================================
Syntax-3: print(Messages Cum Values)
(OR)
print(Values cum Messages)
=========================================================
=>This Syntax displays Values Cum Messages OR Messages Cum Values.
Examples:
>>> a=10
>>> print("Val of a="+a)--------TypeError: can only concatenate str (not "int")
to str
>>> print("Val of a="+str(a))-----Val of a=10
OR
>>> print("Val of a=",a)-----------Val of a= 10
OR
>>> a=10
>>> print(a,"is the value of a")-------10 is the value of a
>>> print(str(a)+" is the value of a")--10 is the value of a
---------------------------
>>> a=10
>>> b=20
>>> c=a+b
>>> print("sum=",c)-----------sum= 30
>>> print(c,"is the sum")-------30 is the sum
>>> print("sum of",a,"and",b,"=",c)-------sum of 10 and 20 = 30
-----------------------------------------------------------------------------------
>>> a=10
>>> b=20
>>> c=30
>>> d=a+b+c
>>> print("Sum of",a,",",b,"and",c,"=",d)---------Sum of 10 , 20 and 30 = 60
=========================================================
Syntax-4: print(Messages Cum Values with format() )
(OR)
print(Values cum Messages with format() )
=========================================================
=>This Syntax displays Values Cum Messages OR Messages Cum Values with
format()
Examples
>>> a=10
>>> print("Val of a={}".format(a) )--------Val of a=10
>>> print("{} is the value of a”. Format(a))----10 is the value of a
-------------------
>>> sno=10
>>> name="Ram"
>>> print("My Number is {} and name is {}".format(sno,name))--My Number
is 10 and name is Ram
-----------------------
>>> a=10
>>> b=20
>>> c=a+b
>>> print("Sum of {} and {}={}".format(a,b,c))---Sum of 10 and 20=30
>>> print("sum({},{})={}".format(a,b,c))---sum(10,20)=30
=========================================================
Syntax-5: print(Messages Cum Values with format specifier )
(OR)
print(Values cum Messages with format specifier )
=========================================================
=>This Syntax displays Values Cum Messages OR Messages Cum Values with
format Specifiers
=>In Python Programming, we Use %d for displaying Integer data, %f for
displaying float data and %s for displaying str data.
=>In Any Value does not contain any format specifier and if we want to display
such type of Values then we must convert them into str by using str() and use
%s.
Examples
>>> sno=10
>>> sname="Ram"
>>> marks=44.56
>>> print("My Number is %d and name is %s and marks is %f" %
(sno,sname,marks))
My Number is 10 and name is Ram and marks is 44.560000
>>> print("My Number is %d and name is %s and marks is %0.2f" %
(sno,sname,marks))
My Number is 10 and name is Ram and marks is 44.56
>>> print("My Number is %d and name is %s and marks is %0.3f" %
(sno,sname,marks))
My Number is 10 and name is Ram and marks is 44.560
------------------------------------------------
>>> a=10
>>> b=20
>>> c=a+b
>>> print("sum of %d and %d=%d" %(a,b,c))---sum of 10 and 20=30
>>> print("sum of %f and %f=%f" %(a,b,c))-----sum of 10.000000 and
20.000000=30.000000
>>> print("sum of %0.2f and %0.2f=%0.3f" %(a,b,c))--sum of 10.00 and
20.00=30.000
-----------------------------
>>> lst=[100,"Ram",45.67,"Python",True]
>>> print("Content of list=",lst)----------Content of list= [100, 'Ram', 45.67,
'Python', True]
>>> print("Content of list={}".format(lst))-----Content of list=[100, 'Ram',
45.67, 'Python', True]
>>> print("Content of list=%d" %lst)---TypeError: %d format: a real
number is required, not list
>>> print("Content of list=%s" %str(lst))---Content of list=[100, 'Ram',
45.67, 'Python', True]
----------------------------------------
>>> d1={10:"Dinesh",20:"Ram",30:"Naresh"}
>>> print("Content of dict=%s" %str(d1))-----Content of dict={10: 'Dinesh', 20:
'Ram', 30: 'Naresh'}
=========================================================
Syntax-6: print(Value, end=" ")
=========================================================
=>This Syntax displays the values on the console in same Line.
Examples:
>>> r=range(10,101,20)
>>> for val in r:
... print(val)
10

30

50

70

90

>>> for val in r:


... print(val,end=" ")---- 10 30 50 70 90
--------------
>>> for val in r:
... print(val,end="->")-------- 10->30->50->70->90
=============================x===========================
Most Imp
=========================================================
>>> s="PYTHON"
>>> print(s)------------PYTHON
>>> s=s+s+s
>>> print(s)------------PYTHONPYTHONPYTHON
-----------------------
>>> s="PYTHON"
>>> print(s)-----------PYTHON
>>> s=s*3 # Here * is called Repetation Operator
>>> print(s)----------PYTHONPYTHONPYTHON
>>> print(2*3) # Here * is called Mul Operator
6
>>> print("KVR"*5)------------KVRKVRKVRKVRKVR
>>> print("22"*4)-------------22222222
>>> print("22"*"4")--------------TypeError: can't multiply sequence by non-
int of type 'str'
----------------------------------
>>> print("=")------ =
>>> print("="*25) #
==========================================
========================X===============================

==================================================
Operators and Expressions in Python
==================================================
=>An operator is a symbol, which is used to perform Certain Operation on
given data and gives Result.
=>If Two or More Variables (Objects) Connected with an Operator then it is
Called Expression,
=>In Otherwards, An Expression is a Collection of Variables (objects)
connected with an Operator.
=>In Python Programming, we have 7 Operators. They are
1. Arithmetic Operators
2. Assignment Operator
3. Relational Operators (Comparison Operators)
4. Logical Operators (Comparison Operators)
5. Bitwise Operators (Most Imp)
6. Membership Operators
a) in
b) not in
7. Identity Operators
a) is
b) is not
======================================================

NOTE1: Python Programming does not Support Unary Operators ( ++ , - - )


------------------------------------------------------------------------------------------------
NOTE2: Python Programming does not Support Ternary Operator of C, C+
+,C#.net, Java ( ? : )
------------------------------------------------------------------------------------------------
NOTE3: Python Programming Contains Short Hand Operators
------------------------------------------------------------------------------------------------
NOTE4: Python Programming Contains Its Own Ternary Operator-- if...else
operator
----------------------------------------------------------------------------------------------

====================================================
1. Arithmetic Operators
====================================================
=>The purpose of Arithmetic Operators is that "To perform Arithmetic
Operations such as Addition, Subtraction, Multiplication. Etc".
=>If two or more Variables (Objects) connected with Arithmetic Operators then
we call it as Arithmetic Expression.
=>In Python Programming, we have 7 Arithmetic Operators. They are given in
the following table.

SLNO SYMBOL MEANING EXAMPLE a=10 b=3

===============================================================================
= 1. + Addition print(a+b)---------->13
2. - Substraction print(a-b)----------->7

3. * Multiplication print(a*b)---------->30

4. / Division print(a/b)----------
>3.333333333333335 (Float Quotient)

5. // Floor Division print(a//b)--- ->3 (Integer


Quotient)

6. % Modulo Division print(a%b) --------->1

(remainder)

7. ** Exponentiation print(a**b) --------->1000

(power of)

==================================================
2. Assignment Operator
==================================================
=>The purpose of assignment operator is that " To assign or transfer Right Hand
Side (RHS) Value / Expression Value to the Left-Hand Side (LHS) Variable "
=>The Symbol for Assignment Operator is single equal to (=).
=>In Python Programming, we can use Assignment Operator in two ways.
1. Single Line Assignment
2. Multi Line Assignment

1. Single Line Assignment:


=>Syntax: LHS Varname= RHS Value
LHS Varname= RHS Expression

=>With Single Line Assignment at a time we can assign one RHS Value /
Expression to the single LHS Variable Name.
Examples:
>>> a=10
>>> b=20
>>> c=a+b
>>> print (a, b, c) ------------10 20 30
------------------------------------------------------------------------------------------------
2. Multi Line Assignment:
=>Syntax: Var1,Var2.....Var-n = Val1,Val2....Val-n
Var1,Var2.....Var-n= Expr1,Expr2...Expr-n
Here The values of Val1, Val2...Val-n are assigned to Var1,Var2...Var-n
Respectively.
Here The values of Expr1, Expr2...Expr-n are assigned to Var1,Var2...Var-n
Respectively.
Examples:
>>> a,b=10,20
>>> print(a,b)------------10 20
>>> c,d,e=a+b,a-b,a*b
>>> print(c,d,e)-------------30 -10 200
>>> sno,sname,marks=10,"Rossum",34.56
>>> print(sno,sname,marks)---------10 Rossum 34.56
===================================X===================
=====================================================
3. Relational Operators (Comparison Operators)
====================================================
=>The purpose of Relational Operators is that " To Compare Two Values OR
Object Values".
=>If two or More Variables OR Objects Connected with Relational Operators
then we it as Relational
Expression.
=>The Result of Relational Expression is Either True of False.
=>The Relational Expression is also Called Test Condition and it gives Either
True or False.
=>In Python Programming, we have 6 Relational Operators. They are given in
the following Table.
===============================================================================
=

SLNO SYMBOL MEANING EXAMPLES

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

1. > Greater Than print(10>5)---------True print(100>200)-----False

2. < LessThan print (10<20)-------True print(100<50)------False

3. == Equality print(10==20)------False (double equal to


print(100==100)---True

4. != Not Equal to print(10!=20)-------True print(100!=100)----False

5. >= Greater Than print(10>=20)------False

or equal to print(10>=5)------True

6. <= Less Than print(10<=20)-----True or Equal to print(100<=50)----


False

===================================================
4. Logical Operators (Comparison Operators)
===================================================
=>The purpose of Logical Operators is that "To Combine Two or More
Relational Expressions".
=>If Two or More Relational Expressions are connected with Logical Operators
then we call it as Logical Expressions OR Compound Condition.
=>The Result of Logical Expressions OR Compound Condition gives either
True of False.
=>In Python Programming, we have 3 Types of Logical Operators and They are
given in the following Table.
=========================================================
SLNO SYMBOL MEANING
=========================================================
1. and Physical ANDing

2. or Physical ORing

3. not ------------------------
=========================================================
1) and Operator
=>The Functionality of 'and' Operator is given in the following Truth Table
-------------------------------------------------------------------------------------
RelExpr1 RelExpr2 RelExpr1 and RelExpr2
-------------------------------------------------------------------------------------
True False False

False True False

False False False

True True True


-------------------------------------------------------------------------------------
Examples:
>>> True and False----------False
>>> False and False---------False
>>> False and True----------False
>>> True and True-----------True
-------------------------------
>>> 10>5 and 20>40-------------False
>>> 10<30 and 30>3------------True
>>> 10<5 and 20>15------------False---Short Circuit Evaluation
>>> 10>20 and 45>30 and 45>20-------False--Short Circuit Evaluation
>>> 10>5 and 20>30 and 40>5 and -10>-20-----False---Short Circuit
Evaluation
--------------------------------------------------------------------------------
Short Circuit Evaluation in the case of 'and' Operator
--------------------------------------------------------------------------------
=>If two or More relational expressions are connected with 'and' Operator
(Logical Expression) and if
First Relational Expr result is False then PVM will not evaluate rest of the
Relational Expressions and PVM Gives result of Entire Logical Expression as
False. This Process of Evaluation is called "Short Circuit Evaluation".
=========================================================
2) or Operator
=>The Functionality of 'or' Operator is given in the following Truth Table
----------------------------------------------------------------------------------
RelExpr1 RelExpr2 RelExpr1 or RelExpr2
-------------------------------------------------------------------------------------
True False True

False True True

False False False

True True True


-------------------------------------------------------------------------------------
Examples:
>>> True or False----------True
>>> False or True---------True
>>> False or False-------False
>>> True or True---------True
------------------------------------------------
>>> 10<5 or 30>20---------True
>>> 10>20 or 20>30 or 40>20-------True
>>> 10>20 or 20>30 or 40>40----False
>>> 10>5 or 20>40 or 40>30----True----Short Circuit Evaluation
>>> 100>200 or 500>2 or 500>1000 or -10>-5---True---Short Circuit
Evaluation
--------------------------------------------------------------------------------
Short Circuit Evaluation in the case of 'or' Operator
--------------------------------------------------------------------------------
=>If two or More relational expressions are connected with 'or' Operator
(Logical Expression) and if First Relational Expr result is True then PVM will
not evaluate rest of the Relational Expressions and PVM Gives result of Entire
Logical Expression as True. This Process of Evaluation is called "Short Circuit
Evaluation".
=========================================================
3) not Operator
=>The Functionality of 'not' Operator is given in the following Truth Table
-------------------------------------------------------------------------------------
RelExpr1 not RelExpr1
-------------------------------------------------------------------------------------
True False
False True
---------------------------------------------------------------------------------
Examples:
>>> a=True
>>> print(a,type(a))--------True <class 'bool'>
>>> b=not a
>>> print(b,type(b))-------False <class 'bool'>
>>> print(10>20)--------False
>>> print(not 10>20)----True
>>> print(not 100)-------False
>>> print(not -100)------False
>>> print(not 0)---------True
>>> 10>20 and 30>40------False
>>> not(10>20 and 30>40)-----True
>>> 10>2 or 20>40----------------True
>>> not(10>2 or 20>40)---------False
-------------------
>>> print(not "Python")--------False
>>> print(not "")------------------True
MOST IMP Questions
>>> 100 and 200---------------200
>>> -100 and -150------------- -150
>>> 100 and 200 and 300---- 300
>>> 100 and 0 and 400-------- 0
>>> 100 and True and False and 234---- False
>>> "PYTHON" and "False" and "JAVA"----'JAVA'
>>> #----------------------------------------------
>>> 10 or 20---------------10
>>> 10 or 20 or 0---------10
>>> 0 or 20 or 30--------20
>>> 0 or 20 or 0---------20
>>> "False" or "True" or "False"-------'False'
>>> False or "True" or "False"---------'True'
>>> False or True or "False"-----------True
>> 100 and 200 or 300---------200
>>> 100 or 200 and 300 and -100 or 201---- 100
>>> "Python" and "Java" and "Django" or "Python-Dsc"---'Django'
>>> True and False or True or False and True and 100----True
=========================================================
===================================================
5. Bitwise Operators (Most Imp)
===================================================
=>The Purpose of Bitwise Operators is that "To Perform operations on Binary
data in the form of Bit by Bit.
=>The Bitwise Operators are applicable on Integer data only but not on floating
point values bcoz Integer values contain Certainty whereas floating point
values does not contain certainty.
=>The Execution Process of Bitwise Operators is that " First Given Integer Data
Converted into Binary Data, and Operations performed on Binary data in the
form of Bit by Bit Depends on Bitwise Operator and Hence these operators are
named as Bitwise Operators ".
=>In Python Programming, we have 6 Types of Bitwise Operators. They are
1. Bitwise Left Shift Operator ( << )
2. Bitwise Right Shift Operator ( >> )
3. Bitwise OR Operator ( | )
4. Bitwise AND Operator ( & )
5. Bitwise Complement Operator ( ~ )
6. Bitwise XOR Operator ( ^ )
=========================================================
=======================================
1. Bitwise Left Shift Operator ( << )
=======================================
Syntax: varname=GivenNumber << No. of Bits
Concept: The Bitwise Left Shift Operator ( << ) shifts No. of Bits Towards
Left Side By adding No. of Zero at Right Side (depends on No. of Bits). So that
result value will display in the form of decimal number system.
Examples:
>>> a=10
>>> b=3
>>> c=a<<b
>>> print(c)-----------80
>>> print(10<<3)--------80
>>> print(3<<2)---------12
>>> print(100<<2)-----400
>>>print(20<<2)-----80
>>> print(25<<3)-----200
-----------------------------------------------------------------------------------
=======================================
2. Bitwise Right Shift Operator ( >> )
=======================================
Syntax: varname=GivenNumber >> No. of Bits
Concept: The Bitwise Right Shift Operator (>>) shifts No. of Bits Towards
Right Side By Adding No. of Zero at Left Side (depends on No. of Bits). So that
result value will displayed in the form of decimal number system.

Examples:
>>> a=10
>>> b=3
>>> c=a>>b
>>> print(c)----------------1
>>> print(10>>2)----------2
>>> print(25>>4)----------1
>>> print(24>>3)---------3
>>> print(50>>4)--------3
>>> print(45>>2)--------11
------------------------------------------------------------------------------------------------
======================================
3. Bitwise OR Operator (|)
======================================
=>The Functionality of Bitwise OR Operator ( | ) is shown in the following
Truth table.
=>------------------------------------------------------------
var1 var2 var1 | var2
------------------------------------------------------------
0 1 1
1 0 1
0 0 0
1 1 1
------------------------------------------------------------
Examples
>>> 0|0-----------0
>>> 0|1-----------1
>>> 1|0-----------1
>>> 1|1-----------1
-------------------------------
>>> a=10
>>> b=12
>>> c=a|b
>>> print(c)--------14
>>> print(5|4)----5
Most Imp Points
>>>s1={10,20,30}
>>>s2={10,15,25}
>>> s3=s1|s2 # Bitwise OR Operator for UNION Operation
>>> print(s3,type(s3))--------{20, 25, 10, 30, 15} <class 'set'>
--------------------------
>>> s1={"Python","Data Sci"}
>>> s2={"C","C++","DSA"}
>>> print(s1,type(s1))------------{'Python', 'Data Sci'} <class 'set'>
>>> print(s2,type(s2))-----------{'C', 'DSA', 'C++'} <class 'set'>
>>> s3=s1|s2
>>> print(s3,type(s3))---------{'C', 'DSA', 'C++', 'Python', 'Data Sci'} <class
'set'>
=====================================x===================
======================================
4. Bitwise AND Operator (&)
======================================
=>The Functionality of Bitwise AND Operator (|) is shown in the following
Truth table.

=>------------------------------------------------------------
var1 var2 var1 & var2
------------------------------------------------------------
0 1 0
1 0 0
0 0 0
1 1 1
---------------------------------------------------------------

Examples
>>> 0&1---------------0
>>> 1&0---------------0
>>> 0&0---------------0
>>> 1&1---------------1
-----------------------------------
>>> a=8
>>> b=6
>>> c=a&b
>>> print(c)-------0
------------------
>>> a=10
>>> b=12
>>> c=a&b
>>> print(c)--------8
-----------------------------
Most Imp Points
>>>s1={10,20,30}
>>>s2={10,15,25}
>>> s3=s1&s2 # Bitwise AND Operator for INTERSECTION Operation
>>> print(s3,type(s3))-------{10} <class 'set'>
--------------------------------------------
>>> s1={"Python","Data Sci"}
>>> s2={"C","C++","DSA","Python"}
>>> s3=s1&s2
>>> print(s3,type(s3))----------{'Python'} <class 'set'>
-----------------------
>>> s1={"Python","Data Sci"}
>>> s2={"C","C++","DSA"}
>>> s3=s1&s2
>>> print(s3,type(s3))-----------set() <class 'set'>
======================================x==================
================================================
5.Bitwise Complement Operator (~)
================================================
=Syntax: varname = ~Value
=>The Bitwise Complement Operator (~) will Invert the Binary Bits.
=>Inverting the Bits are nothing but 1 becomes 0 and 0 becomes 1
=>Formula: - Formula for ~Value = - (Value+1)
---------------------------------------------- --------------------------------------------------
Examples:
>>>a=10
>>>b=~a
>>>print("Val of b=",b)--------------- -11
>>> a=35
>>> b=~a
>>> print("Val of b=",b)----------------Val of b= -36
>>> a=-101
>>> b=~a
>>> print("Val of b=",b)----------------Val of b= 100
------------------------------------------------------------------------------------------------
======================================
6. Bitwise XOR Operator (^ )
======================================
=>The Functionality of Bitwise XOR Operator ( ^ ) is shown in the following
Truth table.

=>------------------------------------------------------------
var1 var2 var1 ^ var2
------------------------------------------------------------
0 1 1
1 0 1
0 0 0
1 1 0
------------------------------------------------------------
Examples
>>> 1^0-------------1
>>> 0^1-------------1
>>> 0^0-------------0
>>> 1^1-------------0
------------------------------
Examples:
-----------------------------
>>> a=10
>>> b=5
>>> c=a^b
>>> print(c)--------------15
----------------
>>> a=10
>>> b=4
>>> c=a^b
>>> print(c)------------14
>>> a=9
>>> b=8
>>> c=a^b
>>> print(c)--------------1
>>> print(4^4)-----------0
=====================================x===================
Most Imp Points
>>> a={10,20,30}
>>> b={-10,15,25}
>>> c=a.symmetric_difference(b)
>>> print(c,type(c))-------------{15, 20, 25, 30} <class 'set'>
>>> d=a^b
>>> print(d,type(d))--------{15, 20, 25, 30} <class 'set'>
====================================x===================

6. Membership Operators
---------------------------------------------------------------------------
=>The purpose of membership operators is that "To Check whether the Specific
Value present in Iterable object Or Not".
=>An Iterable object is one, which contains More Number of Values
(Examples: Sequence Type,List Type,Set type,Duct type)
=>In Python Programming, we have 2 types of Membership Operators. They
are
 in
 not in
------------------------------------------------------------------------------------------------
1. in
------------------------------------------------------------------------------------------------
Syntax: Value in IterableObject
=>"in" operator returns True Provided Value present in Iterable object
=>"in" operator returns False Provided Value not present in Iterable object
------------------------------------------------------------------------------------------------
2. not in
------------------------------------------------------------------------------------------------
Syntax: Value not in IterableObject
=>"not in" operator returns True Provided Value not present in Iterable object
=>"not in" operator returns False Provided Value present in Iterable object
------------------------------------------------------------------------------------------------
Examples:
>>> s="PYTHON"
>>> print(s,type(s))------------PYTHON <class 'str'>
>>> "P" in s---------------------True
>>> "O" in s--------------------True
>>> "O" not in s--------------False
>>> "K" in s-------------------False
>>> "K" not in s--------------True
>>> "p" in s--------------------False
>>> "p" not in s--------------True
---------------------------------------------------------------
>>> s="PYTHON"
>>> print(s,type(s))--------------PYTHON <class 'str'>
>>> "PYT" in s-------------------True
>>> "PYTH" not in s-----------False
>>> "PYTH" in s-----------------True
>>> "THON" not in s------------False
>>> "THON" in s----------------True
>>> s="PYTHON"
>>> print(s,type(s))-------------PYTHON <class 'str'>
>>> "PON" in s------------------False
>>> "HON" in s------------------True
>>> "PON" not in s-------------True
>>> "NOH" in s-----------------False
>>> "NOH" not in s------------True
>>> "PTO" in s-----------------False
>>> s="PYTHON"
>>> print(s,type(s))---------------PYTHON <class 'str'>
>>> "NOH" in s[::-1]---------------True
>>> "PTO" not in s[::2]----------False
>>> "PTO" in s[::2]---------------True
>>> "OTP" not in s[1::-2]----------True
>>> s[1::-2]-----------------------------'Y'
--------------------------------------------------------------
>>> lst=[10,"Rossum",True,2+3j]
>>> print(lst,type(lst))----------------[10, 'Rossum', True, (2+3j)] <class 'list'>
>>> 10 in lst-----------------------------True
>>> True not in lst----------------------False
>>> 2+3j in lst---------------------------True
>>> "Ros" in lst------------------------False
>>> "Ros" in lst[1]-------------------True
>>> "mus" not in lst[1][::-1]---------False
>>> lst[::-1] not in lst[::]---------------True
>>> lst[1][::-1] in lst[::]----------------False
>>> lst[1][::-1] in lst[-3][::-1][::-1]-----False
-------------------------------------------------------------------
>>> lst=[10,"Rossum",True,2+3j]
>>> print(lst,type(lst))-------------------------[10, 'Rossum', True, (2+3j)] <class
'list'>
>>> 2+3j in lst----------------------------------True
>>> 2+3j not in lst----------------------------False
-------------------------
>>> 2 in lst[-1]---------------------TypeError: argument of type 'complex' is not
iterable
>>> 3 in lst[-1].imag------------TypeError: argument of type 'float' is not iterable
------------------------------------------------------------------------
>>> d1={10:"Python",20:"Java",30:"C++"}
>>> print(d1,type(d1))---------------{10: 'Python', 20: 'Java', 30: 'C++'} <class
'dict'>
>>> 10 in d1----------------True
>>> 30 not in d1-----------False
>>> 40 in d1----------------False
>>> "Python" in d1---------False
>>> "Python" in d1[10]----True
>>> "C++" in d1.get(30)---------True
>>> "C++"[::-1] not in d1.get(30)[::-1]----False
>>> "C++"[::-1] in d1.get(30)[::-1]---------True
==============================x==========================
=====================================================

Identity Operators--Python Command / IDLE Shell Basis


=====================================================
=>The purpose of Identity Operators is that "To Compare Memory Address of
Two Objects".
=>In Python Programming, we have Two Types of Identity Operators. They are
 is
 is not
------------------------------------------------------------------------------------------------
1. is
------------------------------------------------------------------------------------------------
=>Syntax: Object1 is Object2
=>"is" Operator returns True Provided Both the objects Contains SAME
Address.
=>"is" Operator returns False Provided Both the objects Contains Different
Address.
------------------------------------------------------------------------------------------------
2. is not
------------------------------------------------------------------------------------------------
Syntax: Object1 is not Object2
=>"is not " Operator returns True Provided Both the objects Contains Different
Address.
=>"is not " Operator returns False Provided Both the objects Contains Same
Address.
------------------------------------------------------------------------------------------------
Examples:
>>> a=None
>>> b=None
>>> print(a,type(a),id(a))---------None <class 'NoneType'> 140704386324472
>>> print(b,type(b),id(b))--------None <class 'NoneType'> 140704386324472
>>> a is b---------True
>>> a is not b---------False
------------------------------------------------------------------------------------------------
>>> d1={10:"Apple",20:"Mango",30:"Kiwi"}
>>> d2={10:"Apple",20:"Mango",30:"Kiwi"}
>>> print(d1,type(d1),id(d1))-----{10: 'Apple', 20: 'Mango', 30: 'Kiwi'} <class
'dict'> 2864804287360
>>> print(d2,type(d2),id(d2))-----{10: 'Apple', 20: 'Mango', 30: 'Kiwi'} <class
'dict'> 2864804287488
>>> d1 is d2--------False
>>> d1 is not d2--True
------------------------------------------------------------------------------------------------
>>> s1={10,20,30,40}
>>> s2={10,20,30,40}
>>> print(s1,type(s1),id(s1))---------{40, 10, 20, 30} <class 'set'>
2864805025088
>>> print(s2,type(s2),id(s2))---------{40, 10, 20, 30} <class 'set'>
2864805024640
>>> s1 is s2-------------------False
>>> s1 is not s2-------------True
>>> fs1=frozenset(s1)
>>> fs2=frozenset(s1)
>>> print(fs1,type(fs1),id(fs1))--------frozenset({40, 10, 20, 30}) <class
'frozenset'> 2864805026208
>>> print(fs2,type(fs2),id(fs2))-------frozenset({40, 10, 20, 30}) <class
'frozenset'> 2864805026656
>>> fs1 is fs2-------------------------False
>>> fs1 is not fs2-------------------True
------------------------------------------------------------------------------------------------
>>> l1=[10,"Rossum",22.22]
>>> l2=[10,"Rossum",22.22]
>>> print(l1,type(l1),id(l1))----------[10, 'Rossum', 22.22] <class 'list'>
2864804317696
>>> print(l2,type(l2),id(l2))---------[10, 'Rossum', 22.22] <class 'list'>
2864804318400
>>> l1 is l2------------------False
>>> l1 is not l2-----------True
>>> t1=(10,20,30,40)
>>> t2=(10,20,30,40)
>>> print(t1,type(t1),id(t1))------------(10, 20, 30, 40) <class 'tuple'>
2864804748304
>>> print(t2,type(t2),id(t2))------------(10, 20, 30, 40) <class 'tuple'>
2864804752304
>>> t1 is t2--------------------------------False
>>> t1 is not t2--------------------------True
------------------------------------------------------------------------------------------------
>>> r1=range(10,20)
>>> r2=range(10,20)
>>> print(r1,type(r1),id(r1))--------range(10, 20) <class 'range'>
2864804444656
>>> print(r2,type(r2),id(r2))--------range(10, 20) <class 'range'>
2864804444224
>>> r1 is r2----------------------False
>>> r1 is not r2---------------True
>>> ba1=bytearray([10,20,30,40])
>>> ba2=bytearray([10,20,30,40])
>>> print(ba1,type(ba1),id(ba1))---bytearray(b'\n\x14\x1e(') <class 'bytearray'>
2864808582512
>>> print(ba2,type(ba2),id(ba2))---bytearray(b'\n\x14\x1e(') <class 'bytearray'>
2864808582896
>>> ba1 is ba2--------False
>>> ba1 is not ba2---True
>>> ba1=bytes([10,20,30,40])
>>> ba2=bytes([10,20,30,40])
>>> print(ba1,type(ba1),id(ba1))----b'\n\x14\x1e(' <class 'bytes'>
2864804444320
>>> print(ba2,type(ba2),id(ba2))---b'\n\x14\x1e(' <class 'bytes'>
2864804442592
>>> ba1 is ba2-----False
>>> ba1 is not ba2----True
---------------------
>>> s1="PYTHON"
>>> s2="PYTHON"
>>> print(s1,type(s1),id(s1))---PYTHON <class 'str'> 2864808583024
>>> print(s2,type(s2),id(s2))---PYTHON <class 'str'> 2864808583024
>>> s1 is s2-----True
>>> s1 is not s2---False
----------------------------
>>> s1="JAVA"
>>> s2="JAVa"
>>> print(s1,type(s1),id(s1))--------JAVA <class 'str'> 2864808582000
>>> print(s2,type(s2),id(s2))-------JAVa <class 'str'> 2864808582512
>>> s1 is s2------------False
>>> s1 is not s2------True
------------------------------------------------------------------------------------------------
>>> a=2+3j
>>> b=2+3j
>>> print(a,type(a),id(a))--------(2+3j) <class 'complex'> 2864803995536
>>> print(b,type(b),id(b))-------(2+3j) <class 'complex'> 2864803999280
>>> a is b-----------False
>>> a is not b-----True
>>> a=True
>>> b=True
>>> print(a,type(a),id(a))---------True <class 'bool'> 140704386272104
>>> print(b,type(b),id(b))--------True <class 'bool'> 140704386272104
>>> a is b--------True
>>> a is not b---False
>>> c=False
>>> print(c,type(c),id(c))-----False <class 'bool'> 140704386272136
>>> a is c-------False
>>> a is not c-------True
>>> a=1.2
>>> b=1.2
>>> print(a,type(a),id(a))---------1.2 <class 'float'> 2864803995984
>>> print(b,type(b),id(b))--------1.2 <class 'float'> 2864803999280
>>> a is b--------------------False
>>> a is not b--------------True
----------------------------
>>> a=300
>>> b=300
>>> print(a,type(a),id(a))-----300 <class 'int'> 2864803995600
>>> print(b,type(b),id(b))----300 <class 'int'> 2864803999184
>>> a is b-----------False
>>> a is not b------True
>>> a=10
>>> b=10
>>> print(a,type(a),id(a))--------10 <class 'int'> 2864802955792
>>> print(b,type(b),id(b))-------10 <class 'int'> 2864802955792
>>> a is b---------True
>>> a is not b------False
>>> a=256
>>> b=256
>>> print(a,type(a),id(a))----256 <class 'int'> 2864802963664
>>> print(b,type(b),id(b))----256 <class 'int'> 2864802963664
>>> a is b--------True
>>> a is not b---False
>>> a=257
>>> b=257
>>> print(a,type(a),id(a))----257 <class 'int'> 2864803995600
>>> print(b,type(b),id(b))---257 <class 'int'> 2864803999248
>>> a is b-------False
>>> a is not b----True
>>> a=0
>>> b=0
>>> print(a,type(a),id(a))------0 <class 'int'> 2864802955472
>>> print(b,type(b),id(b))-----0 <class 'int'> 2864802955472
>>> a is b------------True
>>> a is not b-------False
---------------------------------------
>>> a=-1
>>> b=-1
>>> print(a,type(a),id(a))---------- -1 <class 'int'> 2864802955440
>>> print(b,type(b),id(b))--------- -1 <class 'int'> 2864802955440
>>> a is b--------True
>>> a is not b---False
>>> a=-5
>>> b=-5
>>> print(a,type(a),id(a))------------ -5 <class 'int'> 2864802955312
>>> print(b,type(b),id(b))------------ -5 <class 'int'> 2864802955312
>>> a is b--------------True
>>> a is not b---------False
>>> a=-6
>>> b=-6
>>> print(a,type(a),id(a))-------- -6 <class 'int'> 2864803999248
>>> print(b,type(b),id(b))------ -6 <class 'int'> 2864803995600
>>> a is b-----------------False
>>> a is not b----------True
---------------------------------------------------------------------------------------------
MOST IMP
>>> a,b=400,400 # Multi Line Assigment
>>> print(a,type(a),id(a))-----400 <class 'int'> 2864803995728
>>> print(b,type(b),id(b))----400 <class 'int'> 2864803995728
>>> a is b-----True
>>> a is not b---False
>>> a,b=1.2,1.2 # # Multi Line Assigment
>>> print(a,type(a),id(a))---------1.2 <class 'float'> 2864803999120
>>> print(b,type(b),id(b))---------1.2 <class 'float'> 2864803999120
>>> a is b---------True
>>> a is not b----False
>>> a,b=2+3j,2+3j
>>> print(a,type(a),id(a))-------(2+3j) <class 'complex'> 2864803999344
>>> print(b,type(b),id(b))------(2+3j) <class 'complex'> 2864803999344
>>> a is b--------------------True
>>> a is not b--------------False
---------------------------------------------------------------------------------------------
MOST MOST IMP
>>> l1,l2=[10,20,30],[10,20,30]
>>> print(l1,type(l1),id(l1))---------[10, 20, 30] <class 'list'> 2864804318400
>>> print(l2,type(l2),id(l2))--------[10, 20, 30] <class 'list'> 2864808577600
>>> l1 is l2---------------False
>>> l1 is not l2--------True
>>> d1,d2={10:"Python",20:"Java"},{10:"Python",20:"Java"}
>>> print(d1,type(d1),id(d1))---{10: 'Python', 20: 'Java'} <class 'dict'>
2864804318976
>>> print(d2,type(d2),id(d2))---{10: 'Python', 20: 'Java'} <class 'dict'>
2864804287872
>>> d1 is d2----------False
>>> d1 is not d2-----True
------------------------------------------------------------------------------------------------
NOTE:
=>What are all the objects Participates in DEEP Copy then Those Objects
Contains Same Address and "is" operator Returns True and "is not" operator
Returns False.
=>What are all the objects Participates in SHALLOW DEEP Copy then Those
Objects Contains Different Address and "is" operator Returns False and "is not"
operator Returns True.
------------------------------------------------------------------------------------------------

You might also like