Data Types in Python
Data Types in Python
=================================================
=>The purpose of DataTypes in Python is that " To allocate Sufficient Amount of Memory
Space in main
=>Python Programming Provides 14 data types and Classified into 6 Categories. They are
1. int (immutable)
2. float (immutable)
3. bool (immutable)
4. complex (immutable)
1. str (immutable)
2. bytes (immutable)
3. bytearray(mutable)
4. range (immutable)
1. list (mutable)
2. tuple (immutable)
2. frozenset (immutable)
1. dict (mutable)
VI. NoneType Category Data Type
1. NoneType
===========================================
1. int
===========================================
Properties:
---------------------
=>'int' is one of the pre-defined class name and treated as
Fundamental data Type.
=>The purpose of int data type is that " To store Integer data OR
Whole Numbers OR Integral data
(Numbers without Decimal places)".
----------------
Examples:
------------------------------- -------------
Python Instructions Output
-------------------------------- -------------
>>> a=123
>>> print(a)----------------------------------123
>>> type(a)----------------------------------<class 'int'>
>>> id(a)-------------------------------------140716113134184
---------------------------------------
>>> sno=4567
>>> print(sno,type(sno))----------------4567 <class 'int'>
>>> print(id(sno))-------------------------1785231794992
-------------------------------------------------------------------------------
>>> print(sno,type(sno),id(sno))----4567 <class 'int'>
1785231794992
NOTE: One Can Use Class Names as Variable Names
Examples:
-----------------
>>> int=34 # here int is an object / variables
>>> print(int,type(int))---------------------34 <class 'int'>
---------------------------------------------------------------------------------------------
---------------------------
=>With int data type, In Python Programming, along with Integer
data, we can also store Different
Number System Values. In Any Programming Lang, we have 4
Types of Number Systems. They are
1) bin()
2) oct()
3) hex()
---------------------------------------------------------------------------------------------
--------------
======================================================
====
2. float
======================================================
====
Properties
--------------------------
=>'float' is one of the pre-defined class name and treated as
Fundamental data Type.
=>The purpose of float data type is that "To store Real Constant
values OR Float OR Double Pointing
(Number with Decimal Places)".
=>float data type never allows us to represent / Store Binary,Octal
and Hexa Decimal Values
=>float data type allows us to store Scientific Notation data. The
Advantage of Scientific is that To take Less Memory space for bigger
floating point Values.
--------------------
Examples:
--------------------
>>> a=12.34
>>> print(a,type(a))-----------------------------------12.34 <class 'float'>
>>> b=0.9
>>> print(b,type(b))----------------------------------0.9 <class 'float'>
------------------------------
>>> a=10
>>> b=1.2
>>> c=a+b
>>> print(a,type(a))---------------------------------10 <class 'int'>
>>> print(b,type(b))--------------------------------1.2 <class 'float'>
>>> print(c,type(c))--------------------------------11.2 <class 'float'>
---------------------------------------------------------------------------------------------
-----------
>>> a=0b1010.0b1111------------SyntaxError: invalid decimal literal
>>> a=0o13.0o23----------------SyntaxError: invalid decimal literal
>>> a=0xAC.0XBEE------------SyntaxError: invalid decimal literal
>>> a=0b1010.0xAC------------SyntaxError: invalid decimal literal
>>> a=0o12.0b1010------------SyntaxError: invalid decimal literal
---------------------------------------------------------------------------------------------
-----------
>>> a=3e2
>>> print(a,type(a))--------------------300.0 <class 'float'>
----------------------------------
>>> a=4e-2
>>> print(a,type(a))---------------0.04 <class 'float'>
-----------------------------------------------------------
>>> a=0.00000000000000000000000000000000000000000000006
>>> print(a,type(a))---------6e-47 <class 'float'>
========================================
3. bool
========================================
=>'bool' is one of the pre-defined class name and treated as
Fundamental data Type.
=>The purpose of bool data type is that "To Store True False
Values".
=>In Python Programming True, False are Called Keywords and It is
treated as values of bool data type.
=>In Python Programming, Internally, The value of True is treated as
1 and False is treated as 0.
Examples:
---------------------------------------
>>> a=True
>>> print(a,type(a))--------------------------------True <class 'bool'>
>>> b=False
>>> print(b,type(b))------------------------------False <class 'bool'>
------------------------------
>>> a=true--------------NameError: name 'true' is not defined
>>> a=false----------------------NameError: name 'false' is not defined
---------------------------------------------------------------------------------------------
-------------
>>> a=True
>>> b=False
>>> print(a+b)------------------------1
>>> print(a-b)------------------------1
>>> print(2*True+True+False)------------3
>>> print(2+True*3+False)-------------5
>>> print(0b1010-True+2*True)----------11
>>> print(True/True)----------------------1.0
>>> print(True//True)------------------------1
>>> print(False//True)----------------------0
#Most Imp
>>> print(True//False)---------------------------ZeroDivisionError: integer
division or modulo by zero
======================================x==================
=======================
==============================================
4. complex
================================================
Properties
-----------------
=>'complex' is one of the pre-defined class and treated as
Fundamental data type.
=>The purpose of complex data type is that "To store Complex
Values / Imaginary values".
=>The Gegeral Notation of compelx data type is given bellow
x+yj or x-yj
Here 'x' is called Real Part
Here 'y' is caqlled Imaginary part
and j Represents sqrt(-1) or sqr(j)=-1
=>Internally, Real and Imgainary parts of Complex Object are treated
as float data type
=>To Extract the real and Imginary parts of Complex object, we have
2 pre-defined attributes. They are
1) real
2) imag
=================================================
1. str ( Part-1 )
=================================================
iNDEX
-----------
=>Purpose of str
=>Definition of str
=>Types of strs
a) Indexing
b) Slicing
=>Programmin Examples
-------------------------------------------------------------------------------------------------------------------------------
---------
Properties
----------------------------
=>'str' is one of the pre-defined class 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 tripple double
quotes and tripple single quotes. "
------------------------
=>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 tripple double quotes and tripple single
quotes. "
----------------------------
-----------------------------
=>In Python Programming, we have two types of Str Data. They are
-----------------------------------------
-----------------------------------------
(OR)
---------------------------------------------------------------------------------------------------------------------
-----------------------------------------
String Data2
------------------
(OR)
String Data2
------------------
=>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"
>>> s2='Python'
>>> print(s2,type(s2))-------------Python <class 'str'>
>>> s3='A'
>>> s4="A"
------------------------------
>>> s5="Python3.10"
>>> s6="123456"
---------------------------
------------------------------
-----------------------------
-------------------------------------
>>> print(addr1,type(addr1))
-------------------
>>> print(addr2,type(addr2))
Travis Oliphant
Numpy Organization
------------------------------------------
------------------------
-------------------------------
>>> print(s1,type(s1))
>>> s1
' Python is an oop lang \n python is also fun Prog lang \n Python is also
Modular Prog lang '
=================================x======================================\
====================================================
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]
>>> lst=[10,-34,56,100,255,0,102]
>>> lst=[10,34,56,100,255,0,102]
>>> b=bytes(lst)
... print(val)
...
10
34
56
100
255
102
>>> print(b[0])--------------------10
>>> print(b[-1])-------------------102
... print(val)
...
10
34
56
100
>>> b[0]----------------------------10
==============================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 oraganizing (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 Assigment.
=>On the object of bytearray, we can perform both Indexing and Slicing Operations.
=>An object of bytearray maintains Insertion Order (Which is nothing but, whatever the order
we insert the data, In the same order data will be displayed).
-------------------------------------------------------------------------------------------------------------------------------
-------------
NOTE: The Functionality of bytes and bytearray are exactly same but bytes object belongs to
Immutable bcoz bytes object does not support Item assigment where bytearray object blongs
to Mutable bcoz bytearray object supports Item Assigment.
-------------------------------------------------------------------------------------------------------------------------------
-------------
Examples:
-------------------------------------------------------------------------------------------------------------------------------
-------------
>>> tpl=(10,34,56,100,256,0,102)
>>> tpl=(-10,34,56,100,255,0,102)
-------------------------
>>> tpl=(10,34,56,100,255,0,102)
-------------------------------
>>> tpl=(10,34,56,100,255,0,102)
>>> ba=bytearray(tpl)
... print(val)
...
10
34
56
100
255
102
... print(val)
...
102
255
100
56
34
10
... print(val)
...
10
34
56
100
255
102
>>> ba[0]--------------10
>>> ba[-1]------------102
... print(val)
...
123
34
56
100
255
102
>>> print(ba,type(ba),id(ba))-----bytearray(b'{"8d\xff\x00f') <class 'bytearray'>
2149788291504
------------------------------------------
... print(val)
...
123
34
56
100
255
102
>>> b[0]---------------------123
>>> for v in x:
... print(v)
...
123
34
56
100
255
102
>>> x[1]----------------------34
>>> x[1]=234
>>> for v in x:
... print(v)
...
123
234
56
100
255
102
==============================================
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
-------------------------------------------------------------------------------------------------------------------------------
-------------
Syntax-1: varname=range(Value)
-------------------------------------------------------------------------------------------------------------------------------
-------------
Examples:
-----------------
>>> r=range(6)
>>> print(r,type(r))
>>> for v in r:
... print(v)
...
... print(val)
...
... print(val)
...
-------------------------------------------------------------------------------------------------------------------------------
-------------
Syntax-2 : varname=range(Begin,End)
-------------------------------------------------------------------------------------------------------------------------------
-------------
Examples:
------------------
>>> r=range(10,16)
>>> print(r,type(r))
... print(val)
...
10
11
12
13
14
15
... print(val)
...
10
11
12
13
14
15
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 Backdirection.
-----------------------------------
Examples:
------------------------------------
>>> r=range(10,21,2)
... print(val)
...
10
12
14
16
18
20
... print(val)
...
10
12
14
16
18
20
-------------------------------------------------------------------------------------------------------------------------------
-----
-------------------------------------------------------------------------------------------------------------------------------
-----
Q1) 0 1 2 3 4 5 6 7 8 9 10--------range(11)
... print(val)
...
10
-------------------------------------------------------------------------------------------------------------------------------
-----
Q2) 10 11 12 13 14 15 16 17 18 19 20--range(10,21)
... print(val)
...
10
11
12
13
14
15
16
17
18
19
20
-------------------------------------------------------------------------------------------------------------------------------
-----
... print(val)
...
1000
1001
1002
1003
1004
1005
-------------------------------------------------------------------------------------------------------------------------------
-----
Q4) 10 12 14 16 18 20-------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)
... 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)
... print(v)
...
10
-------------------------------------------------------------------------------------------------------------------------------
-----
... print(hyd)
...
100
90
80
70
60
50
40
30
20
10
-------------------------------------------------------------------------------------------------------------------------------
-----
... print(val)
...
-10
-11
-12
-13
-14
-15
-------------------------------------------------------------------------------------------------------------------------------
-----
Q9) -100 -90 -80 -70 -60 -50 -40 -30 -20 -10 ----range(-100,-9,10)
... print(val)
...
-100
-90
-80
-70
-60
-50
-40
-30
-20
-10
-------------------------------------------------------------------------------------------------------------------------------
-----
... print(val)
...
-5
-4
-3
-2
-1
3
4
... print(val)
...
-5
-4
-3
-2
-1
-------------------------------------------------------------------------------------------------------------------------------
-----
... print(val)
...
-1000
-1050
-1100
-1150
-1200
>>> r=range(-1000,-1201,-50)
>>> r[0]-------------------------1000
>>> r[-1]-------------------------1200
... print(val)
...
-1000
-1050
-----------------
... print(val)
...
20
18
16
14
12
10
==============================================================
==============================================
==============================================
=>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]
=>If we enter Valid Index value then we get Corresponding Inxdexed Value.
---------------------------
Examples
---------------------------
>>> s="PYTHON"
>>> 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'
---------------------
>>> s="123453456"
>>> s[2]----------------------'3'
>>> s[-1]-------------------'6'
>>> s[0]--------------------'1'
>>> s[len(s)-1]-------------'6'
>>> s[-len(s)]---------------'1'
--------------------------------------------
>>> s="JAVA"
>>> 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="MISSISSIPPI"
>>> s[len(s)-1]----------------'I'
>>> s[-len(s)]-----------------'M'
-------------------------------------------------------------------------------------------------------------------------------
---------
[Link]
-------------------------------------------------------------------------------------------------------------------------------
---------
=>The Process obtaining Range of Values OR Sub String from Given main Str Object is called
Slicing.
-------------------------------------------------------------------------------------------------------------------------------
---------
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"
>>> s[0:3]--------------------------------'PYT'
>>> s[2:6]--------------------------------'THON'
>>> s[1:6]---------------------------------'YTHON'
>>> s[0:6]----------------------------------'PYTHON'
----------------------------
>>> s="PYTHON"
>>> s[-6:-2]---------------------------'PYTH'
>>> s[-3:-1]---------------------------'HO'
>>> s[-6:-3]---------------------------'PYT'
>>> s[-6:-1]---------------------------'PYTHO'
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
-----------------
Examples
-----------------
>>> s="PYTHON"
>>> s[1:-2]-------------'YTH'
>>> s[2:-1]--------------'THO'
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"
>>> s[2:]----------------------------'THON'
>>> s[0:]----------------------------'PYTHON'
>>> s[3:]----------------------------'HON'
>>> s[4:]----------------------------'ON'
>>> s[1:]---------------------------'YTHON'
-------------------------------
>>> s="PYTHON"
>>> s[-3:]-----------------------------'HON'
>>> s[-2:]-----------------------------'ON'
>>> s[-6:]-----------------------------'PYTHON'
>>> s[-5:]----------------------------'YTHON'
>>> s[-4:]----------------------------'THON'
>>> s[-1:]----------------------------'N'
-----------------------------------------------------
Special Points
-----------------------------------------------------
>>> s="PYTHON"
>>> s[-12:]-----------------------------------'PYTHON'
>>> s[-23:]----------------------------------'PYTHON'
>>> s[2:-1]----------------------------------'THO'
-------------------------------------------------------------------------------------------------------------------------------
----------
-------------------------------------------------------------------------------------------------------------------------------
----------
=>In This Syntax, we are Specifying END Index and we did'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"
>>> s[:3]----------------------------'PYT'
>>> s[:6]----------------------------'PYTHON'
>>> s[:5]---------------------------'PYTHO'
>>> s[:4]---------------------------'PYTH'
------------------------------
>>> s="PYTHON"
>>> 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
(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).
---------------------------------------------
Examples
---------------------------------------------
>>> s="PYTHON"
>>> s[:]-----------------------------------'PYTHON'
NOTE: All the above Syntaxes are Extracting the data from strobj in Forward Direction with
Default
Step value 1.
-------------------------------------------------------------------------------------------------------------------------------
------------
-------------------------------------------------------------------------------------------------------------------------------
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
-------------
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[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[-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[:0:1]-----------------------------''
-------------------------------------------------------------------------------------------------------
Examples-------------RULE-5
-------------------------------------------------------------------------------------------------------
>>> s="PYTHON"
>>> print(s)--------------------PYTHON
>>> s[::-1]---------------------'NOHTYP'
-------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------
1. list
==============================================
Index:
------------------------------------------------------------------------
=>Properties of list
=>Types of list
a) emty list
b) non-empty list
=>Operations on 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
separted by comma.
=>On object of list, we can perform Both Indexing and Slicing Operations.
=>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]
>>> l2[0]----------------20
>>> l2[-1]-------------(2+3j)
---------------------------
>>> l2=[20,"Rossum",34.56,True,2+3j]
>>> l2[0]=30
>>> l2[2:4]=[44.55,False]
---------------------------------------
>>> l1=[10,"Rossum",34.56]
>>> l2=[]
>>> l3=list()
-------------------------------------
>>> l1=[10,20,30,40,50,60]
>>> b=bytes(l1)
>>> l2=list(b)
>>> s="MISSISSIPPI"
>>> l3=list(s)
>>> print(l3,type(l3))-----['M', 'I', 'S', 'S', 'I', 'S', 'S', 'I', 'P', 'P', 'I'] <class 'list'>
------------------------
>>> a=[10]
>>> x=100
>>> b=list([x])
-------------------------------------------------------------------------------------------------------------------------------
---
==================================================
==================================================
=>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
-------------------------------------------------------------------------------------------------------------------------------
---------------
1. append()
-------------------------------------------------------------------------------------------------------------------------------
---------------
Syntax: [Link](Value)
=>This Function is used for adding the values to list object at end.
--------------------------
Example
-------------------------
>>> lst=[10,"Rossum",34.56]
>>> [Link]("PYTHON")
>>> [Link](True)
>>> [Link](1+2.5j)
>>> print(lst,id(lst))----[10, 'Rossum', 34.56, 'PYTHON', True, (1+2.5j)] 2705927639808
-------------------------------------------------------------------------------------------------------------------------------
---------------
2. insert()
-------------------------------------------------------------------------------------------------------------------------------
---------------
Syntax: [Link](Index,Value)
=>This Function is used for adding the value to list object at Specified Index
=>When we enter Invalid Possitive 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]
>>> [Link](2,"PYTHON")
>>> lst[-1]=44.56
>>> [Link](2,True)
>>> [Link](-1,2+3j)
-------------------------------------
>>> lst=[10,"Rossum",34.56]
>>> [Link](10,"PYTHON")
>>> print(lst,id(lst))-----------[10, 'Rossum', 34.56, 'PYTHON'] 2705927639296
>>> [Link](-10,"HYD")
-------------------------------------------------------------------------------------------------------------------------------
---------------
3. clear()
-------------------------------------------------------------------------------------------------------------------------------
---------------
Syntax: [Link]()
=>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]
>>> [Link]()
----------------------------------
(OR)
>>> print([Link]())----------None
(OR)
>>> print([].clear())
None
>>> print(list().clear())----------None
-------------------------------------------------------------------------------------------------------------------------
4. remove()----Based on Value
-------------------------------------------------------------------------------------------------------------------------------
---------------
=>Syntax: [Link](Value)
=>This Function is used for Removing the First Occurence 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"]
>>> [Link]("Rossum")
>>> [Link](34.56)
>>> [Link]("Python")
>>> print(lst,len(lst))------------------[10] 1
>>> [Link](10)
>>> print(lst,len(lst))------------------[] 0
---------------------------------------
>>> lst1=[10,20,30,10,30,"Python",True]
>>> [Link](10)
>>> [Link](30)
>>> [Link](30)
-------------------------------------------------------------------------------------------------------------------------
5. pop(index)----Index Based
-------------------------------------------------------------------------------------------------------------------------------
---------------
=>Syntax: [Link](index)
=>This Function is used for Removing the Element of listobj based on Index.
-------------------
Examples:
-------------------
>>> lst1=[10,20,30,10,30,"Python",True]
>>> [Link](3)-------------------------10
>>> [Link](-4)-----------------------30
>>> [Link](0)------------------------10
>>> [Link](0)------------------------20
>>> [Link](0)-----------------------'Python'
>>> print(lst1)------------------------[True]
>>> [Link](0)-----------------------True
>>> print(lst1)-------------------------[]
-------------------------------------------------------------------------------------------------------------------------------
------------
6. pop()
-------------------------------------------------------------------------------------------------------------------------------
---------------
Syntax: [Link]()
------------------
Examples:
------------------
>>> lst=[10,"Rossum",34.56,"Python"]
>>> [Link]()--------------------------'Python'
>>> [Link]()-------------------------34.56
>>> [Link]()--------------------------'Rossum'
>>> print(lst)-------------------------[10]
>>> [Link]()--------------------------10
>>> print(lst)-------------------------[]
---------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------
---------
------------------
Examples
------------------
>>> lst=[10,"Sagatika",66.66,"OUCET","HYD"]
>>> lst=[10,"Sagatika",66.66,"OUCET","HYD"]
-------------------------------------------------------------------------------------------------------------------------------
------------
7) index()
-------------------------------------------------------------------------------------------------------------------------------
------------
Syntax: [Link](Value)
------------
=>This Function is used for Finding Index of First Occurence 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]
>>> [Link](10)---------0
>>> [Link](20)---------1
>>> [Link](30)----------2
-------------------------------------------------------------------------------------------------------------------------------
------------
8) copy()----Shallow Copy
-------------------------------------------------------------------------------------------------------------------------------
------------
=>This Function is Used for Copying the content of One Object into another Object (
Implements
Shallow Copy).
=>Syntax: Listobject2=[Link]()
----------------
Examples:
----------------
>>> lst1=[10,"Rossum",34.56]
>>> [Link]("PYTHON")
>>> [Link]("NL")
-------------------------------
Deep Copy--Examples
---------------------------------
>>> lst1=[10,"Rossum",34.56]
>>> [Link]("HYD")
>>> [Link]("Rossum")
-------------------------------------------------------------------------------------------------------------------------------
------------
9. count()
-------------------------------------------------------------------------------------------------------------------------------
------------
=>Syntax: [Link](Value)
=>This Function is used for finding / Counting Number of occurences 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]
>>> [Link](10)-------3
>>> [Link](20)-------2
>>> [Link](30)-------1
>>> [Link](40)-------0
>>> list().count(10)----------0
>>> [].count(10)-------------0
-------------------------------------------------------------------------------------------------------------------------------
------------
10. reverse()
-------------------------------------------------------------------------------------------------------------------------------
------------
Syntax: [Link]()
=>This Function is used for reversing(Front elements to back and back elements to Front) the
elements
----------------------
Examples:
---------------------
>>> lst1=[10,"Rossum",34.56,"PYTHON"]
>>> [Link]()
-----------------------
>>> lst2=[10,20,30,100,200,300]
>>> [Link]()
---------------------------------------
>>> lst2=[10,20,30,100,200,300]
>>> lst3=[Link]()
>>> print(lst3)--------------None
(OR)
>>> print(list().reverse())--------None
-------------------------------------------------------------------------------------------------------------------------------
---------------
11) extend()
-------------------------------------------------------------------------------------------------------------------------------
---------------
Syntax: [Link](listobj2)
=>This Function is used for Merging / Combining The values of listobj2 with ListObj1. Hence
Listobj1
=>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"]
>>> [Link](lst2)
>>> [Link](lst3)
------------------------------
OR
------------------------------
>>> lst1=[10,20,30,40]
>>> lst2=["Python","Java"]
>>> lst3=["Rossum","Gosling"]
-------------------------------------------------------------------------------------------------------------------------------
---------------
12) sort()
-------------------------------------------------------------------------------------------------------------------------------
---------------
-----------------
Examples:
-----------------
>>> lst=[10,-2,12,56,13,-7,45,6]
>>> [Link]()
>>> #------------------------------------
>>> lst=[10,-2,12,56,13,-7,45,6]
>>> [Link]()
>>> [Link]()
>>> #-----------------------------------------
>>> lst=[10,-2,12,56,13,-7,45,6]
>>> [Link](reverse=True)
>>> #------------------------------------------
>>> lst=[10,-2,12,56,13,-7,45,6]
>>> [Link](reverse=False)
>>> #-----------------------------------------------
>>> lst=["Trump","Zaki","Biden","Putin","Rossum","Alen"]
>>> [Link](reverse=True)
>>> #-------------------------------------------------
>>> lst=["Trump","Zaki","Biden","Putin","Rossum","Alen"]
>>> [Link]()
>>> #---------------------------------------------------
>>> lst=[10,"Trump",33.33,2+3j,True]
>>> [Link]()----------TypeError: '<' not supported between instances of 'str' and 'int'
==================================x==========================================
=
==================================================
==================================================
=>The Process of Defining One tuple in another tuple is called Inner or Nested 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
---------------------------------------------------------------------------
>>> 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'>
>>> sf[2].append(12)
-------------------------------------------------------
>>> 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'>
-----------------------------------------------------------------------------------------------------------
NOTE:
--------
---------------------------------------------------------------------------------------------------------
>>> print(t1,type(t1))
(10, 'Rossum', [16, 18, 17], ('CSE', 'AI', 'DS'), 'OUCET') <class 'tuple'>
>>> t1[2].append("KVR")
>>> print(t1,type(t1))--(10, 'Rossum', [16, 18, 17, 'KVR'], ('CSE', 'AI', 'DS'), 'OUCET') <class
'tuple'>
>>> t1[2][1]--------18
>>> t1[2][-2]------17
>>> t1[2][-3]------18
>>> k=sorted(t1[-2])
>>> 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')
-------------------------------
>>> k=sorted(t1)
----------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
---------------
===============================================
=>The Process of Defining one list inside of another list is called Inner or Nested List
=>In inner list we can perform Both Indexing and Slicing Operations
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[1]-----------------------------------'Karthik'
>>> lst[0]-----------------------------------100
>>> 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']
>>> lst[2].clear()
>>> [Link](2,[16,15,18,17])
>>> [Link](-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'>
>>> studlist[-3].append(17)
>>> print(studlist,type(studlist))--[100, 'DLPrince', [18, 16, 19, 17], [78, 66, 79],
'OUCET'] <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()
>>> studlist[2].append(14)
>>> [Link](2,[17,15,14,19])
>>> print(studlist,type(studlist))--[100, 'DLPrince', [17, 15, 14, 19], 'OUCET'] <class
'list'>
>>> [Link]([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==================================
=============================================
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
separted by comma.
=>On object of tuple, we can perform Both Indexing and Slicing Operations.
=>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:
--------------------------
=>An 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
-------------------------------------------------------------------------------------------------------------------------------
---------------
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")
>>> len(t1)-----------7
>>> len(t2)----------4
>>> t3=100,"Rossum",44.44,"PYTHON"
--------------------------------------------
>>> t1=()
>>> len(t1)-------------------------0
>>> t2=tuple()
>>> len(t2)------------------------0
--------------------------------------------
>>> t=(100,"Rossum",44.44,"PYTHON")
>>> t[0]--------------100
>>> t[1]--------------'Rossum'
>>> t[-1]------------'PYTHON'
----------------------------------------------
>>> l1=[100,"Rossum",44.44,"PYTHON"]
>>> t1=tuple(l1)
------------------------------------------------------------
>>> t=(100,"Rossum",44.44,"PYTHON")
-------------------------------------------------------------------------------------------------------------------------
Special Points
---------------------------
>>> a=10
>>> t=tuple([a])
--------OR-------------
>>> b=100
>>> t=(b,)
-----------------------------------
>>> b=12.34
>>> t=(b)
>>> t=(b,)
=====================================================
=====================================================
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.
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
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:
-----------------
>>> 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={}
>>> len(d3)-------------0
>>> d4=dict()
>>> len(d4)---------------0
------------------------------------------------------------------------
>>> d2={10:3.4,20:4.5,30:5.6,40:3.4}
>>> d2[0]----------------------------------------KeyError: 0
>>> d2[10]-------------------------------------3.4
>>> d2[10]=10.44
-------------------------------------------------------------------------------------
>>> 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={}
>>> 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()
>>> 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'}
--------------------------------------------------------------------------------------
>>> 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
=========================================================
=========================================================
=>In dict object, we have the following function for performing Various Operations.
-------------------------------------------------------------------------------------------------------------------------------
---
1. clear()
-------------------------------------------------------------------------------------------------------------------------------
---
Syntax: [Link]()
=>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
>>> [Link]()
>>> print([Link]())----------None
>>> print(dict().clear())---------None
>>> print({}.clear())--------------None
-------------------------------------------------------------------------------------------------------------------------------
---
2. pop()
-------------------------------------------------------------------------------------------------------------------------------
---
=>Syntax: [Link](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
>>> [Link](20)--------------------'Java'
>>> [Link](30)------------------'C++'
>>> [Link](40)0---------------'C'
>>> [Link](50)------------------------KeyError: 50
>>> [Link](10)-------------'Python'
>>> [Link](10)-----------KeyError: 10
>>> dict().pop(10)----------------KeyError: 10
-------------------------------------------------------------------------------------------------------------------------------
---
3. popitem()
-------------------------------------------------------------------------------------------------------------------------------
---
Syntax: [Link]()
------------
=>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
-------------------------------------------------------------------------------------------------------------------------------
---
4. copy()
-------------------------------------------------------------------------------------------------------------------------------
---
Syntax: dictobj2=[Link]()
------------
=>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
>>> 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
----------------
---------------------
>>> d1={10:"Python",20:"Java",30:"C++",40:"C"}
>>> print(d1,id(d1))---------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'} 2364669155776
>>> 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=[Link](10)
>>> print(v)------------Python
>>> v=[Link](20)
>>> print(v)-----------Java
#OR
>>> [Link](10)--------'Python'
>>> [Link](20)--------'Java'
>>> print([Link](200))-------None
>>> v=[Link](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
>>> [Link](10)--------------'Python'
>>> d1[10]--------------------'Python'
>>> d1[20]---------------------'Java'
>>> d1[40]--------------------'C'
>>> d1[400]-------------------KeyError: 400
>>> print([Link](400))--------None
-------------------------------------------------------------------------------------------------------------------------------
---
6. keys()
-------------------------------------------------------------------------------------------------------------------------------
---
Syntax: varname=[Link]()
------------ (OR)
[Link]()
=>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=[Link]()
... print(k)
...
10
20
30
40
...
10
20
30
40
... print(k,"-->",[Link](k))
...
10 --> Python
20 --> Java
30 --> C++
40 --> C
... print(k,"-->",d1[k])
...
10 --> Python
20 --> Java
30 --> C++
40 --> C
-------------------------------------------------------------------------------------------------------------------------------
---
7. values()
-------------------------------------------------------------------------------------------------------------------------------
---
Syntax: varname=[Link]()
------------ (OR)
[Link]()
=>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=[Link]()
... print(v)
...
Python
Java
C++
... print(v)
...
Python
Java
C++
---------------------------------
MOST IMP
---------------------------------
... print(x)
...
10
20
30
40
... print(x,"-->",d1[x])
...
10 --> Python
20 --> Java
30 --> C++
40 --> C
-------------------------------------------------------------------------------------------------------------------------------
---
8. items()
-------------------------------------------------------------------------------------------------------------------------------
---
Syntax: varname=[Link]()
------------
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=[Link]()
... print(kv)
...
(10, 'Python')
(20, 'Java')
(30, 'C++')
(40, 'C')
... print(kv)
...
(10, 'Python')
(20, 'Java')
(30, 'C++')
(40, 'C')
... print(k,"--->",v)
...
10 ---> Python
20 ---> Java
30 ---> C++
40 ---> C
-------------------------------------------------------------------------------------------------------------------------------
---
9. update()
-------------------------------------------------------------------------------------------------------------------------------
---
Syntax: [Link](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"}
>>> [Link](d2)
>>> print(d1,type(d1))---------{10: 1.2, 20: 3.4, 30: 4.5, 100: 'Apple', 200: 'Kiwi'} <class 'dict'>
---------------------------------------------------
>>> d1={10:1.2,20:3.4,30:4.5}
>>> d2={100:"Apple",20:13.4}
>>> [Link](d2)
>>> print(d1,type(d1))--------{10: 1.2, 20: 13.4, 30: 4.5, 100: 'Apple'} <class 'dict'>
>>> d1={10:1.2,20:3.4,30:4.5}
>>> d2={10:11.2,20:13.4,30:14.5}
>>> [Link](d2)
==========================x===================================================
=
-----------------------------------------------
----------------------------------------------
>>>
d1={"sno":10,"sname":"Rossum","imarks":{"cm":17,"cppm":16,"pym":18},"emarks":{"cm":6
7,"cppm":78,"pym":70},"cname":"OUCET"}
>>> print(d1,type(d1))
... print(k)
...
sno
sname
imarks
emarks
cname
... print(v)
...
10
Rossum
OUCET
... print(v,type(v))
...
10 <class 'int'>
... print(kv)
...
('sno', 10)
('sname', 'Rossum')
('cname', 'OUCET')
...
sno
sname
imarks
emarks
cname
... print(k,"--->",[Link](k))
...
sno ---> 10
... print(v)
...
cm
cppm
pym
... print(v,"-->",[Link]("imarks").get(v))
...
cm --> 17
cppm --> 16
pym --> 18
... print(v,"-->",d1["imarks"][v])
...
cm --> 17
cppm --> 16
pym --> 18
=========================================================================
----------------
Examples:
---------------
>>>
d1={"sno":10,"sname":"Rossum","imarks":[16,18,17],"emarks":(60,77,67),"crs":{"[Link](cse)
", "[Link](AI)"}, "cname":"OUECT"}
... print(k,"-->",v,"--->",type(v))
...
=========================================================================
----------------------------------
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'>
... print(val,type(val))
...
10 <class 'int'>
... print(k,"--->",v)
...
cm ---> 16
cppm ---> 17
pym ---> 18
------------------------------------------------------------------
------------------------------------------------------------------
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'>
...
10 <class 'int'>
... print(k,"-->",v)
...
cm --> 16
cppm --> 17
pym --> 18
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
----------
-------------------------------------------------------------------------------------------------------------------------------
---
=======================================
=======================================
=>'NoneType' is one the pre-defined class and treated as None type Data type
--------------------------------------------------------------------
Examples:
------------------
>>> a=None
>>> l1=[]
>>> print([Link]())------------None
>>> s1=set()
>>> print([Link]())---------None
>>> d1=dict()
>>> print([Link]())-----------None
>>> d1={10:1.2,20:3.4}
>>> print([Link](100))---------None
-------------------------------------------------------------------------------------------------------------------