Python02 Collections-123456789
Python02 Collections-123456789
Python
Sara Jurczyk
Collection Can we sort Can we Can we store
4 types of elements? modify
elements?
duplicates?
Set No Yes No
• We use different type of bracket to indicate the type of
collection
Dictionary No Yes No
• Python does not have built-in support for Arrays, but
Python Lists can be used instead.
• To work with arrays in Python you will have to import a
library, like the NumPy library.
List
• The most basic and most often used
• Creating an empty list:
• 2. way:
List
• Checking if element is on the list:
List
• Accessing only part of the list:
List – method count
• Counting the number of appearances
List
• Returning index of the first appearance
List – sorting elements
List – reversing list
Lists in Python can store different data types
Similar to list, We have to
but cannot be define
modified – it is elements while
immutable declaring a
after creating tuple
Tuple
We can refer to
No functions to
elements and
add or remove
iterate over
elements are
them similarly
available
to list
Tuple
• Creating a tuple and iterating over it:
Tuple
two methods available: count and index
Tuple is a result if we return multiple function
results
Set
Only unique
No indexes No sorting values, no
duplicates
Set
• Craeting an empty set:
• Note that element are not display in the order of adding tchem. There
is no order!
What happens if we try to add the same
element?
•
Set - no index, no order, no sorting
Set
• We cannot refer to element by index
BUT
we can refer to the value,
e.g. we can check if an element is on the set
Set – removing elements
• We can also remove an element by its value
Set – removing all elements
Set – difference
Set – difference_update
• Deletes elements that are in the second set
Set - union
• Returns the sum of sets
Set - update
Dictionary
• Set of pairs key- value
Dictionary - getting value by its key
Dictionary – changing the value
Dictionary – iterating over keys
Dictionary – iterating over values
Dictionary – iterating over keys and values
Dictionary – removing elements by key
Dictionary – simple example
EXERCISE 1
• Create two lists – one storing grades a student got during the first
semester and one storing grades the student got during the second
semester.
• Create a list storing grades from the whole year, based on values in
those lists.
• Display on the screen the list of all grades.
• Find the average based on all grades.
• Find the number of appearances of a grade given by user.
Example solution
EXERCISE 2
• Create a mini game ☺ Declare a list inventory that stores sword, shield and
one healing potion. Display information about user’s inventory.
• Declare a tuple starting_chest that stores some gold (integer) and magic
ball. Display information about items found.
• Increase user’s inventory by items in chest. Remember to empty the chest
too. Then, display new inventory status.
• Prepare a dictionary that stores monsters names as keys and monsters
power as values. Let user fight with the first monster. User can beat a
monster if he enters his power (let user guess the power and after every
attempt display information if the power is bigger or lower). If during a
fight user made more than 5 attacks (attempts), he uses a healing potion.
Thank you!