KEMBAR78
Lec 2 .PDF Python | PDF | Database Index | Data Structure
0% found this document useful (0 votes)
9 views25 pages

Lec 2 .PDF Python

The document is a lecture on data structures, detailing lists, tuples, and dictionaries in Python. It covers their definitions, functionalities, and differences, including methods for accessing, modifying, adding, removing, and copying items. Additionally, it includes assignments for practical application of the concepts discussed.

Uploaded by

ojana3622
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views25 pages

Lec 2 .PDF Python

The document is a lecture on data structures, detailing lists, tuples, and dictionaries in Python. It covers their definitions, functionalities, and differences, including methods for accessing, modifying, adding, removing, and copying items. Additionally, it includes assignments for practical application of the concepts discussed.

Uploaded by

ojana3622
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Lecture 2

Data Structures

Prepared by:
Ahmed Elhoseiny
Agenda
⚫ What is data structures ?

⚫ List (Access Items, Change Items, Add Items, Remove Items, Sort, Copy, Join)

⚫ Tuples (Access Items, Update, Join)

⚫ Dictionaries (Access Items, Change Items, Add Items, Remove Items, Copy,
Nested)

⚫ List vs Tuples vs Dictionaties


What is Data Structures ?
⚫ A data structure is a specialized format for organizing,
processing, retrieving, and storing data.

⚫ It enables efficient data management and manipulation,


providing a way to organize data so that it can be accessed
and modified effectively.

⚫ Data structures are fundamental to designing efficient


algorithms and are critical for software engineering,
database management, and various computational tasks.
List
⚫ Lists are used to store multiple items in a single variable.

⚫ Lists are one of 4 built-in data types in Python used to store


collections of data, the other 3 are Tuple, Set, and Dictionary,
all with different qualities and usage.

⚫ Use Cases: Suitable for ordered collections where elements may need
to be frequently added, removed, or modified.

⚫ To determine how many items a list has, use the len() function
List (Access Items)
⚫ List items are indexed and you can access them by
referring to the index number
⚫ List items are indexed, the first item has index [0], the
second item has index [1]
⚫ Negative indexing means start from the end [-1] refers to
the last item, [-2] refers to the second last item.
⚫ You can specify a range of indexes by specifying where to
start and where to end the range.
List (Change Items)
⚫ To change the value of a specific item, refer to the index number

⚫ To change the value of items within a specific range, define a list with the
new values, and refer to the range of index numbers where you want to
insert the new values

⚫ If you insert more items than you replace, the new items will be inserted
where you specified, and the remaining items will move accordingly

⚫ If you insert less items than you replace, the new items will be inserted
where you specified, and the remaining items will move accordingly
List (Add Items)
⚫ To add an item to the end of the list, use the append()
method

⚫ The insert() method inserts an item at the specified index

⚫ Or you can use the extend() method, where the purpose is


to add elements from one list to another list
List (Remove Items)
⚫ The remove() method removes the specified item
⚫ If there are more than one item with the specified value,
the remove() method removes the first occurrence
⚫ The pop() method removes the specified index
⚫ The del keyword also removes the specified index
⚫ The del keyword can also delete the list completely
⚫ The clear() method empties the list
List (Sort)
⚫ List objects have a sort() method that will sort the list
alphanumerically, ascending, numerically

⚫ To sort descending, use the keyword argument reverse=


True

⚫ The reverse() method reverses the current sorting order of


the elements.
List (Copy)
⚫ You cannot copy a list simply by typing list2 = list1

⚫ Make a copy of a list with the copy() method

⚫ Another way to make a copy is to use the built-in method


list()
List (Join)
⚫ One of the easiest ways are by using the + operator.

⚫ you can use the extend() method, where the purpose is to


add elements from one list to another list
Tuples
⚫ Tuples are used to store multiple items in a single variable.

⚫ A tuple is a collection which is ordered and unchangeable.

⚫ Use Cases: Suitable for read-only collections of items where


immutability is desired for ensuring data integrity.

⚫ To determine how many items a list has, use the len() function
Tuples (Access Items)
⚫ Tuple items are indexed and you can access them by referring
to the index number

⚫ Tuple items are indexed, the first item has index [0], the
second item has index [1]

⚫ Negative indexing means start from the end [-1] refers to the
last item, [-2] refers to the second last item.

⚫ You can specify a range of indexes by specifying where to start


and where to end the range.
Tuples(Update)
⚫ Once a tuple is created, you cannot change its values.
Tuples are unchangeable, or immutable as it also is called.

⚫ But there is a workaround. You can convert the tuple into


a list, change the list, and convert the list back into a tuple.
Tuples(Join)
⚫ To join two or more tuples you can use the + operator

⚫ If you want to multiply the content of a tuple a given


number of times, you can use the * operator
Dictionaries
⚫ Dictionaries are used to store data values in key:value
pairs.
Dictionaries(Access Items)
⚫ You can access the items of a dictionary by referring to its key
name, inside square brackets
⚫ There is also a method called get() that will give you the same
result
⚫ The keys() method will return a list of all the keys in the
dictionary.
⚫ The values() method will return a list of all the values in the
dictionary.
⚫ The items() method will return each item in a dictionary, as
tuples in a list.
Dictionaries(Change Items)
⚫ You can change the value of a specific item by referring to
its key name

⚫ The update() method will update the dictionary with the


items from the given argument.
Dictionaries(Add Items)
⚫ Adding an item to the dictionary is done by using a new
index key and assigning a value to it

⚫ The update() method will update the dictionary with the


items from a given argument. If the item does not exist,
the item will be added.
Dictionaries(Remove Items)
⚫ The pop() method removes the item with the specified key
name
⚫ The del keyword removes the item with the specified key
name
⚫ The clear() method empties the dictionary
Dictionaries(Copy)
⚫ You cannot copy a dictionary simply by typing dict2 =
dict1

⚫ There are ways to make a copy, one way is to use the


built-in Dictionary method copy()

⚫ Another way to make a copy is to use the built-in function


dict()
Dictionaries(Nested)
⚫ A dictionary can contain dictionaries, this is called nested
dictionaries.

⚫ Or, if you want to add three dictionaries into a new


dictionary

⚫ To access items from a nested dictionary, you use the


name of the dictionaries, starting with the outer dictionary
List vs Tuples vs Dictionaties
Assignment
⚫ 1- Code to print avrage this list [50,80,16,24,180]
⚫ Note: (Avrage = sum / len)

⚫ 2- Code to add ‘orange’ in this tuple (‘apple’ ,’banana’)

⚫ 3- code to add (color is blue) to this


Questions ?

You might also like