0% found this document useful (0 votes)
9 views

Lab 11

Uploaded by

Arsalan Ahmed
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)
9 views

Lab 11

Uploaded by

Arsalan Ahmed
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/ 5

Programming Fundamentals (CT-153) Practical Workbook

LAB # 11
OBJECTIVE
Implement looping through tuple and dictionary and how to access values in
dictionary and also implement adding and removing concept and new key value
pairs in dictionary.

THEORY
Tuples:
A tuple is a sequence of immutable Python objects. Tuples are sequences, just like
lists. The differences between tuples and lists are, the tuples cannot be changed
unlike lists and tuples use parentheses, whereas lists use square brackets.
Creating a tuple is as simple as putting different comma-separated values. Optionally
you can put these comma-separated values between parentheses also. For example

tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";
The empty tuple is written as two parentheses containing nothing −
tup1 = ();
To write a tuple containing a single value you have to include a comma, even though
there is only one value −
tup1 = (50,);

Example#01

✓ We define the tuple dimensions at line 1, using parentheses instead of square


brackets. At line 2, we print each element in the tuple individually, using the
same syntax we’ve been using to access elements in a list or sting:

Output:

1
Programming Fundamentals (CT-153) Practical Workbook

Example#02: Looping through all values in Tuple

Output:

Writing over TUPLES:


In this section, we can’t modify a tuple, you can assign a new value to a variable that
holds a tuple. So if we wanted to change our dimensions, we could redefine the
entire tuple:

Example#03:

The block at line 1, defines the original tuple and prints the initial dimensions. At line
5, we store a new tuple in the variable dimensions. We then print the new
dimensions at line 6. Python doesn’t raise any errors this time, because overwriting
a variable is valid:

Output:

Dictionary:
A dictionary in Python is a collection of key-value pairs. Each key is connected to a
value, and you can use a key to access the value associated with that key. A key’s
value can be a number, a string, a list, or even another dictionary. Python dictionary
is an unordered collection of items. While other compound data types have only

2
Programming Fundamentals (CT-153) Practical Workbook

value as an element, a dictionary has a key: value pair. Dictionaries can store an
almost limitless amount of information.

Creating a dictionary is as simple as placing items inside curly braces {} separated by


comma. An item has a key and the corresponding value expressed as a pair, key:
value.

dict = {'color': 'green', 'points': 5}

Example#04: Accessing values in Dictionary

Output:

Adding New Key-Value Pairs:


Dictionaries are dynamic structures, and you can add new key-value pairsto a
dictionary at any time. For example, to add a new key-value pair, you would give the
name of the dictionary followed by the new key in square brackets along with the
new value.

Example#05:

Output:

Removing Key-value pair:


When you no longer need a piece of information that’s stored in a dictionary, you
can use the del statement to completely remove a key-value pair. All ‘del’ needs is
the name of the dictionary and the key that you want to remove.

Example#06:

3
Programming Fundamentals (CT-153) Practical Workbook

Output:

Looping Through a Dictionary:


A single Python dictionary can contain just a few key-value pairs or millions of pairs.
Dictionaries can be used to store information in a variety of ways; therefore, several
different ways exist to loop through them. You can loop through all of a dictionary’s
key-value pairs, through its keys, or through its values.

Example#07:

✓ The method items () returns a list of dict's (key, value) tuple pairs. The syntax
of items () method is: dictionary.items ().
✓ The key-value pairs are not returned in the order in which they were stored,
even when looping through a dictionary. Python doesn’t care about the order
in which key-value pairs are stored; it tracks only the connections between
individual keys and their values.

Output:

4
Programming Fundamentals (CT-153) Practical Workbook

Lab Exercise:
1. Write a python program to find the minimum and maximum value (in
numbers) within TUPLE.

2. Write a python program to count the number of occurrence of each letter in


word "MISSISSIPPI" (or take input from user). Store count of letters in a
DICTIONARY.

You might also like