Sidama Public Health Institute (SPHI)
Regional Data Management and Analytics Center
(RDMC)
Python Training-Data Structure
By Senbeto K. Hawassa, Sidama
(MSc in Information Technology January, 2025
python data
structures
Data Structures in Python
3
Data structure is a particular way of organizing and storing data in
a computer so that it can be used efficiently.
Different kinds of data structures are suited to different kinds of
applications, and some are highly specialized for specific tasks.
Key Data Structures in Python:
Lists:
Tuples
Dictionaries
Sets
Lists
4An ordered, mutable (changeable) sequence of items.
Lists are used to store multiple items in a single variable.
Items in a list can be of different data types.
Lists are defined using square brackets [].
E.g. my_list = [1, 2, "hello", 3.14]
Lists …
5Key Features:
Ordered: Elements have a specific order.
Mutable: You can add, remove, or modify elements.
Duplicates Allowed: Lists can contain duplicate elements.
Indexing: Elements can be accessed by their index
(position), starting from 0. the first item has index [0], the
second item has index [1] etc.
Access List Items---
6List items are indexed and you can access them by
referring to the index number
Example
symptoms = ["fever", "cough", "fatigue", "headache",
"sore throat"]
# Accessing the first symptom
Negative indexing means start from the end
Print(symptoms[0])
-1 refers to the last item, -2 refers to the second last item
etc.
Eg. Print(symptoms[-1])
Lists…
7Range of Indexes: You can specify a range of indexes by
specifying where to start and where to end the range.
Example-1:
symptoms = ["fever", "cough", "fatigue",
"headache", "sore throat"]
Print(symptoms[1:3])
Example-2: # Adding a new symptom
symptoms.append("loss of taste or smell")
print (symptoms)
Resul ['fever', 'cough', 'fatigue', 'headache', 'sore throat', 'loss
t of taste or smell']
Lists…
8Range of Indexes: You can specify a range of indexes by
specifying where to start and where to end the range.
Example-1:
symptoms = ["fever", "cough", "fatigue",
"headache", "sore throat"]
Print(symptoms[1:3])
Example-2: # Adding a new symptom
symptoms2 = ["Runny Nose", "dearrha",
"sleepy"]symptoms.append(symptoms2)
print (symptoms)
Resul ['fever', 'cough', 'headache', 'sore throat', 'loss of taste or
t smell', ['Runny Nose', 'dearrha', 'sleepy']]
Lists…
9Check if Item Exists To determine if a specified item is present in a
list use the in keyword:
Example 1: Check if "apple" is present in the list:
fruit = ["apple", "banana",
"cherry"]
if "apple" in fruit:
print("Yes, 'apple' is in the
fruits list")
else:
print("No, 'apple' in the fruits
list")
Lists…
10
Example 1: Example: Storing a list of daily case counts for a
disease outbreak:
daily_cases = [15, 22, 30, 28, 35, 40, 38] # Daily new cases
total_cases = sum(daily_cases) # Calculate the total number
of cases
print ("Total daily cases = " , total_cases)
Resu Total daily cases = 208
lt
Tuple in python
A tuple is a collection which is ordered (items have a defined order,
and that order will not change)
unchangeable (we cannot change, add or remove items after the
tuple has been created). We will store data items whose values
couldn't change
Allow duplicate values (Since tuples are indexed, they can have
items with the same value)
Tuple items are indexed, the first item has index [0], the second item has
index [1] etc.
Tuples are written with round brackets ().
Tuple in python …
Access Tuple Items:
We can access tuple items by referring to the index number, inside
square brackets:
Example
mytuple = ("apple", "banana", "cherry", "orange", "kiwi",
"melon", "mango")
print(mytuple[2])
print(mytuple[2:5])
Resu Cherry
lt ('cherry', 'orange', 'kiwi')
Python Dictionaries
Dictionaries are used to store data values in key:value pairs.
A dictionary is a collection which is ordered, changeable and do
not allow duplicates.
Dictionaries are written with curly brackets { }, and have keys
and values: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
Resu {'brand': 'Ford', 'model': 'Mustang', 'year':
lt 1964}
Python Dictionaries
Dictionary items are presented in key:value pairs, and can be
referred to by using the key name.
Example: Print the "brand" value of the dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["brand"])
Resu Ford
lt
Python Dictionaries
Dictionaries cannot have two items with the same key.
Duplicate values will overwrite existing values.
Example: duplicate key name “year" in dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
"year": 2020
}
print(thisdict)
Resu {'brand': 'Ford', 'model': 'Mustang', 'year':
lt 2020}
Python Dictionaries …
Exercise: Storing information about a patient:
patient_data = {
"patient_id": "PH12345",
"age": 55,
"gender": "Female",
"diagnosis": "Hypertension",
"medications": ["Lisinopril", "Amlodipine"]
}
# Accessing the patient's age
print(patient_data["age"])
# Adding a new key-value pair
patient_data["city"] = "Anytown"
print (patient_data)
Sets in Python
Sets:
An unordered collection of unique items.
Mutable: You can add or remove elements.
Unique Elements: Sets automatically eliminate duplicate elements.
Fast Membership Testing: Sets are very efficient for checking if an
element is present in the set.
Sets are defined using curly braces {} or the set() constructor.
Example
my_set = {1, 2, 3, 4}
print(my_set)
Recap
Python data structure(Collections or Arrays)
There are four collection data types in the Python programming
language:
• List is a collection which is ordered and changeable. Allows
duplicate members.
• Tuple is a collection which is ordered and unchangeable. Allows
duplicate members.
• Set is a collection which is unordered, unchangeable, and
unindexed. No duplicate members.
• Dictionary is a collection which is ordered and changeable. No
duplicate members.
Best sites for Python Tutorial
https://www.w3schools.com/python/
https://www.tutorialspoint.com/python/
https://www.learnpython.org/
20 .
21
Data Access and Sharing Policy