SlideShare a Scribd company logo
5
Most read
6
Most read
7
Most read
Python
Data Structures
DR. VAZI OKHANDIAR
WWW.NRCLC.COM
NR Computer Learning Center (NRCLC)
• Established in 2002
• Provide Computer Training
• Microsoft Partner
Hands-on classroom training
Online Training
Virtual Live Training
Private Lesson
Dr. Vazi Okhandiar
 Over 30 years of teaching experience in Computer
Science & IT courses and Microsoft Products.
 Worked for the World Bank, General Motors, HP/EDS,
Toyota, CSC, Olympus, and Mitsubishi, as well as
numerous small and medium-sized enterprises.
 Education:
 DBA, Walden University
 MBA, University of California, Irvine
 Masters in Computer Science, Illinois Institute of
Technology, Chicago
 Bachelor’s Degree in Electrical Engineering,
University of California, Irvine.
 Microsoft Certified Trainer (MCT) by Microsoft and
Certified Project Management Professional (PMP) by PMI
Data Structures in Python
A data structure is a way of organizing and storing data in memory
so that it can be used efficiently.
Four types of data structure in Python:
 List
 Tuple
 Set
 Dictionary
List
List
• General Purpose
• Most widely used data structure
• Change size as needed
• Uses square bracket “[…]”
• Allow duplicate values
Tuple
• Immutable (Can’t add or change)
• Useful for fixed data
• Faster the lists
• Sequence type
• Uses round bracket “(…)”
• Allow duplicate values
Set
• Store non-duplicate items
• immutable
• Very fast access vs Lists
• Math Set Ops
• Change size as needed
• Unordered, unchangeable
• Uses curly bracket “{…}”
• duplicate values ignored
Dictionary
• Store non-duplicate items
• Key/value pairs
• Change size as needed
• Unordered
• Duplicate values ignored
• Uses curly bracket “{…}”
Lists
 A list can be created for storing any type of data that can be stored as a variable.
 A set is written with square brackets “[ .. ]”.
 FORMAT :
variable_name = [ value1, …, value n]
Example:
myList1 = [] # Create an empty list
myList2 = [1, 2, 3]
myList3 =[1, “Hello”, 3]
myList4 = [0] *3 #[0, 0, 0]
index
 The first element of a List is index as 0 instead of 1.
 The highest element number is one less than total number of elements.
Original: Value = [0, 0, 0, 0, 0]
 Value[0] = 10
 Value[1] = 1
 Value[2] = 30
UpdatedValue = [10, 1, 30, 0, 0]
index 0 1 2 3 4
value 10 1 30 0 0
index 0 1 2 3 4
value 0 0 0 0 0
Example (Slicing)
myList = [10, “Apple”, 12, 13, 14]
print (myList[1:2]) => [‘Apple’]
print (myList[:2] => [10, ‘Apple’]
print (myList[2:]) => [12, 13, 14]
print (myList[-3:-1]) => [13, 14]
if 12 in myList:
print(myList)
else:
print("Not found") => [10, ‘Apple’, 12, 13, 14]
index 0 1 2 3 4
value 10 Apple 12 13 14
Example (Updating List)
myList = [10, “Apple”, 12, 13, 14]
myList[1:2] = [20, 30]
print (myList) => Output: [10, 20, 30, 12, 13, 14]
myList.insert (3, 40)
print(myList) => Output: [10, 20, 30, 40, 12, 13, 14]
myList.append(15)
print(myList) => Output: [10, 20, 30, 40, 12, 13, 14, 15]
del myList[2]
print (myList) => Output: [10, 20, 40, 12, 13, 14, 15]
del myList[1:4]
print (myList) => Output: [10, 13, 14, 15]
myList.clear()
print(myList) => Output: []
index 0 1 2 3 4
value 10 Apple 12 13 14
Example (Merging lists)
myList1 = [10, 20, 30]
myList2 = [40, 50, 60, 70, 80]
newList1 = myList1 + myList2
or
for index in myList2:
myList1.append(index)
or
myList1.extend(myList2)
print (myList1)
Output: [10, 20, 30, 40, 50, 60, 70, 80]
Tuple
List
• General Purpose
• Most widely used data structure
• Change size as needed
• Sequence type
• Sortable
• Uses square bracket “[…]”
• Allow duplicate values
Tuple
• Immutable (Can’t add or change)
• Useful for fixed data
• Faster than lists
• Uses round bracket “(…)”
• Allow duplicate values
Set
• Store non-duplicate items
• immutable
• Very fast access vs Lists
• Math Set Ops
• Change size as needed
• Unordered, unchangeable
• Uses curly bracket “{…}”
• duplicate values ignored
Dictionary
• Store non-duplicate items
• Key/value pairs
• Change size as needed
• Unordered
• Duplicate values ignored
• Uses curly bracket “{…}”
Examples (Tuple)
myTuple = ("Apple", 10, "Orange", "Grapes", 20.0, 10)
print(myTuple[3]) => Grapes
print(myTuple[-1]) => 10
print(myTuple[2:4]) => (‘Orange’, ‘Grapes’)
print(myTuple[:4]) => (‘Apple’, 10, ‘Orange’, ‘Grapes’)
print(myTuple[2:]) => (‘Orange’, ‘Grapes’, 20.0, 10)
print(myTuple[-4:-1]) => (‘Orange’, ‘Grapes’, 20.0)
Sets
List
• General Purpose
• Most widely used data structure
• Change size as needed
• Sequence type
• Sortable
• Uses square bracket “[…]”
• Allow duplicate values
Tuple
• Immutable (Can’t add or change)
• Useful for fixed data
• Faster the lists
• Sequence type
• Uses round bracket “(…)”
• Allow duplicate values
Set
• immutable
• Math Set Ops
• Change size as needed
• Uses curly bracket “{…}”
• duplicate values ignored
Dictionary
• Store non-duplicate items
• Key/value pairs
• Change size as needed
• Unordered
• Duplicate values ignored
• Uses curly bracket “{…}”
Example (Set)
mySet1 = {"Apple", 10, "Orange", "Grapes", 20.0}
mySet2 = {30.0, 10, "Orange", "Apple", 40}
 Print (mySet1 | mySet2) or print(mySet1.union(mySet2))
{"Apple", 40, 10, "Grapes", 20.0, “Orange”, 30.0}
 Print (mySet1 & mySet2) or print(mySet1.intersection(mySet2))
 {10, ‘Apple’, ‘Orange’}
 Print (mySet1 – mySet2) or print(mySet1.difference(mySet2))
 {‘Grapes’, 20.0}
 Print (mySet1 ^ mySet2) or print(mySet1.symmetric_difference(mySet2))
 {‘Grapes’, 20.0, 30.0, 40}
A
A
A A Union B
A interest B
A difference B
A
Dictionary
List
• General Purpose
• Most widely used data structure
• Change size as needed
• Sequence type
• Sortable
• Uses square bracket “[…]”
• Allow duplicate values
Tuple
• Immutable (Can’t add or change)
• Useful for fixed data
• Faster the lists
• Sequence type
• Uses round bracket “(…)”
• Allow duplicate values
Set
• immutable
• Very fast access vs Lists
• Math Set Ops
• Change size as needed
• Uses curly bracket “{…}”
• duplicate values ignored
Dictionary
• Store non-duplicate items
• Key: value pairs
• Mutable
• Unordered
• Uses curly bracket “{…}”
Example (Dictionary)
myFriends = {"Sandy":25, "John": 20, "Jane": 22}
print(Friends.items())  output: ('Sandy’,25), ('John', 20), ('Jane', 22)
print(Friends.keys())  output: ‘Sandy’, ‘John’, ‘Jane’
print(Friends.values())  output: 25, 20. 22
print(myFriends["Sandy“])  output: 25
myFriends["Sandy"] = 30
print (myFriends.items())  output: ('Sandy',30), ('John', 20), ('Jane', 22)
myFriends.update({"Sandy": 40})
print (myFriends.items())  output: ('Sandy’,40), ('John', 20), ('Jane', 22)
myFriends.pop("Sandy")
print (myFriends.items())  output: ('John', 20), ('Jane', 22)
myFriends.popitem()
print (myFriends.items())  output: ('Sandy', 25), ('John', 20)
del myFriends["Sandy"]
print (myFriends.items())  output: ('John', 20), ('Jane', 22)
del myFriends
print (myFriends.items())  output: Error
Sample Code
myD = {"URL": "Uniform Resource Locator", "CPU": "Central Processing Unit",
"RAM": "Random Access Memory"}
print("=>" + str(myD.keys()))
print("=>"+ str(myD.values()))
print("=>" + str(myD.items()))
print(myD["URL"])
del myD["CPU"]
print("Size: " + str(len(myD)))
myKeys = list(myD.keys())
myValues = list(myD.values())
for J in range(len(myD)):
print(str(J +1) + ". "+ myKeys[ J ] + ": " + (myValues[ J ]))
OUTPUT:
 dict_keys(['URL', 'CPU', 'RAM'])
 dict_values(['Uniform Resource Locator',
'Central Processing Unit', 'Random
Access Memory'])
 dict_items([('URL', 'Uniform Resource
Locator'), ('CPU', 'Central Processing
Unit'), ('RAM', 'Random Access
Memory')])
 Uniform Resource Locator
 Size: 2
 1. URL: Uniform Resource Locator
 2. RAM: Random Access Memory
Data Structure
List
• mutable
• Uses square bracket “[value, …]”
• General Purpose
• Most widely used data structure
• Allow duplicate values
Tuple
• Immutable
• Uses round bracket “(value, …)”
• Useful for fixed data
• Allow duplicate values
Sets
• Immutable
• Uses curly bracket “{value, …}”
• Math Set Ops
• duplicate values ignored
Dictionary
• Mutable
• Uses curly bracket “{key: value, …}”
• Key/value pairs
• Non-duplicate keys
• Unordered
Free Online Python IDE
https://replit.com
Free Exercise
https://www.w3resource.com/python-exercises/
Instructor-Led and Self-Paced Online Courses
https://ed2go.com/nrclc
--------- * ----------
Vazi Okhandiar
NR computer Learning Center
www.nrclc.com

More Related Content

PPTX
Basic data structures in python
PDF
Introduction to Python
PPTX
Python Collections
PPTX
List in Python
PPTX
Unit 4 python -list methods
PDF
Python Sequence Data types in Brief
PDF
Python tuples and Dictionary
PDF
Python-03| Data types
Basic data structures in python
Introduction to Python
Python Collections
List in Python
Unit 4 python -list methods
Python Sequence Data types in Brief
Python tuples and Dictionary
Python-03| Data types

What's hot (20)

PDF
Python libraries
PPT
Python GUI Programming
PDF
Python Collections Tutorial | Edureka
PDF
Python Variable Types, List, Tuple, Dictionary
PPTX
Functions in python
PPTX
Tuple in python
PPSX
python Function
PPTX
Python Functions
PDF
Arrays in python
PPT
FUNCTIONS IN c++ PPT
PPTX
Fundamentals of c programming
PPTX
Python decorators
PPTX
Java Stack Data Structure.pptx
PPTX
6-Python-Recursion PPT.pptx
PPTX
Python dictionary
PDF
Arrays In Python | Python Array Operations | Edureka
PPTX
Python Data Structures and Algorithms.pptx
PDF
9 python data structure-2
PPTX
For Loops and Nesting in Python
PDF
Python Modules
Python libraries
Python GUI Programming
Python Collections Tutorial | Edureka
Python Variable Types, List, Tuple, Dictionary
Functions in python
Tuple in python
python Function
Python Functions
Arrays in python
FUNCTIONS IN c++ PPT
Fundamentals of c programming
Python decorators
Java Stack Data Structure.pptx
6-Python-Recursion PPT.pptx
Python dictionary
Arrays In Python | Python Array Operations | Edureka
Python Data Structures and Algorithms.pptx
9 python data structure-2
For Loops and Nesting in Python
Python Modules
Ad

Similar to Python - Data Structures (20)

PPTX
Chapter 3-Data structure in python programming.pptx
PPTX
UNIT-3 python and data structure alo.pptx
PPTX
Python data structures
PPTX
Learn python - for beginners - part-2
PPTX
fundamental of python --- vivek singh shekawat
PPTX
ITS-16163: Module 5 Using Lists and Dictionaries
PPTX
Python introduction data structures lists etc
PPTX
Datastrucure
PPTX
Datastructures in python
PPTX
datastrubsbwbwbbwcturesinpython-3-4.pptx
PPTX
Understanding-Python-Data-Structures-A-Comprehensive-Guide.pptx
PPTX
Introduction To Programming with Python-4
PPTX
List_tuple_dictionary.pptx
PPTX
pythonlist_arrays_and some practice problems
PPTX
Python list tuple dictionary presentation
PPTX
Python list tuple dictionary .pptx
PPTX
Tuples-and-Dictionaries.pptx
PPTX
Programming with Python_Unit-3-Notes.pptx
DOC
Revision Tour 1 and 2 complete.doc
PDF
Data structures
Chapter 3-Data structure in python programming.pptx
UNIT-3 python and data structure alo.pptx
Python data structures
Learn python - for beginners - part-2
fundamental of python --- vivek singh shekawat
ITS-16163: Module 5 Using Lists and Dictionaries
Python introduction data structures lists etc
Datastrucure
Datastructures in python
datastrubsbwbwbbwcturesinpython-3-4.pptx
Understanding-Python-Data-Structures-A-Comprehensive-Guide.pptx
Introduction To Programming with Python-4
List_tuple_dictionary.pptx
pythonlist_arrays_and some practice problems
Python list tuple dictionary presentation
Python list tuple dictionary .pptx
Tuples-and-Dictionaries.pptx
Programming with Python_Unit-3-Notes.pptx
Revision Tour 1 and 2 complete.doc
Data structures
Ad

More from NR Computer Learning Center (20)

PPTX
Power BI Desktop Overview
PPTX
Building Dashboard with Excel
PPTX
Introduction to Data Analytics
PPTX
Introduction to SQL
PDF
Office 2019 tips & tricks
PDF
App Development with Apple Swift Certification at Certiport Centers
PDF
Project management fundamentals
PDF
National College Testing Association (NCTA)
PDF
National College Testing Association (NCTA)
PDF
Building a Dashboard in an hour with Power Pivot and Power BI
PPTX
Introduction to the basic mathematical concept with Python Turtle.
PDF
Stem presentation - Pathways to Technology Oriented Careers
PDF
MTA 98 364 - database fundamentals
PDF
MTA 361 software development fundamentals
PDF
Introduction to java
PDF
Introduction to c++
PDF
Executive dashboard for small business
PDF
Building a Dashboard in an Hour using Microsoft PowerPivot & Power BI
PDF
Arduino for teens
PDF
Microsoft Office Specialist (MOS) Excel 2013 certification pathway
Power BI Desktop Overview
Building Dashboard with Excel
Introduction to Data Analytics
Introduction to SQL
Office 2019 tips & tricks
App Development with Apple Swift Certification at Certiport Centers
Project management fundamentals
National College Testing Association (NCTA)
National College Testing Association (NCTA)
Building a Dashboard in an hour with Power Pivot and Power BI
Introduction to the basic mathematical concept with Python Turtle.
Stem presentation - Pathways to Technology Oriented Careers
MTA 98 364 - database fundamentals
MTA 361 software development fundamentals
Introduction to java
Introduction to c++
Executive dashboard for small business
Building a Dashboard in an Hour using Microsoft PowerPivot & Power BI
Arduino for teens
Microsoft Office Specialist (MOS) Excel 2013 certification pathway

Recently uploaded (20)

PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
PPTX
Business Acumen Training GuidePresentation.pptx
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
PPT
Quality review (1)_presentation of this 21
PDF
Foundation of Data Science unit number two notes
PPT
Reliability_Chapter_ presentation 1221.5784
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PDF
Clinical guidelines as a resource for EBP(1).pdf
PDF
Business Analytics and business intelligence.pdf
PPTX
Supervised vs unsupervised machine learning algorithms
PDF
Fluorescence-microscope_Botany_detailed content
PPTX
Computer network topology notes for revision
PDF
Mega Projects Data Mega Projects Data
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Galatica Smart Energy Infrastructure Startup Pitch Deck
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
Business Acumen Training GuidePresentation.pptx
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
Quality review (1)_presentation of this 21
Foundation of Data Science unit number two notes
Reliability_Chapter_ presentation 1221.5784
Introduction-to-Cloud-ComputingFinal.pptx
Clinical guidelines as a resource for EBP(1).pdf
Business Analytics and business intelligence.pdf
Supervised vs unsupervised machine learning algorithms
Fluorescence-microscope_Botany_detailed content
Computer network topology notes for revision
Mega Projects Data Mega Projects Data
Business Ppt On Nestle.pptx huunnnhhgfvu
Data_Analytics_and_PowerBI_Presentation.pptx
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf

Python - Data Structures

  • 1. Python Data Structures DR. VAZI OKHANDIAR WWW.NRCLC.COM
  • 2. NR Computer Learning Center (NRCLC) • Established in 2002 • Provide Computer Training • Microsoft Partner Hands-on classroom training Online Training Virtual Live Training Private Lesson
  • 3. Dr. Vazi Okhandiar  Over 30 years of teaching experience in Computer Science & IT courses and Microsoft Products.  Worked for the World Bank, General Motors, HP/EDS, Toyota, CSC, Olympus, and Mitsubishi, as well as numerous small and medium-sized enterprises.  Education:  DBA, Walden University  MBA, University of California, Irvine  Masters in Computer Science, Illinois Institute of Technology, Chicago  Bachelor’s Degree in Electrical Engineering, University of California, Irvine.  Microsoft Certified Trainer (MCT) by Microsoft and Certified Project Management Professional (PMP) by PMI
  • 4. Data Structures in Python A data structure is a way of organizing and storing data in memory so that it can be used efficiently. Four types of data structure in Python:  List  Tuple  Set  Dictionary
  • 5. List List • General Purpose • Most widely used data structure • Change size as needed • Uses square bracket “[…]” • Allow duplicate values Tuple • Immutable (Can’t add or change) • Useful for fixed data • Faster the lists • Sequence type • Uses round bracket “(…)” • Allow duplicate values Set • Store non-duplicate items • immutable • Very fast access vs Lists • Math Set Ops • Change size as needed • Unordered, unchangeable • Uses curly bracket “{…}” • duplicate values ignored Dictionary • Store non-duplicate items • Key/value pairs • Change size as needed • Unordered • Duplicate values ignored • Uses curly bracket “{…}”
  • 6. Lists  A list can be created for storing any type of data that can be stored as a variable.  A set is written with square brackets “[ .. ]”.  FORMAT : variable_name = [ value1, …, value n] Example: myList1 = [] # Create an empty list myList2 = [1, 2, 3] myList3 =[1, “Hello”, 3] myList4 = [0] *3 #[0, 0, 0]
  • 7. index  The first element of a List is index as 0 instead of 1.  The highest element number is one less than total number of elements. Original: Value = [0, 0, 0, 0, 0]  Value[0] = 10  Value[1] = 1  Value[2] = 30 UpdatedValue = [10, 1, 30, 0, 0] index 0 1 2 3 4 value 10 1 30 0 0 index 0 1 2 3 4 value 0 0 0 0 0
  • 8. Example (Slicing) myList = [10, “Apple”, 12, 13, 14] print (myList[1:2]) => [‘Apple’] print (myList[:2] => [10, ‘Apple’] print (myList[2:]) => [12, 13, 14] print (myList[-3:-1]) => [13, 14] if 12 in myList: print(myList) else: print("Not found") => [10, ‘Apple’, 12, 13, 14] index 0 1 2 3 4 value 10 Apple 12 13 14
  • 9. Example (Updating List) myList = [10, “Apple”, 12, 13, 14] myList[1:2] = [20, 30] print (myList) => Output: [10, 20, 30, 12, 13, 14] myList.insert (3, 40) print(myList) => Output: [10, 20, 30, 40, 12, 13, 14] myList.append(15) print(myList) => Output: [10, 20, 30, 40, 12, 13, 14, 15] del myList[2] print (myList) => Output: [10, 20, 40, 12, 13, 14, 15] del myList[1:4] print (myList) => Output: [10, 13, 14, 15] myList.clear() print(myList) => Output: [] index 0 1 2 3 4 value 10 Apple 12 13 14
  • 10. Example (Merging lists) myList1 = [10, 20, 30] myList2 = [40, 50, 60, 70, 80] newList1 = myList1 + myList2 or for index in myList2: myList1.append(index) or myList1.extend(myList2) print (myList1) Output: [10, 20, 30, 40, 50, 60, 70, 80]
  • 11. Tuple List • General Purpose • Most widely used data structure • Change size as needed • Sequence type • Sortable • Uses square bracket “[…]” • Allow duplicate values Tuple • Immutable (Can’t add or change) • Useful for fixed data • Faster than lists • Uses round bracket “(…)” • Allow duplicate values Set • Store non-duplicate items • immutable • Very fast access vs Lists • Math Set Ops • Change size as needed • Unordered, unchangeable • Uses curly bracket “{…}” • duplicate values ignored Dictionary • Store non-duplicate items • Key/value pairs • Change size as needed • Unordered • Duplicate values ignored • Uses curly bracket “{…}”
  • 12. Examples (Tuple) myTuple = ("Apple", 10, "Orange", "Grapes", 20.0, 10) print(myTuple[3]) => Grapes print(myTuple[-1]) => 10 print(myTuple[2:4]) => (‘Orange’, ‘Grapes’) print(myTuple[:4]) => (‘Apple’, 10, ‘Orange’, ‘Grapes’) print(myTuple[2:]) => (‘Orange’, ‘Grapes’, 20.0, 10) print(myTuple[-4:-1]) => (‘Orange’, ‘Grapes’, 20.0)
  • 13. Sets List • General Purpose • Most widely used data structure • Change size as needed • Sequence type • Sortable • Uses square bracket “[…]” • Allow duplicate values Tuple • Immutable (Can’t add or change) • Useful for fixed data • Faster the lists • Sequence type • Uses round bracket “(…)” • Allow duplicate values Set • immutable • Math Set Ops • Change size as needed • Uses curly bracket “{…}” • duplicate values ignored Dictionary • Store non-duplicate items • Key/value pairs • Change size as needed • Unordered • Duplicate values ignored • Uses curly bracket “{…}”
  • 14. Example (Set) mySet1 = {"Apple", 10, "Orange", "Grapes", 20.0} mySet2 = {30.0, 10, "Orange", "Apple", 40}  Print (mySet1 | mySet2) or print(mySet1.union(mySet2)) {"Apple", 40, 10, "Grapes", 20.0, “Orange”, 30.0}  Print (mySet1 & mySet2) or print(mySet1.intersection(mySet2))  {10, ‘Apple’, ‘Orange’}  Print (mySet1 – mySet2) or print(mySet1.difference(mySet2))  {‘Grapes’, 20.0}  Print (mySet1 ^ mySet2) or print(mySet1.symmetric_difference(mySet2))  {‘Grapes’, 20.0, 30.0, 40} A A A A Union B A interest B A difference B A
  • 15. Dictionary List • General Purpose • Most widely used data structure • Change size as needed • Sequence type • Sortable • Uses square bracket “[…]” • Allow duplicate values Tuple • Immutable (Can’t add or change) • Useful for fixed data • Faster the lists • Sequence type • Uses round bracket “(…)” • Allow duplicate values Set • immutable • Very fast access vs Lists • Math Set Ops • Change size as needed • Uses curly bracket “{…}” • duplicate values ignored Dictionary • Store non-duplicate items • Key: value pairs • Mutable • Unordered • Uses curly bracket “{…}”
  • 16. Example (Dictionary) myFriends = {"Sandy":25, "John": 20, "Jane": 22} print(Friends.items())  output: ('Sandy’,25), ('John', 20), ('Jane', 22) print(Friends.keys())  output: ‘Sandy’, ‘John’, ‘Jane’ print(Friends.values())  output: 25, 20. 22 print(myFriends["Sandy“])  output: 25 myFriends["Sandy"] = 30 print (myFriends.items())  output: ('Sandy',30), ('John', 20), ('Jane', 22) myFriends.update({"Sandy": 40}) print (myFriends.items())  output: ('Sandy’,40), ('John', 20), ('Jane', 22) myFriends.pop("Sandy") print (myFriends.items())  output: ('John', 20), ('Jane', 22) myFriends.popitem() print (myFriends.items())  output: ('Sandy', 25), ('John', 20) del myFriends["Sandy"] print (myFriends.items())  output: ('John', 20), ('Jane', 22) del myFriends print (myFriends.items())  output: Error
  • 17. Sample Code myD = {"URL": "Uniform Resource Locator", "CPU": "Central Processing Unit", "RAM": "Random Access Memory"} print("=>" + str(myD.keys())) print("=>"+ str(myD.values())) print("=>" + str(myD.items())) print(myD["URL"]) del myD["CPU"] print("Size: " + str(len(myD))) myKeys = list(myD.keys()) myValues = list(myD.values()) for J in range(len(myD)): print(str(J +1) + ". "+ myKeys[ J ] + ": " + (myValues[ J ])) OUTPUT:  dict_keys(['URL', 'CPU', 'RAM'])  dict_values(['Uniform Resource Locator', 'Central Processing Unit', 'Random Access Memory'])  dict_items([('URL', 'Uniform Resource Locator'), ('CPU', 'Central Processing Unit'), ('RAM', 'Random Access Memory')])  Uniform Resource Locator  Size: 2  1. URL: Uniform Resource Locator  2. RAM: Random Access Memory
  • 18. Data Structure List • mutable • Uses square bracket “[value, …]” • General Purpose • Most widely used data structure • Allow duplicate values Tuple • Immutable • Uses round bracket “(value, …)” • Useful for fixed data • Allow duplicate values Sets • Immutable • Uses curly bracket “{value, …}” • Math Set Ops • duplicate values ignored Dictionary • Mutable • Uses curly bracket “{key: value, …}” • Key/value pairs • Non-duplicate keys • Unordered
  • 19. Free Online Python IDE https://replit.com Free Exercise https://www.w3resource.com/python-exercises/ Instructor-Led and Self-Paced Online Courses https://ed2go.com/nrclc --------- * ---------- Vazi Okhandiar NR computer Learning Center www.nrclc.com