The introduction to Python session is for students wanting to learn the basics of the Python programming language. Python is an easy-to-learn programming language. Python is used to develop websites and software, manipulate and analyze data, create data visualization and dashboard, build machine learning algorithms, and much more. Since 2018, the demand for Python programmers has been growing (https://www.tiobe.com/tiobe-index/).
In this session, you will learn about four Python data structures: list, dictionary, tuple, and sets.
Prerequisite: Knowledge of Python Basics.
Audience: Suitable for beginners interested in developing a program in Python using various data structures available in Python.
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
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 “{…}”