0% found this document useful (0 votes)
3 views

Ch05 - Python - Data Structures - 1721754633

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Ch05 - Python - Data Structures - 1721754633

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Python Programming:

Data Structures
Lecturer: SEK SOCHEAT
E-mail: [email protected]
S2, Y1 (2023-2024) Mobile: +855 17 879 967
Contents
• Introduction to Data Structure
• List Data Structure
• Tuples Data Structure
• Sets Data Structure
• Dictionaries Data Structure
• String Data Structure
• Other Collections
• Exercises

2
Introduction to Data Structure
• A data structure is a way of organizing, storing, and manipulating data to
efficiently perform certain operations.
• It provides a systematic and efficient means to access and manage data.
• The choice of a data structure depends on the nature of the data, the
operations that need to be performed on the data, and the constraints of the
problem being solved.
• In Python, data structures are used to organize and store data efficiently.
• There are several built-in data structures that come with the language, and
each has its own characteristics and use cases.
• Understanding how to use these data structures and choosing the appropriate
one for a specific task is crucial for writing efficient and maintainable Python
code.
3
Introduction to Data Structure
Here are some of the common built-in data structures in Python:
• Lists:
▪ Lists are ordered, mutable collections of elements.
▪ Elements can be of different data types.
▪ You can add, remove, and modify elements in a list.
my_list = [1, 2, 3, "apple", "banana", True]
• Tuples:
▪ Tuples are ordered, immutable collections of elements.
▪ Once created, you cannot change the elements of a tuple.
my_tuple = (1, 2, 3, "apple", "banana", True)
• Sets:
▪ Sets are unordered collections of unique elements.
▪ They are useful for eliminating duplicate values.
my_set = {1, 2, 3, "apple", "banana", True}
4
Introduction to Data Structure
Here are some of the common built-in data structures in Python:
• Dictionaries:
▪ Dictionaries are unordered collections of key-value pairs.
▪ Each key must be unique and is associated with a value.
my_dict = {"name": "John", "age": 25, "city": "New York"}
• Strings:
▪ Strings are sequences of characters.
▪ They are immutable in Python, meaning you cannot change their characters once
they are created.
my_string = "Hello, World!“
• Other Collections:
▪ Python also provides other collection types like collections.deque (double-
ended queue) and collections.Counter (counter dictionary).
5
List Data Structure
In Python, a list is a mutable, ordered collection of elements. Lists allow you to store
and manipulate a sequence of items. Each element in a list can be of any data type,
and you can easily perform various operations on lists such as adding, removing, or
modifying elements.
Here are some examples of lists in Python: • Lists are versatile and
Creating a List: widely used in Python
for various tasks.
• Understanding how to
work with lists and
their operations is
essential for writing
effective Python code.

6
List Data Structure
Here are some examples of lists in Python:
Accessing Elements:

Modifying Elements:

7
List Data Structure
Here are some examples of lists in Python:
Adding Elements:

Removing Elements:

8
List Data Structure
Here are some examples of lists in Python:
Slicing:

List Comprehension:

9
Tuples Data Structure
In Python, a tuple is an immutable, ordered collection of elements. Once a tuple is
created, you cannot modify its elements, add new elements, or remove existing
elements. Tuples are often used for fixed-size collections of related items.
Here are some examples of tuples in Python:
Creating a Tuple:

10
Tuples Data Structure
Here are some examples of tuples in Python:
Accessing Elements:

Tuple Unpacking:

11
Tuples Data Structure
Here are some examples of tuples in Python:
Creating a Single-Element Tuple:

Concatenating Tuples:

12
Tuples Data Structure
Here are some examples of tuples in Python:
Repetition:

Nested Tuples:

13
Tuples Data Structure
Here are some examples of tuples in Python:
Returning Multiple Values from a Function:

• Tuples are commonly used in scenarios where you want to represent a


fixed set of related values, and immutability is desired.
• They are often employed in functions that return multiple values, as
shown in the last example.
• Understanding how to use tuples and their immutability is important for
writing robust and efficient Python code.
14
Sets Data Structure
In Python, a set is an unordered collection of unique elements. Sets are useful for
tasks that involve testing membership, removing duplicates from a sequence, and
performing mathematical set operations.
Here are some examples of sets in Python:
Creating a Set:

15
Sets Data Structure
Here are some examples of sets in Python:
Adding and Removing Elements:

16
Sets Data Structure
Here are some examples of sets in Python:
Set Operations:

17
Sets Data Structure • Sets are highly useful for scenarios where you
need to work with unique elements and
Here are some examples of sets in Python: perform operations such as union, intersection,
Set Comprehension: and difference.
• Understanding how to use sets is beneficial for
writing efficient and clean Python code.

Removing Duplicates from a List:

18
Dictionaries Data Structure
In Python, a dictionary is an unordered collection of key-value pairs. Each key in a
dictionary must be unique, and it is associated with a specific value. Dictionaries are
versatile and allow you to store and retrieve data using descriptive keys.
Here are some examples of dictionaries in Python:
Creating a Dictionary:

19
Dictionaries Data Structure
Here are some examples of dictionaries in Python:
Accessing Values:

Modifying Values:

20
Dictionaries Data Structure
Here are some examples of dictionaries in Python:
Adding a New Key-Value Pair:

Removing a Key-Value Pair:

21
Dictionaries Data Structure
Here are some examples of dictionaries in Python:
Checking for Key Existence:

Looping Through Keys and Values:

22
Dictionaries Data Structure
Here are some examples of dictionaries in Python:
Dictionary Comprehension:

• Dictionaries are widely used in Python for tasks such as storing configurations,
representing records, and efficiently retrieving information using descriptive keys.
• Understanding how to work with dictionaries is important for effective data
manipulation in Python.

23
String Data Structure
In Python, a string is a sequence of characters, and it is one of the built-in data types.
Strings are enclosed in either single ( ' ) or double ( " ) quotes.
Here are some examples and operations with strings:
Creating a String:

24
String Data Structure
Here are some examples and operations with strings:
Accessing Characters:

Slicing Strings:

25
String Data Structure
Here are some examples and operations with strings:
Concatenating Strings:

String Length:

26
String Data Structure
Here are some examples and operations with strings:
String Methods:

String Formatting:

27
String Data Structure
Here are some examples and operations with strings:
Escape Characters:

String Repetition:

28
String Data Structure
Here are some examples and operations with strings:
String Conversion:

• Strings are versatile in Python, and they come with a variety of methods and
operations for text manipulation.
• Understanding how to work with strings is essential for many programming tasks,
including data processing, user input handling, and text-based output.

29
Other Collections Data Structure
In addition to strings, lists, tuples, sets, and dictionaries, Python provides other
collections and data structures that serve specific purposes.
Here are some notable ones:
1. Deque (Double-Ended Queue):
A deque is a double-ended queue, which
allows adding and removing elements
from both ends efficiently.

30
Other Collections Data Structure
Here are some notable ones:
2. Counter: The Counter is a dictionary subclass for counting hashable objects. It is
particularly useful for counting occurrences of elements in a collection.

31
Other Collections Data Structure
Here are some notable ones:
3. NamedTuple: NamedTuple creates a simple class with named fields, which allows
accessing elements by name as well as by index.

32
Other Collections Data Structure
Here are some notable ones:
4. DefaultDict: A defaultdict is a dictionary subclass that provides a default value for
a nonexistent key.

33
Other Collections Data Structure
Here are some notable ones:
5. Heapq: Heapq provides an implementation of the heap queue algorithm, which is a
way to manage a priority queue efficiently.

• These collections and data structures are part of the Python standard library and
offer additional tools for specific use cases, such as efficient queue operations,
counting elements, named fields, and more.
• Understanding when and how to use these collections can improve the efficiency
and readability of your code.
34
Exercises:
Exercise 1: List Manipulation
Write a Python function that takes a list of numbers as input and returns a new list
containing only the even numbers from the original list.

def filter_even_numbers(numbers):
# Your code here

Exercise 2: Tuple Unpacking


Write a function that takes a tuple of coordinates (x, y, z) as input and returns the sum
of these coordinates.

def sum_coordinates(coordinates):
# Your code here

35
Exercises:
Exercise 3: Set Operations
Write a function that takes two sets as input and returns a new set containing the
common elements between the two sets.

def find_common_elements(set1, set2):


# Your code here

Exercise 4: Dictionary Manipulation


Write a function that takes a dictionary of student grades as input and returns the
average grade.

def calculate_average_grade(student_grades):
# Your code here

36
Thanks for attention,

Any Question?

37

You might also like