0% found this document useful (0 votes)
3 views20 pages

Data Structures Python

The document provides an overview of Python data structures, including lists, tuples, sets, and dictionaries. It explains the characteristics of each type, such as mutability and ordering, and includes examples of how to create and manipulate these structures. Additionally, it highlights the use of dictionaries for mapping keys to values, emphasizing their flexibility in storing data.
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)
3 views20 pages

Data Structures Python

The document provides an overview of Python data structures, including lists, tuples, sets, and dictionaries. It explains the characteristics of each type, such as mutability and ordering, and includes examples of how to create and manipulate these structures. Additionally, it highlights the use of dictionaries for mapping keys to values, emphasizing their flexibility in storing data.
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

Python Data Structures

Built in Types

• Lists
• Tuples
• Sets
• Dictionaries
Terms We'll Use

• Data structure-A way of organizing or storing information.


• Ordered-The data structure has the elements stored in an
ordered sequence
• Mutable-The contents of the data structure can be
changed.
• Immutable-The contents of the data structure cannot be
changed.
Lists
• An ordered group of items

• Does not need to be the same type

– Could put numbers, strings or donkeys in the same list

– List contain items separated by a comma, and enclosed


within square brackets[]. They are mutable.

• List notation

– A = [1,”This is a list”, c, Donkey(“Kong”)]


Methods of Lists
• [Link](x)
– adds an item to the end of the list

• [Link](L)
– Extend the list by appending all in the given list L

• [Link](I,x)
– Inserts an item at index I

• [Link](x)
– Removes the first item from the list whose value is x
Examples of other methods
• a = [66.25, 333, 333, 1, 1234.5] #Defines List
– print ([Link](333), [Link](66.25), [Link]('x') ) #calls method
– 2 1 0 //output

• [Link]() #Reverses order of list


– print ([Link]()) returns None
[Link]()
print(a) #Prints reversed list
[1234.5, 1, 333, 333, 66.25] #Output

• [Link]()) #Sorts list in an ordered fashion


print(a)
– [ 1, 66.25, 333, 333, 1234.5] #Output

• [Link](333)
– print([Link](333)) #Returns the first index where the given value appears
– 1 #output

• print(a[0]) Output?????????
• print(a[1:3]) Output???????
• print(a+a) Output ???????
• How do we use len() and key?????????????
• Can we sort a list with both numbers and strings using sort()????????
Tuples
Tuples are ordered, immutable collections of elements.

The only difference between a tuple and a list is that once a


tuple has been made, it can't be changed!

Tuples contain items separated by a comma, and are enclosed


in parenthesis.

Making a tuple:
a = (1, 2, 3)
Examples
a = (66.25, 333, 333, 1, 1234.5) #Defines Tuple

• print(a) #Prints tuple a


– 66.25, 333, 333, 1, 1234.5 #Output

• [Link](333)
– print([Link](333) ) #Returns the first index where the given
value appears
– 1 #output
sort(a) reverse(a) ???????????????
• print(a[0])Output?????????

• print(a[0:3]) Output???????

• print(a+a) Output ???????


Tuples So Far
Why would you want a tuple?

Sometimes it's important that the contents of something not be


modified in the future.

Instead of trying to remember that you shouldn't't modify


something, just put it in a tuple.
Sets
A set is an unordered collection of elements where each
element must be unique. Attempts to add duplicate
elements are ignored.
Sets
Creating a set:
mySet = set(['a', 'b', 'c', 'd'])
Or:
myList = [1, 2, 3, 1, 2, 3]
mySet2 = set(myList)
Note that in the second example, the set would consist of
the elements ??????
Sets
Things we can do with a set:

mySet = set(['a‘])

# Add an element:
[Link](‘e')
print(mySet)
[Link](‘a')
print(mySet)
Output ???????????

#Remove an element:
[Link]('b')
print(mySet)
Output ????????????
Sets
There is also support for combining sets.
Returns a new set with all the elements from both sets:
[Link](someOtherSet)
Returns a new set with elements that were in both sets:
[Link](someOtherSet)
Tons more methods can be found here:
[Link]
Dictionaries
Lists can be viewed as a structure that map indexes to values.

If I make the list:

myList = ['a', 'b', 'c']

I have created a mapping from 0 to 'a', 1 to 'b', and so on.


If I put in 0, I'll get 'a' back.
Dictionaries
Dictionaries let use whatever kind of keys we want!
Instead of having 1 correspond to 'b', I can have "hello"
correspond to 'b'.
Before: Now I can do things like:
0 'a' "Hello" 'a'
1 'b' 1 'b'
2 'c' 3.3 'c'
Dictionaries
myDict = {}

Keys are enclosed in single quotes (‘’)


plus a colon separated by a comma.

Values are enclosed in double quotes if


they are strings. (“”)

e.g.

myDict ={‘student’: “John”, ‘age’: 20}


Dictionaries
When you look up something in a dictionary, the thing you're
putting in (like the index in a list) is called a key.

What we get out is called a value. A dictionary maps keys to


values.

myDict["hello"] = 10
^ ^
Key Value
Dictionaries
If we want to get just the keys, or just the values, there's a
function for that!

listOfKeys = [Link]()

listOfValues = [Link]()
Dictionaries
e.g
dict= {ʻnameʼ: “Rise”, ʻcodeʼ:8, ʻdeptʼ: “CS”}
Determine the Output of the following:

print(dict[ʻnameʼ])

print(dict)

print([Link]())

print([Link]())
Dictionaries
Homework

Imagine you have a bunch of university students, and you're


storing their grades in all their classes.

Use a dictionary to look up grades by name:

You might also like