Uploaded by new life

Part2 PythonProgramming Data Strcutures

advertisement
See discussions, stats, and author profiles for this publication at: https://www.researchgate.net/publication/377529570
PYTHON PROGRAMMING 2
Book · January 2024
CITATIONS
READS
0
23
1 author:
Ahmed Shahin
Majmaah University
11 PUBLICATIONS 77 CITATIONS
SEE PROFILE
All content following this page was uploaded by Ahmed Shahin on 19 January 2024.
The user has requested enhancement of the downloaded file.
PYTHON PROGRAMMING
ENGINEERING AND TECHNICAL PROGRAMS
Dr. Ahmed Shahin
Main References
V0.1
Data Structure
in Python
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
3
DATA STRUCTURE IN PYTHON
 Data Structure
provides a particular way of organizing data so it can be accessed efficiently,
depending on your use case.
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
4
DATA STRUCTURE IN PYTHON
 What is meant by access data?
Delete
Sort
Remove
Retrieve
Update
Extend
Modify
ADD
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
5
DATA STRUCTURE IN PYTHON
Data
Structures
NonPrimitive
Primitive
Integer
Float
DR. AHMED I. SHAHIN
String
Boolean
Lists
a.shahin@mu.edu.sa
Tuple
Dictionary
Set
6
List
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
7
LIST
 List
❑ Is one of the Non-Primitive data structures in Python.
❑ To Construct an empty list,
List = [ ]
❑ The List is defined by Bracket [ ]
List = [ 1, 2, 4, 8 ]
Single Variable contains multi-items
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
8
LIST
❑ List accept non-same data type of items
List=[1,"ahmed",2.4]
Integer
String
Float
❑ Items in List will not be associated otherwise “ Dictionary” will be explained in Next
Course
List=["ahmed", 80, "Mohamed" , 95]
Non- Associated
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
9
LIST
 List contains heterogenous elements
“Indexing” How to retrieve data from List
Python Script
Item 0 Item 1
Item 2 Item 3
X= [1, 10, 100, 1000]
[1, 10, 100, 1000]
X [2]
100
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
10
LIST
 List contains heterogenous elements
“Indexing” How to retrieve data from List
Python Script
Item 0 Item 1
Item 2 Item 3
X= [1, 10, 100, 1000]
[1, 10, 100, 1000]
X [-2]
100
Item -4
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
Item -3 Item -2 Item -1
11
LIST
 Slicing ….“Indexing” How to retrieve data from Tuple
Python Script
Item 0 Item 1
Item 2 Item 3
X= [1, 10, 100, 1000]
[1, 10, 100, 1000]
X [0:2]
0:
[1,10]
DR. AHMED I. SHAHIN
2
End value is
not included
a.shahin@mu.edu.sa
12
LIST
 Slicing ….“Indexing” How to retrieve data from Tuple
Python Script
Item 0 Item 1
Item 2 Item 3
X= [1, 10, 100, 1000]
[1, 10, 100, 1000]
X [1:]
1:
End
[10,100,1000]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
13
LIST
 Slicing ….“Indexing” How to retrieve data from Tuple
Python Script
Item 0 Item 1
Item 2 Item 3
X= [1, 10, 100, 1000]
[1, 10, 100, 1000]
X [:-2]
[1,10]
Item -4
Item -3 Item -2 Item -1
Start
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
:-2
14
LIST
 Slicing ….“Indexing” How to retrieve data from Tuple
Python Script
Item 0 Item 1
Item 2 Item 3
X= [1, 10, 100, 1000]
[1, 10, 100, 1000]
X [:]
[1,10,100,1000]
Item -4
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
Item -3 Item -2 Item -1
15
LIST
 Slicing ….“Indexing” How to retrieve data from Tuple
Python Script
Item 0 Item 1
Item 2 Item 3
X= [1, 10, 100, 1000]
[1, 10, 100, 1000]
X [6]
IndexError: list index out of range
Item -4
Item -3 Item -2 Item -1
Start
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
:-2
16
LIST
❑ List is used to store multiple items in a single variable; However, List is mutable,
“Items in List can be modified.”
Python Script
Python Script
X=[1,2,3,4,5]
X=[1,2,3,4,5]
X [0] =6
X [-2] =20
X=[6,2,3,4,5]
X=[6,2,3,20,5]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
17
LIST
❑ Clear List Values
Python Script
X=[1,2,3,4,5]
X =[]
X=[]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
18
LIST
❑ Overwrite List Values
Python Script
X=[1,2,3,4,5]
X=[3,5,6]
X
X=[3,5,6]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
19
LIST
❑ Copy List to a new List
Python Script
X=[1,2,3,4,5]
Y=X
Y
Y=[1,2,3,4,5]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
20
LIST
 However, the List items are not mutable, it allows to add items in the
tuple structure
Python Script
X= [1, 10, 100, 1000]
X= X + [10000, 100000]
[1,10,100,1000,10000,100000]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
Using Plus
+
Operator
21
LIST
 However, the List items are not mutable, it allows to add items in the
tuple structure
Python Script
X= [1, 10, 100, 1000]
X= X * 2
[1,10,100,1000, 1,10,100,1000]
1
DR. AHMED I. SHAHIN
Using Multiply
*
Operator
2
a.shahin@mu.edu.sa
22
LIST
❑ Application of List in real life, Student Degrees
Student_degrees=[97,85,87,66,80]
❑ Application of List in real life, Student Names
Student_degrees=["Ahmed","AMR","MOHAMED"]
❑ Application of List in real life, Student Names, Degrees, Table
Student_degrees=["Ahmed",97,"AMR",85,"MOHAMED“,87]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
23
LIST
❑ Application of List in real life, music
music=[0.65,0.97,0.65,0.987,…………]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
24
Lesson 1 LAB
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
25
List
Methods
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
26
TERMS
 Function
 Method
Python Script
Python Script
X= Y. Method()
X= Function (Y)
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
27
LIST METHODS
❑ Python has a set of 11 built-in methods that you can use on lists/arrays.
No.
Method
Description
1
2
3
4
5
6
7
8
9
10
11
append()
clear()
copy()
count()
extend()
index()
insert()
pop()
remove()
reverse()
sort()
Adds an element at the end of the list
Removes all the elements from the list
Returns a copy of the list
DR. AHMED I. SHAHIN
Returns the number of elements with the specified value
Add the elements of a list (or any iterable), to the end of the current list
Returns the index of the first element with the specified value
Adds an element at the specified position
Removes the element at the specified position
Removes the first item with the specified value
Reverses the order of the list
Sorts the list
a.shahin@mu.edu.sa
28
LIST METHODS
The first search item
 Index Method
index ( X )
Item 0 Item 1
Python Script
X=[1,5,7,5]
X.index(5)
X
X=[1,
Item 2 Item 3
5, 7,
5]
1
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
29
LIST METHODS
New Item
 Append Method
Python Script
append ( X )
Python Script
Python Script
X=[1,5,7]
X.append(4)
X
X=[1,5,7]
X.append(4,14)
X
X=[1,5,7]
X.append([4,14])
X
X=[1,5,7,4]
list.append() takes exactly
one argument (2 given)
X=[1,5,7,4,[4,14]]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
30
LIST METHODS
 Append Method
append ( X )
Python Script
X=[1,5,7]
X.append(4)
X
X=[1,5,7,4]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
31
LIST METHODS
 Append Method
Python Script
append ( X )
Python Script
X=[1,5,7]
X.append(4)
X
X=[1,5,7]
X.append(4,14)
X
X=[1,5,7,4]
list.append() takes exactly
one argument (2 given)
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
32
LIST METHODS
 Append Method
Python Script
append ( X )
Python Script
Python Script
X=[1,5,7]
X.append(4)
X
X=[1,5,7]
X.append(4,14)
X
X=[1,5,7]
X.append([4,14])
X
X=[1,5,7,4]
list.append() takes exactly
one argument (2 given)
X=[1,5,7,4,[4,14]]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
33
LIST METHODS
New List
 Extend Method
extend ( X )
Python Script
Python Script
X=[1,5,7]
X.append([4,14])
X
X=[1,5,7]
X.extend([4,14])
X
X=[1,5,7,4, [4,14] ]
X=[1,5,7,4, 4,14 ]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
34
LIST METHODS
 Extend Method
Python Script
extend ( X )
Python Script
X=[1,5,7]
X.extend([4,14])
X
X=[1,5,7]
X=X + [4,14]
X
X=[1,5,7,4, 4,14 ]
X=[1,5,7,4, 4,14 ]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
Using Plus
+
Operator
35
LIST METHODS
X is the location and Y is the
value of the New Item
 Insert Method
Python Script
insert ( X , Y )
Python Script
Python Script
X=[1,5,7]
X.insert(0,4)
X
X=[1,5,7]
X.insert(1,3)
X
X=[1,5,7]
X.insert(3,1)
X
X=[4,1,5,7]
X=[1,3,5,7]
X=[1,5,7,1]]
Position of arguments is
critical
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
36
LIST METHODS
No Argument
 Clear Method
clear (
)
Python Script
X=[1,5,7]
X.clear()
X
X=[]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
37
LIST METHODS
 Clear Method
clear (
)
Python Script
Python Script
X=[1,5,7]
X.clear()
X
X=[1,5,7]
X=[]
X
X=[]
X=[]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
38
LIST METHODS
Remove the item
 Remove Method
remove (
)
Python Script
X=[1,5,7]
X.remove(5)
X
X=[1,7]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
39
LIST METHODS
 Remove Method
Python Script
remove (
)
Python Script
X=[1,5,7]
X.remove(5)
X
X=[1,5,7,5]
X.remove(5)
X
X=[1,7]
X=[1,7,5]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
40
LIST METHODS
 Remove Method
Python Script
remove (
)
Python Script
Python Script
X=[1,5,7]
X.remove(5)
X
X=[1,5,7,5]
X.remove(5)
X
X=[1,5,7]
X.remove(4)
X
X=[1,7]
X=[1,7,5]
ValueError: list.remove(x):
x not in list
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
41
LIST METHODS
No Arguments
Pop (
 POP Method
)
Python Script
X=[1,5,5,7]
X.pop()
X
[1,5,5]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
42
LIST METHODS
No Arguments
copy (
 Copy Method
)
Python Script
X=[1,5,7]
Y=X.copy()
Y
Y=[1,5,7]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
43
LIST METHODS
copy (
 Copy Method
)
Python Script
Python Script
X=[1,5,7]
Y=X.copy()
Y
X=[1,5,7]
Y=X
Y
Y=[1,5,7]
Y=[1,5,7]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
44
LIST METHODS
How many time this item is
repeated
 Count Method
Count (
)
Python Script
Python Script
X=[1,5,5,7]
X.count(5)
X=[1,5,5,7]
X.count(3)
2
0
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
45
LIST METHODS
 Count Method
Count (
)
Python Script
Python Script
X=[1,5,5,7]
X.count(5)
X=[1,5,5,7]
X.count(3)
2
0
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
46
LIST METHODS
sort (
 Sort Method
)
The sort() method sorts the
list ascending by default.
Python Script
X=[1,5,7,3]
X.sort()
X
[1,3,5,7]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
47
LIST METHODS
sort (reverse=True )
 Sort Method
Python Script
The sort() method sorts the
list descending
Python Script
X=[1,5,7,3]
X.sort()
X
X=[1,5,7,3]
X.sort(reverse=True|)
X
[1,3,5,7]
[7,5,3,1]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
48
LIST METHODS
No Arguments
 Reverse Method
reverse (
)
Python Script
X=[1,5,7,3]
X.reverse()
X
[3,7,5,1]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
49
Lesson 2 LAB
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
50
List
Functions
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
51
LIST FUNCTIONS
❑ Python has a set of 13 built-in methods that you can use on lists/arrays.
No.
Method
Description
1
2
3
4
5
6
7
8
9
10
len
list
range
sum
min
max
sorted
reversed
enumerate
zip
Returns the number of element in the list .
DR. AHMED I. SHAHIN
Creates a list out of an iterable.
Returns an iterator of integers from start to stop, with an increment of step.
Adds all items of an iterable.
Gets the smallest item in a sequence.
Gets the largest item in a sequence.
Returns a new list of sorted items in iterable.
Reverses an iterator.
Returns an enumerate object.
Returns an iterator that aggregates items from each iterables.
a.shahin@mu.edu.sa
52
LIST FUNCTIONS
❑ Python has a set of 13 built-in methods that you can use on lists/arrays.
No.
Method
Description
11
12
13
map
filter
iter
Returns iterator that applies function to each item of iterables.
DR. AHMED I. SHAHIN
Returns an iterator from elements of iterable for which function returns true.
Converts an iterable into an iterator.
a.shahin@mu.edu.sa
53
LIST FUNCTIONS
List
 len Function
len (
)
Python Script
X=[1,5,7,3]
Len(X)
4
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
54
LIST FUNCTIONS
List
 Sum Function
sum (
)
Python Script
X=[1,5,7,3]
sum(X)
16
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
55
LIST FUNCTIONS
List
 Max Function
max (
)
Python Script
X=[1,5,7,3]
max(X)
7
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
56
LIST FUNCTIONS
List
 Min Function
min (
)
Python Script
X=[1,5,7,3]
min(X)
1
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
57
LIST FUNCTIONS
Sequence or iterable
list (
 List Function
)
Python Script
Python Script
X=(1,5,7,3)
Y=list(X)
Y
X="Ahmed"
Y=list(X)
Y
[1,5,7,3]
[“A“ ,“ h“,“ m“,“ e“,“ d“]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
58
LIST FUNCTIONS
 Sorted Function
sorted (
)
The sort() method sorts the
list ascending by default.
Python Script
X=[1,5,7,3]
Y=sorted(X)
X
[1,3,5,7]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
59
LIST FUNCTIONS
 Sorted Function
Python Script
Python Script
X=[1,5,7,3]
Y=sorted(X)
X
X=[1,5,7,3]
X.sort()
X
[1,3,5,7]
[1,3,5,7]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
60
LIST FUNCTIONS
List
 Reversed Function
reversed (
)
Python Script
X=[1,5,7,3]
Y=reversed(X)
Y=list(Y)
[3,7,5,1]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
61
LIST FUNCTIONS
 Reversed Function
reversed (
Python Script
)
Python Script
X=[1,5,7,3]
Y=reversed(X)
Y=list(Y)
X=[1,5,7,3]
X.reverse()
X
[3,7,5,1]
[3,7,5,1]
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
62
Lesson 3 LAB
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
63
Python
Tuples
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
TUPLES
 Tuple
❑ Is one of the Non-Primitive data structures in Python.
❑ To Construct an empty tuple,
Tuple = ()
❑ The Tuple is defined by parenthesis ( )
Tuple = ( 1, 2, 4, 8 )
If you write a sequence of numbers with a comma, the python
defines it as a tuple.
Tuple = 1, 2, 4, 8
65
TUPLES
❑ Tuple is used to store multiple items in a single variable, same as List Data Structure.
However, List is mutable, and Tuple is Un mutable. “Items in Tuple can not be
modified.”
Python Script
Python Script
X=[1,2,3,4,5]
X= (1,2,3,4,5)
X [0] =6
X [0] =6
X=[6,2,3,4,5]
TypeError: 'tuple' object does
not support item assignment
66
TUPLES
❑ Tuple is like list accept non-same data type of items
Tuple=(1,"ahmed",2.4)
Integer
Float
String
67
TUPLE
 Tuple contains heterogenous elements
“Indexing” How to retrieve data from Tuple
Python Script
Item 0 Item 1
Item 3 Item 4
X= (1, 10, 100, 1000)
X [2]
(1, 10, 100, 1000)
100
68
TUPLE
 “Indexing” How to retrieve data from Tuple
Python Script
Item -4 Item -3 Item -2 Item -1
X= (1, 10, 100, 1000)
X [-2]
(1, 10, 100, 1000)
100
69
TUPLE
 “Indexing” How to retrieve data from Tuple
Python Script
Item -4 Item -3 Item -2 Item -1
X= (1, 10, 100, 1000)
X [0:2]
(1, 10, 100, 1000)
(1,10)
70
TUPLE
 “Indexing” How to retrieve data from Tuple
Python Script
Item 0 Item 1
Item 3 Item 4
X= (1, 10, 100, 1000)
X [1:]
(1, 10, 100, 1000)
1:
End
(10,100,1000)
71
TUPLE
 “Indexing” How to retrieve data from Tuple
Python Script
Item -4 Item -3 Item -2 Item -1
X= (1, 10, 100, 1000)
X[:-2]
(1, 10, 100, 1000)
Start
:-2
(1,10,100)
72
TUPLE
 However, the Tuple items are not mutable, it allows to add items in
the tuple structure
Python Script
X= (1, 10, 100, 1000)
X= X + (10000, 100000)
(1,10,100,1000,10000,100000)
Using Plus
+
Operator
73
TUPLE
 However, the Tuple items are not mutable, it allows to add items in
the tuple structure
Python Script
X= (1, 10, 100, 1000)
X= X * 2
(1,10,100,1000, 1,10,100,1000)
1
Using Multiply
*
Operator
2
74
TUPLE
 However, the Tuple items are not mutable, it allows to add items in
the tuple structure
Python Script
X= (1, 10, 100, 1000)
X= X.append(10000,100000)
AttributeError: 'tuple' object has
no attribute 'append'
Tuple has not
an append
method
75
TUPLE METHODS
Python has two built-in methods that you can use on tuples.
Method
Description
count()
Returns the number of times a specified
value occurs in a tuple
index()
Searches the tuple for a specified value and
returns the position of where it was found
76
TUPLE
 Count Method
The checked item
Tuple.count (
)
Python Script
Tuple=(1,2,1,1)
Tuple.count(1)
3
Count returns
how many
times 1 is
repeated
77
TUPLE
 Count Method
Python Script
Tuple=(1,2,1,1)
Tuple.count(100)
0
Count returns
0, if the item is
not found.
78
TUPLE
 Index Method
The checked item
Tuple.index (
)
Python Script
Tuple=(1,6,3,2)
Tuple.index(3)
2
Index returns
the location of
the item
79
TUPLE
 Index Method
Python Script
Tuple=(1,6,3,2)
Tuple.index(100)
ValueError: tuple.index(x): x not
in tuple
Index returns
error, if the
item is not
found.
80
TUPLE
 Index Method
Python Script
Tuple=(1,6,3,6)
Tuple.index(6)
1
Index returns
only the first
one if the
item is
repeated
81
Python
Dictionaries
DR. AHMED I. SHAHIN
a.shahin@mu.edu.sa
DICTIONARY
 Dictionary
❑ Is one of the Non-Primitive data structures in Python.
❑ To Construct an empty dictionary,
Dict = { }
❑ The Dictionary is defined by braces { }
Dict={1: 'Ahmed','Age':25 ,'Degrees': [100, 67, 89, 99] , 2:
'ZIZO','Age':23 ,'Degrees': [94, 88, 78, 99]}
83
DICTIONARY
 Dictionary contains Items, each item consists of key-value paired elements
Dict =
Item 0
{
"brand": "Ford",
"Height":1.10,
Item 1
Item 2
"electric": False,
Item 3
"year": 1964,
Item 4
"colors": ["red", "white", "blue"]
}
84
DICTIONARY
 Dictionary contains Items, each item consists of key-value paired elements
Dict =
Item 0
{
"brand": "Ford",
"Height":1.10,
Item 1
Item 2
"electric": False,
Item 3
"year": 1964,
Item 4
"colors": ["red", "white", "blue"]
}
85
DICTIONARY
❑ Dictionary accepts non-same data type of items
Dict =
{
"brand": "Ford",
String
"Height":1.10,
Float
"electric": False,
Boolean
"year": 1964,
Integer
"colors": ["red", "white", "blue"]
List
}
86
DICTIONARY
❑ Dictionary accepts non-same data type of items
Dict =
{
"brand": "Ford",
String
"Height":1.10,
Float
"electric": False,
Boolean
"year": 1964,
Integer
"colors": ("red", "white", "blue“)
Tuple
}
87
DICTIONARY
❑ Dictionary is used to store multiple items in a single variable, same as List, Tuple Data
Structure. However, Dictionary is mutable “Items in Dictionary can be modified.”
Python Script
Dict =
Dict =
{
"brand": "Ford",
{
"brand": "Ford",
"Height":1.10,
"Height":1.5,
}
}
Dict['Height']=1.5
88
DICTIONARY
❑ ADDING Items to the Dictionary
Python Script
Dict =
Dict =
{
"brand": "Ford",
{
"brand": "Ford",
"Height":1.5,
"Height":1.10,
}
"Engine" = "Benzine"
}
Dict["Engine"] = "Benzine"
89
DICTIONARY
 Methods
Method
Description
clear()
Removes all the elements from the dictionary
copy()
Returns a copy of the dictionary
fromkeys()
Returns a dictionary with the specified keys and
value
get()
Returns the value of the specified key
items()
Returns a list containing a tuple for each key value
pair
keys()
Returns a list containing the dictionary's keys
90
DICTIONARY
 Methods Cont.
Method
Description
pop()
Removes the element with the specified key
popitem()
Removes the last inserted key-value pair
setdefault()
Returns the value of the specified key. If the key does not
exist insert the key, with the specified value
update()
Updates the dictionary with the specified key-value pairs
values()
Returns a list of all the values in the dictionary
91
DICTIONARY
❑Items Method
Python Script
No Arguments
Dict.items (
)
[
( "brand": "Ford" ),
Dict =
( "Height": 1.5),
{
"brand": "Ford",
]
"Height":1.10,
}
Dict.items( )
92
DICTIONARY
❑Keys Method
Python Script
Dict =
No Arguments
Dict.keys (
)
[
'brand’
,
{
"brand": "Ford",
"Height":1.10,
'Height'
]
}
Dict.keys()
93
DICTIONARY
❑Values Method
Python Script
Dict =
No Arguments
Dict.values (
)
[
"Ford"
,
{
"brand": "Ford",
"Height":1.10,
1.10
]
}
Dict.values()
94
DICTIONARY
❑Get Method
Key name
Dict.get (
)
Python Script
Dict =
"Ford"
{
"brand": "Ford",
"Height":1.10,
}
Dict.get("brand")
95
DICTIONARY
❑Fromkeys Method
[Keys,Values]
Dict.fromkeys (
Python Script
Keys=
[ 'name’, 'Age’
Values= [ 'Ahmed’ ,25
)
{
]
]
'name': 'Ahmed’,
'Age': 25
Dict.fromkeys(Keys,Values)
}
96
DICTIONARY
❑Clear Method
Python Script
Dict =
No Argument
Dict.clear (
)
Dict =
{
}
{
"brand": "Ford",
"Height":1.10,
}
Dict.clear()
97
DICTIONARY
❑Clear Method
No Argument
Dict.clear (
)
Python Script
Dict =
{
Python Script
Dict =
{
"brand": "Ford",
"brand": "Ford",
"Height":1.10,
"Height":1.10,
}
Dict.clear()
}
Dict={ }
98
DICTIONARY
❑POP Method
Python Script
Dict =
Key
Dict.pop (
)
Dict =
{
"brand": "Ford",
{
"brand": "Ford",
}
"Height":1.10,
}
Dict.pop("Height")
99
DICTIONARY
❑POPitem Method
Python Script
Dict =
{
"brand": "Ford",
"Height":1.10,
"color":1.10,
}
No Argument
Dict.popitem (
)
Dict =
{
"brand": "Ford",
"Height":1.10,
}
Dict.popitem()
100
DICTIONARY
{ [Key] : new_value }
❑Update Method
Dict.update (
Python Script
Dict =
)
Dict =
{
"brand": "Ford",
{
"brand": "Ford",
"Height":1.10,
"Height":1.5,
}
}
Dict.update({'Height']:1.5})
101
DICTIONARY
No Arguments
❑Copy Method
Dict.copy (
Python Script
Dict =
)
New_Dict =
{
"brand": "Ford",
{
"brand": "Ford",
"Height":1.10,
"Height":1.10,
}
}
New_Dict=Dict.copy()
102
DICTIONARY
No Arguments
❑Copy Method
Dict.copy (
)
Python Script
Python Script
Dict =
{
"brand": "Ford",
"Height":1.10,
}
New_Dict=Dict.copy()
Dict =
{
"brand": "Ford",
"Height":1.10,
}
New_Dict=Dict
103
DICTIONARY
{ [Key] : new_value }
❑Update Method
Dict.update (
Python Script
Dict =
)
New_Dict =
{
"brand": "Ford",
{
"brand": "Ford",
"Height":1.10,
"Height":1.10,
}
}
New_Dict=Dict.copy()
104
DICTIONARY
Key,value
❑Setdefault Method
Dict.setdefault (
Python Script
Dict =
)
New_Dict =
{
"brand": "Ford",
{
"brand": "Ford",
"Height":1.10,
"Height":1.10,
}
}
Dict.setdefault("Height",1.9)
105
DICTIONARY
Key,value
❑Setdefault Method
Dict.setdefault (
Python Script
Dict =
)
New_Dict =
{
"brand": "Ford",
{
"brand": "Ford",
"Height":1.10,
"Height":1.10,
"width":1.9
}
Dict.setdefault("width",1.9)
}
106
DICTIONARY
 Clear Method
Python Script
Dict=(1,2,1,1)
Tuple.count(1)
3
Count returns
how many
times 1 is
repeated
107
Thanks
DR. AHMED I. SHAHIN
View publication stats
a.shahin@mu.edu.sa
108
Download