EXPERIMENT NO.
Aim / Title: To understand the Basic concept of Lists and Tuples in python
Problem Statement:
Lists and Tuples
Write program to
1) Find second smallest and second largest number in a list
2) Find intersection of two lists and two nested list
3) Find the maximum from a list of numbers.
4) Add an item in a tuple
5) Convert a tuple to a string
6) Find the repeated items of a tuple
Objectives: Performing operations on lists and tuple
Outcomes: Students will be able to learn to implement lists and tuple
Prerequisite: Basic knowledge of Python
Hardware requirements:
Processors: Intel Atom® processor or Intel® Core™ i3 processor.
Disk space: 1 GB
Software requirements:
Operating systems: Windows* 7 or later, macOS, and Linux.
Python versions: 2.7.X, 3.6.X. , Jupyter Notebook
Theory:
LIST in python:
Lists are used to store multiple items in a single variable.Lists are one of 4 built-in data types in Python used to
store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
Lists are just like dynamic sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Lists
need not be homogeneous always which makes it a most powerful tool in Python. A Single list may contain
DataTypes like Integers, Strings, as well as Objects. Lists are mutable, and hence, they can be altered even after
their creation.
Lists in Python are ordered and have a definite count. The elements in a list are indexed according to a definite
sequence and the indexing of a list is done with 0 being the first index. Each element in the list has its definite
place in the list, which allows duplication of elements in the list, with each element having its own distinct place
and credibility.
Example
Create a List:
thislist = ["apple", "banana", "cherry"]
print(thislist)
The list data type has some more methods. Here are all of the methods of list objects:
list.append(x)
Add an item to the end of the list. Equivalent to a[len(a):] = [x].
list.extend(iterable)
Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable.
list.insert(i, x)
Insert an item at a given position. The first argument is the index of the element before which to insert, so
a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
list.remove(x)
Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such
item.
list.pop([i])
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes
and returns the last item in the list. (The square brackets around the i in the method signature denote that
the parameter is optional, not that you should type square brackets at that position. You will see this
notation frequently in the Python Library Reference.)
list.clear()
Remove all items from the list. Equivalent to del a[:].
list.index(x[, start[, end]])
Return zero-based index in the list of the first item whose value is equal to x. Raises a ValueError if there
is no such item.
The optional arguments start and end are interpreted as in the slice notation and are used to limit the
search to a particular subsequence of the list. The returned index is computed relative to the beginning of
the full sequence rather than the start argument.
list.count(x)
Return the number of times x appears in the list.
list.sort(*, key=None, reverse=False)
Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their
explanation).
list.reverse()
Reverse the elements of the list in place.
list.copy()
Return a shallow copy of the list. Equivalent to a[:].
TUPLE in python:
A tuple in Python is similar to a list. The difference between the two is that we cannot change the elements of a
tuple once it is assigned whereas we can change the elements of a list.
Creating a Tuple
A tuple is created by placing all the items (elements) inside parentheses (), separated by commas. The
parentheses are optional, however, it is a good practice to use them.
A tuple can have any number of items and they may be of different types (integer, float, list, string, etc.).
Advantages of Tuple over List:
Since tuples are quite similar to lists, both of them are used in similar situations. However, there are certain
advantages of implementing a tuple over a list. Below listed are some of the main advantages:
We generally use tuples for heterogeneous (different) data types and lists for homogeneous (similar) data
types.
Since tuples are immutable, iterating through a tuple is faster than with a list. So there is a slight performance
boost.
Tuples that contain immutable elements can be used as a key for a dictionary. With lists, this is not possible.
If you have data that doesn't change, implementing it as tuple will guarantee that it remains write-
protected.
Program: //Paste Code and Output here : -
1) Find second smallest and second largest number in a list
Output :
2) Find intersection of two lists and two nested list
Output :
3) Find the maximum from a list of numbers.
Output :
4) Add an item in a tuple
Output :
5) Convert a tuple to a string
Output :
6) Find the repeated items of a tuple
Output ;
Sample Viva Questions and Answers:
1. How to create a List ?
Ans : Var = ["Sujeet", "Suraj", "Shivam","Aarif"]
print(Var)
2. How to know the size of a List ?
Ans : days = ["Mon","Tue","Wed"]
print (len(days))
3. How to add an element to a List ?
Ans : l1 = [1,2,"abc",4.7,True,False,0.5,8,"qwerty"]
print(l1)
l1.append("IIST")
print(l1)
4. How to create a Tuple ?
Ans : my_tuple = ("red", "blue", "green")
print(my_tuple)
5. How to delete a Tuple ?
Ans : tup = ('physics', 'chemistry', 1997, 2000);
print tup;
del tup;
print "After deleting tup : ";
print tup;
Here an exception raised, this is because after del tup
tuple does not exist any more.
Roll Name of Date of Date of Grade Sign of Sign of
No. Student Performance Evaluation Student Faculty
0818CS Sujeet Kumar 27/09/2022 11/10/2022
201175 Soni