Department of Computer Science & Engineering
Class 5th Sem. - B. Tech. (CSE)
Course
Course Programming in python BTCS 510-18
Code
Prepared by Er. Rajni Bedi
Lecture Topic List and Tuples
Course Outcome C510-18.3 Program Outcome PO1,PO2,PO3,PO4,PO12
Duration 1 hr Lecture 5 and 6 Unit I
Learning Level REMEMBER UNDERSTAND APPLY ANALYSE EVALUATE CREATE
(Tick whichever is applicable) √ √ √ √ √
1. Objectives
a. To Have understanding of basic data structures in python
2. Topic Learning Outcomes
After the completion of the class the students will able to
a. Create programs using basic data structures list and tuples.
b. Differentiate list and tuples
3. Teaching Methodology
Chalk & Talk, Visual Presentation
4. Applications:
List:
Used in JSON format
Useful for Array operations
Used in Databases
Tuple:
Used to insert records in the database through SQL query at a time
Ex: (1.’sravan’, 34).(2.’geek’, 35)
Used in parentheses checker
5. Evocation
Fig.1 Evocation diagram
6. Discussion
A list can be defined as a collection of values or items of different types. The items in the
list are separated with the comma (,) and enclosed with the square brackets [].
Characteristics of Lists
The lists are ordered.
The element of the list can be accessed by index.
The lists are mutable types.
How to Create and Assign Lists
A list can be define as below
L1 = ["John", 102, "USA"]
L2 = [1, 2, 3, 4, 5, 6]
How to Access Values in Lists
Slicing works similar to strings; use the square bracket slice operator ( [ ] ) along with the
index or indices.
How to Remove List Elements and Lists
To remove a list element, you can use either the del statement if you know exactly
which element(s) you are deleting or the remove() method if you do not know.
list=[1,'abc',6,7,8]
>>> del list[2]
>>> list // [1, 'abc', 7, 8]
>>> list.remove('abc')
>>> list // [1, 7, 8]
To remove an entire list, use the del statement:
del List
Pop()
List.pop()
Removes an element from a list.
This method differs from .remove() in two ways:
You specify the index of the item to remove, rather than the object itself.
The method returns a value: the item that was removed.
Updating List values
Lists are the most versatile data structures in Python since they are mutable, and their
values can be updated by using the slice and assignment operator.
list = [1, 2, 3, 4, 5, 6]
list[2] = 10
print(list) // [1, 2, 10, 4, 5, 6]
# Adding multiple-element
list[1:3] = [89, 78]
print(list) // [1, 89, 78, 4, 5, 6]
Built-in Functions
len(list): It is used to calculate the length of the list.
L1 = [1,2,3,4,5,6,7,8]
print(len(L1)) // 8
max(list): It returns the maximum element of the list
L1 = [12,34,26,48,72]
print(max(L1)) //72
min(list): It returns the minimum element of the list.
L1 = [12,34,26,48,72]
print(min(L1)) //12
Reversed():
for t in reversed(list):
print(t) // 23, 9,7,5,4,3,1
Reverse():
list.reverse()
list // [10, 9, 5, 4, 7, 2]
reverse() actually reverses the elements in the container. reversed() doesn't
actually reverse anything, it merely returns an object that can be used to iterate over the
container's elements in reverse order. If that's what you need, it's often faster than
actually reversing the elements.
Append:
list= [2,7,4,5,9]
list.append(10)
list // [2, 7, 4, 5, 9, 10]
TUPLE
Python Tuple is used to store the sequence of immutable Python objects. The tuple is
similar to lists since the value of the items stored in the list can be changed, whereas the
tuple is immutable, and the value of the items stored in the tuple cannot be changed.
Creating a tuple
A tuple can be written as the collection of comma-separated (,) values enclosed with the
small () brackets. The parentheses are optional but it is good practice to use. A tuple can
be defined as follows.
T1 = (101, "Peter", 22)
T2 = ("Apple", "Banana", "Orange")
T3 = 10,20,30,40,50
Creating a tuple with single element is slightly different. We will need to put comma after
the element to declare the tuple.
tup1 = ("JavaTpoint")
print(type(tup1)) // <class 'str'>
#Creating a tuple with single element
tup2 = ("JavaTpoint",)
print(type(tup2)) // <class 'tuple'>
How to Access Values in Tuples
Slicing works similarly to lists. Use the square bracket slice operator ( [ ] ) along with the
index or indices.
How to Update Tuples
Like numbers and strings, tuples are immutable, which means you cannot update them or
change values of tuple elements
Adding Elements in tuple
#tuples are immutable, so you can not add new elements
#using merge of tuples with the + operator you can add an element and it will create a
new tuple
tuplex = (4, 6, 2, 8, 3, 1)
tuplex = tuplex + (9,)
print(tuplex) // (4,6,2,8,3,1,9)
#adding items in a specific index
tuplex = tuplex[:5] + (15, 20, 25) + tuplex[5:]
print(tuplex) // (4, 6, 2, 8, 3,15,20,25, 1)
How to Remove Tuple Elements and Tuples
Removing individual tuple elements is not possible. There is, of course, nothing wrong
with putting together another tuple with the undesired elements discarded.
To explicitly remove an entire tuple, just use the del statement
Fig. 2 Mind Map
7. Reading Materials
“Core Python Programming.” Wesley J. Chun, Second Edition, Pearson
“Introduction to python programming”, Gowrishankar S. Veena A., CRC Press
9. Questions
Remember
1. Discuss the following list functions
a) len() b) sum() c) any() d) all() e) sorted()
Understanding
1. Why tuples are more memory efficient than list data strucrute?
Analyze
1. Differentiate List and tuples.
Apply
Check if the items in the list are sorted in ascending or descending order and print
suitable messages accordingly. Otherwise, print “Items in list are not sorted”
Given a tuple and a list as input, write a Python program to count the occurrences
of all items of the list in the tuple.
10. Key words
List
Tuples
mutable
11. Scope for Mini Project
Yes, In any software project, Students can use list or tuples to store data.