0% found this document useful (0 votes)
279 views7 pages

Lab 06 Tuples Sets and Dictionary

This document provides an overview of tuples, sets, and dictionaries in Python. It describes how to create and manipulate each type of data structure, including adding and removing elements from tuples, sets, and dictionaries. It also discusses common operations like checking for subset/superset relationships between sets and looping through elements. The objectives are to learn how to create and use these data types to store and organize data. Exercises are provided to apply the concepts by creating and manipulating tuples, sets, and dictionaries.

Uploaded by

Dawit Gebre
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)
279 views7 pages

Lab 06 Tuples Sets and Dictionary

This document provides an overview of tuples, sets, and dictionaries in Python. It describes how to create and manipulate each type of data structure, including adding and removing elements from tuples, sets, and dictionaries. It also discusses common operations like checking for subset/superset relationships between sets and looping through elements. The objectives are to learn how to create and use these data types to store and organize data. Exercises are provided to apply the concepts by creating and manipulating tuples, sets, and dictionaries.

Uploaded by

Dawit Gebre
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/ 7

Addis Ababa Institute of Technology

Center of information technology and scientific computing


Fundamental of computer science and programing
Lab06 : Tuples , sets and Dictionary

Lab Objectives :
To create and use tuples as fixed list to prevent elements from being added , deleted or
replaced
To apply common sequence operation for tuples
To create and apply add and remove element in a set using add and remove methods
To apply common methods for sets
To test whether a set is subset or superset of another set using the issubset or issuperset
methods
To use sets to develop a program that counts the keywords in a Python source file
To create dictionary
To add, modify, and retrieve elements in a dictionary using the syntax
dictionaryName[key]
To apply common methods for dictionary
Tuples

Tuples are like lists, but their elements are fixed; that is, once a tuple is created, you cannot add new
elements, delete elements, replace elements, or reorder the elements in the tuple.

If the contents of a list in your application shouldnt change, you can use a tuple to prevent elements
from being added, deleted, or replaced accidentally.

You create a tuple by enclosing its elements inside a pair of parentheses. The elements are separated by
commas.

You can use the functions len, min, max, and sum on a tuple.

You can use a for loop to traverse all elements in a tuple

You can access the elements or slices of the elements using an index operator.
You can use the in and not in operators to determine whether an element is in a tuple

You can also compare the elements in tuples using the comparison operators

Sets
Sets are like lists in that you use them for storing a collection of elements. Unlike lists, however, the
elements in a set are nonduplicates and are not placed in any particular order.

Creating sets

You can create a set of elements by enclosing the elements inside a pair of curly braces ({}). The
elements are separated by commas. You can create an empty set, or you can create a set from a list or a
tuple,

A set can contain the elements of the same type or mixed types. For example, s {1, 2, 3, "one", "two",
"three"} is a set that contains numbers and strings. Each element in a set must be hashable , Each object
in Python has a hash value and an object is hashable if its hash value never changes during its lifetime.
Manipulating and Accessing Sets
You can add an element to a set or remove an element by using the add(e) or remove(e) method. You
can use the len, min, max, and sum functions on a set, and a for loop to traverse all elements in a set.
You can use the in or not in operator to determine whether an element is in the set

Subset and Superset

A set s1 is a subset of s2 if every element in s1 is also in s2. You can use the s1.issubset(s2) method to
determine whether s1 is a subset of s2.

A set s1 is a superset of set s2 if every element in s2 is also in s1. You can use the s1. issuperset(s2)
method to determine whether s1 is a superset of s2.

Comparison of sets and set operation are reading assignment


Dictionaries

A dictionary is a container object that stores a collection of key/value pairs. It enables fast retrieval,
deletion, and updating of the value by using the key.

A dictionary is a collection that stores the values along with the keys. The keys are like an index
operator. In a list, the indexes are integers. In a dictionary, the key must be a hashable object. A
dictionary cannot contain duplicate keys. Each key maps to one value. A key and its corresponding value
form an item (or entry) stored in a dictionary

Creating a Dictionary
You can create a dictionary by enclosing the items inside a pair of curly braces ({}). Each item consists of
a key, followed by a colon, followed by a value. The items are separated by commas.

Adding, Modifying, and Retrieving Values

To add an item to a dictionary, use the syntax:

dictionaryName[key] = value : If the key is already in the dictionary, the preceding statement replaces
the value for the key.

To retrieve a value, simply write an expression using dictionaryName[key]. If the key is in the dictionary,
the value for the key is returned. Otherwise, a KeyError exception is raised.
Deleting Items

To delete an item from a dictionary, use the syntax:

del dictionaryName[key]

The len Function


You can find the number of the items in a dictionary by using len(dictioinary).

Testing Whether a Key Is in a Dictionary


You can use the in or not in operator to determine whether a key is in the dictionary.

Looping Items
You can use a for loop to traverse all keys in the dictionary
Dictionary methods

Exercise

1. Create a tuple with four element and add the elements of tuples to list using while loop
2. Create a list with five element and change the list to tuple
3. Create a tuple of five element and print their sum by using while loop
4. create a tuple of six element and check weather an element exist in the tuple
5. create an HR system using dictionary with the following specification
a. create an empty dictionary
b. add elements to the dictionary as id and full name of the employee
c. your system must allow the HR employees to register more than one employee when
running your application one time
i. you can use loop with continue option for this time
6. create two sets with five elements
a. check if the first one is subset or superset of second set
b. display the interaction of the two sets
c. display the union of the two element

You might also like