Lab2
This lab will give you practical implementation of different types of sequences including lists, tuples, sets and dictionaries. We will use lists alongside loops in order to know about indexing individual items of these containers. This lab will also allow students to write their own functions.
Activity Outcomes:
This lab teaches you the following topics:
- How to use lists, tuples, sets and dictionaries
- How to use loops with lists
- How to write customized functions
Introduction:
Python provides different types of data structures as sequences.
In a sequence, there are more than one values and each value has its own index. The first value will have an index 0 in python, the second value will have index 1 and so on. These indices are used to access a particular value in the sequence.
Python offers different types of sequences.
Lists are the most important type of sequence being used in Python. It is a collection of same or different type of objects. These objects are separated by commas to distinguish from each other enclosed in square brackets. The following activities show that how lists are used in Python.
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.
The data type “set”, which is a collection type, has been part of Python since version 2.4. A set contains an unordered collection of unique and immutable objects. The set data type is, as the name implies, a
Python implementation of the sets as they are known from mathematics. This explains, why sets unlike lists or tuples can’t have multiple occurrences of the same element.
A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values.
Lab Activities:
Activity 1:
Use loops to accept 5 values from user and store them in a list. Display all the values (objects) of the list.
Solution:

You will get the following output.

Activity 2:
Repeat the above code by accepting 5 integer values from user. Store these values in a list and display the sum of given values.
Solution:

You will get the following output.

Activity 3:
Accept 5 integer values from user. Store these values in a list and display the list in ascending order.
Solution:
Create a new Python file from Python Shell and type the following code.
Run the code by pressing F5.

You will get the following output.

Activity 4:
Accept two lists from user and display their join.
Solution:

You will get the following output.

Activity 5:
Write a Python code to accept a list from user and find a required element in it.
Solution:

You will get the following output.

If we run the program again and enter 55 to find in the list then the output will be as below.

Activity 6:
Write a function called say_hello that takes in a person’s name as a parameter. The function should print a greeting message with the person’s name. Then call the function three times with three different names.
Solution:

Activity 7:
A palindrome is a string which is same read forward or backwards. For example: “dad” is the same in forward or reverse direction. Another example is “aibohphobia” which literally means, an irritable fear of palindromes.
Write a function in python that receives a string and returns True if that string is a palindrome and False otherwise. Remember that difference between upper and lower case characters are ignored during this determination.
Solution:

Activity 8:
Imagine two matrices given in the form of 2D lists as under;
a = [[1, 0, 0], [0, 1, 0], [0, 0, 1] ]
b = [[1, 2, 3], [4, 5, 6], [7, 8, 9] ]
Write a python code that finds another matrix/2D list that is a product of and b, i.e.,
C=a*b
Solution:

Activity 9:
A closed polygon with N sides can be represented as a list of tuples of N connected coordinates,
i.e., [ (x1,y1), (x2,y2), (x3,y3), . . . , (xN,yN) ]. A sample polygon with 6 sides (N=6) is shown
below.

Write a python function that takes a list of N tuples as input and returns the perimeter of the polygon. Remember that your code should work for any value of N.
Hint: A perimeter is the sum of all sides of a polygon.
Solution:

Activity 10:
Imagine two sets A and B containing numbers. Without using built-in set functionalities, write your own function that receives two such sets and returns another set C which is a symmetric difference of the two input sets. (A symmetric difference between A and B will return a set C which contains only those items that appear in one of A or B. Any items that appear in both sets are not included in C). Now compare the output of your function with the following built-in functions/operators.
A.symmetric_difference(B)
B.symmetric_difference(A)
A ^ B
B ^ A
Solution:

Activity 11:
Create a Python program that contains a dictionary of names and phone numbers. Use a tuple of separate first and last name values for the key field. Initialize the dictionary with at least three names and numbers. Ask the user to search for a phone number by entering a first and last name. Display the matching number if found, or a message if not found.
Solution:

Home Activities:
Activity 1:
Create two lists based on the user values. Merge both the lists and display in sorted order.
Activity 2:
Repeat the above activity to find the smallest and largest element of the list. (Suppose all the elements are integer values)