[go: up one dir, main page]

0% found this document useful (0 votes)
33 views50 pages

Python02 Collections-123456789

The document discusses different types of collections in Python including lists, tuples, sets, and dictionaries. It provides examples and explanations of how to use each type of collection, their properties and common operations like adding and removing elements. It also includes exercises for the reader to practice working with collections.

Uploaded by

gyanendra pandey
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)
33 views50 pages

Python02 Collections-123456789

The document discusses different types of collections in Python including lists, tuples, sets, and dictionaries. It provides examples and explanations of how to use each type of collection, their properties and common operations like adding and removing elements. It also includes exercises for the reader to practice working with collections.

Uploaded by

gyanendra pandey
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/ 50

Collections in

Python

Sara Jurczyk
Collection Can we sort Can we Can we store
4 types of elements? modify
elements?
duplicates?

collections List Yes Yes Yes


• Collections are used to store multiple items in a single Tuple Yes No Yes
variable.

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:

• Creating a list with values:

• Adding elements (as the last element) – append method:


List – elements are indexed
• Indexes starts from 0
List – we can use index to refer to an element
and modify it
List – it is possible to use negative index
List
• Len function returns the length of a list
List
• Method insert allows to add element under the specify index
List
• Iterating over a list:
List – removing elements
• Method pop (without parameter we delete the last value)
List – removing elements
• Method pop (we delete value at the given index):
List – removing elements
• Method remove – delete element with a value:
List – removing elements
• Method del with given index:

• Deleting the whole list (we cannot use it later)


List – clearing (removing) all elements
List – joining lists
• 1. way:

• 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:

• Craeting a set with values:


Set – adding elements

• 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!

You might also like