[go: up one dir, main page]

0% found this document useful (0 votes)
15 views24 pages

Tuple

Uploaded by

Davey Kalang
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)
15 views24 pages

Tuple

Uploaded by

Davey Kalang
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/ 24

Python Tuples & Dictionaries

by Irwandi Hipiny, Fatin Hanani Kamaluddin


& Michelle George
Learning Outcomes:
After this lecture, students would be able to:

define and differentiate between tuples and


dictionaries.

create tuples and dictionaries, access their


elements, and apply basic operations.

utilize tuple and dictionary methods within


their structures.
Tuple
Definition and characteristics of tuples
Tuples in Python are immutable
sequences, typically used to store
collections of heterogeneous data.
They are ordered and indexed,
allowing for duplicate elements.
Tuples can contain any data type and
support nested tuples.
Due to their immutability, tuples
provide a safeguard for data integrity.
Creating and Accessing Tuples
Creating tuples: Use parentheses to define the elements of
a tuple.
Accessing tuples: Tuples are accessed using indexing or
slicing methods.
Try Me! Create a tuple
Try Me! Access element inside a tuple
Modifying Tuples
Addition
1 New elements can be added to a tuple by first converting it to
a list, add the element, then re-convert to tuple
Remove
2
Same steps as above, replace “add” with “remove”

Join
3 Tuples can be joined using the concatenation operator (+) to
create a new tuple.
Try Me! Add
Try Me! Remove
Try Me! Join
Tuple methods and operations
Tuple Methods Tuple Operations
Tuples have built-in methods Tuples support common
such as count() and index() for operations like concatenation,
easy data manipulation. repetition, and membership
These methods allow for testing.
efficient search and retrieval of
data within a tuple.

https://www.w3schools.com/python/python_tuples_methods.asp
Group Activity 1: Using count( ) and index( )
Methods with Tuples
Instructions:

1. Create a tuple named colors that contains several color values, with
some colors repeated. For example, use colors = ('red', 'blue', 'green',
'blue').
2. Use the count( ) method to find out how many times 'blue' appears in
the tuple.
3. Use the index( ) method to determine the position of 'green' in the
tuple.
4. Observe how these methods work to access and analyze data in
tuples.
Dictionary
Definition and characteristics of dictionaries
Python dictionaries are unordered collections of items
that store data in key-value pairs.
They are versatile and can store different types of data.
Dictionaries are commonly used for tasks such as
storing user information, configuration settings, and
more.
Definition and Characteristics of Dictionaries
Key-Value Pairs Unordered
Dictionaries are collections of key-
Dictionaries are unordered,
value pairs, where each key is
meaning that the elements do not
unique and is used to access its
have a specific order, unlike
corresponding value.
tuples.

Mutable Flexible Data Types


Dictionaries are mutable, which Dictionaries can store various data
means that the values associated types as values, including strings,
with keys can be changed or integers, lists, or even other
updated. dictionaries.
Try Me! Create a dictionary
Modifying Dictionaries

Editing Updating
Modify dictionaries by Update the values of
adding or removing existing keys with new
key-value pairs as information or data.
needed.
Try Me! Edit
Try Me! Update
Dictionary methods and operations
Dictionary Methods Dictionary Operations
Dictionaries have built-in Similar to Tuple, Dictionary
methods such as clear() and support common operations
copy() for easy dictionary like concatenation and
manipulation. repetition.

https://www.w3schools.com/python/python_dictionaries_methods.asp
Group Activity 2: Copying and Modifying a
Dictionary
Instructions:

1. Imagine now that you want to create a dictionary to store color codes for
each color in the colors tuple. Create a dictionary named color_codes with
color names as keys and example color codes as values, like {'red':
'#FF0000', 'blue': '#0000FF', 'green': '#008000'}.
2. Make a copy of color_codes using the copy( ) method and assign it to a new
variable, new_color_codes.
3. In new_color_codes, add a new key-value pair for another color, such as
'yellow': '#FFFF00'.
4. Print both dictionaries to confirm that new_color_codes has been
modified, but color_codes remains unchanged.
Q&A

You might also like