DigitalSkills&Python Chapter4
DigitalSkills&Python Chapter4
Chapter 4
Python Overview and Basic Syntax:
Python Data Structures: List, Tuple &
Dictionary
Pr. Mehdia AJANA
Email: [email protected]
PYTHON Lists
A list is a data structure used to store multiple
First steps items in a single variable.
Lists are created using square brackets [ and ]:
[ ] : empty list
2
Pr. Mehdia AJANA
Examples of Lists :
PYTHON >>> # Assigning a list to the variable "precious_stones"
Example :
list : ['A','C','D','E','F','G','H','I','K','L']
Positive Index : 0 1 2 3 4 5 6 7 8 9
Data Structures Negative Index : -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Lists Example 2:
>>> IP = [192, 162, 1, 1]
>>> # Duplicating the list IP 2 times
>>> IP2 = IP * 2
>>> IP2
[192, 162, 1, 1, 192, 162, 1, 1]
Pr. Mehdia AJANA 13
Lists Operations
PYTHON Adding Elements with « + »
First steps >>> Grades = [] # Initialize an empty list with []
>>> Grades
[]
>>># Add an element by concatenation
Data Structures >>> Grades = Grades + [15]
>>>#Concatenate the empty list [] with the list [15]
Lists >>> Grades #Querying Python for the value of Grades?
[15]
>>># Similarly adding a second element
>>> Grades = Grades + [-5]
>>> Grades
[15, -5]
Pr. Mehdia AJANA 14
PYTHON Lists Operations
Check if Item Exists
First steps To determine if a specified item is present in a list
use the in keyword.
Example:
Check if "apple" is present in the list:
Data Structures
list1 = ["apple", "banana", "cherry"]
Lists if "apple" in list1:
>>># [5, 6, 7, 8, 9]
>>> len(list(range(5,10)))
5
>>># [200, 300, 400, 500, 600, 700, 800, 900]
>>> len(list(range(200,1000,100)))
8
Pr. Mehdia AJANA 18
Predefined Functions for Lists
PYTHON
First steps The append() Method allows you to add an element to
the end of a list:
Example 1 :
Sort the list alphabetically:
Data Structures
>>> fruits = ["orange", "mango", "kiwi",
"pineapple", "banana"]
Lists
>>> fruits.sort()
>>>print(fruits)
Example 2 :
Sort the list numerically:
Data Structures
>>> thislist = [100, 50, 65, 82, 23]
Lists >>> thislist.sort()
>>>print(thislist)
[23, 50, 65, 82, 100]
Lists uppercase_fruits = []
uppercase_fruits.append(fruit.upper())
print(uppercase_fruits)
Lists
for every item in list, execute the expression if
the condition is True
Note: The if statement in list comprehension is
optional
Lists
Data Structures
Lists
Lists
b) [40, 60]
d) [60, 80]
b) [40, 60]
d) [60, 80]
a->b->c->d->e->
Error:
TypeError: 'tuple' object does not support item
assignment
Data Structures
>>>tuple1 = (‘Ali',) + tuple1[1:]
Tuple
>>> print(tuple1)
Number of Elements
>>> len(tup4)
8
Pr. Mehdia AJANA 63
Operations on Tuples
PYTHON Tuples are unchangeable, so you cannot remove
First steps their items.
Example: We want to delete an element from a
tuple using del:
>>> thistuple = ("apple", "banana", "cherry")
>>> del thistuple[2]
name Karim
phone 0644332211
The del keyword removes the item with the specified key
name:
>>> del thisdict["model"]
{'brand': 'Ford', 'year': 1964}
Pr. Mehdia AJANA 79
Remove Dictionary Items:
PYTHON The popitem() method removes the last inserted
First steps item (in versions before 3.7, a random item is
removed instead)
>>> thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
Data Structures
>>> thisdict.popitem()
Dictionary >>> print(thisdict)
{'brand': 'Ford', 'model': 'Mustang'}
brand
model
year
brand Ford
model Mustang
year 1964
Data Structures items() Returns a list containing a tuple for each key value pair
For more examples about Dictionary methods you can check the
following link: https://www.geeksforgeeks.org/python-dictionary-
methods/
Pr. Mehdia AJANA 93