Module 3.2.1
Module 3.2.1
Prof. Sarju S
24 October 2024
Module 3
Page 2
Module 3
► SELECTION AND ITERATION USING PYTHON:- if-else, elif, for loop, range, while loop.
► SEQUENCE DATA TYPES IN PYTHON - list, tuple, set, strings, dictionary, Creating and
using Arrays in Python (using Numpy library).
Page 3 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Python Lists
Page 4
A List is a Kind of Collection
► A list is an ordered set of data values. The values that make up a list are
called its elements or items.
Page 5 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
List Constants
► A list element can be any Python object >>> print([ 1, [5, 6], 7])
- even another list [1, [5, 6], 7]
>>> print([])
► A list can be empty []
Page 6 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
List Constants
>>> digits=list(range(10))
>>> print(digits)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> mul5=list(range(10,50,5))
>>> print(mul5)
[10, 15, 20, 25, 30, 35, 40, 45]
Page 7 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Looking Inside Lists
0 1 2
Page 8 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
We Already Use Lists!
for i in [5, 4, 3, 2, 1] : 5
print(i) 4
print('Blastoff!') 3
2
1
Blastoff!
Page 9 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
How Long is a List?
Page 10 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Lists and Definite Loops - Best Pals
Page 11 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
List operations
Page 12
Concatenating Lists Using +
Page 13 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Repeats a List Using *
Page 14 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Equality operator works well on lists
>>> composite=[4,6,8]
>>> print(even==composite)
False
Page 15 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Relational operators also work with lists
>>> prime=[2,3,5] ► Python starts by comparing the first element from each
>>> even=[2,4,6] list.
>>> print(prime>even)
► If they are equal, it goes on to the next element, and so
False
on, until it finds the first pair of elements that are
different and determines the relation between them.
► In this example, prime[0] == even[0].
► Next, prime[1] and even[1] are compared.
► Thus, the resulting relation is ‘<’ and prime > even is thus
False.
► Once the result is determined, the subsequent elements are
skipped.
Page 16 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Membership operators can be applied to a list as well
>>> even=[2,4,6,8]
>>> composite=[4,6,8]
>>> print(2 in even)
True
>>> print(2 in composite)
False
Page 17 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Lists Can Be Sliced Using :
>>> t[:4]
[9, 41, 12, 3] ► Remember: Just like in strings, the
second number is “up to but not
>>> t[3:] including”
[3, 74, 15]
>>> t[:]
[9, 41, 12, 3, 74, 15]
Page 18 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Lists Can Be Sliced Using :
last_three = names[-3:]
print(last_three)
Page 19 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Lists are Mutable
Page 20 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Slice operator and mutations
► In Python, the slice operator can be used not only for accessing portions of
sequences but also for modifying mutable sequences like lists.
Page 21 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Replacing Elements Using the Slice Operator
Page 22 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Replacing Elements Using the Slice Operator
Page 23 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Inserting Elements Using the Slice Operator
► You can insert new elements into a list at any position by assigning values to an empty slice
(start:stop with start == stop).
► numbers = [1, 2, 3, 4, 5]
► # Inserting at the beginning
► numbers[0:0] = [10]
► print(numbers) # Output: [10, 1, 2, 3, 4, 5]
Page 26
List Methods
Method Description
index() Returns the index of the first occurrence of the specified value.
Page 27 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
List Methods
Method Description Example
► Returns the index of the first occurrence of a
specified value. numbers = [10, 20, 30, 40, 50]
index() ► Syntax: list.index(value) print(numbers.index(30))
► Raises: ValueError if the specified value is not # Output: 2
found.
numbers = [10, 20, 30, 40, 50]
# Insert 25 at index 2
► Inserts an element at a specified position. numbers.insert(2, 25)
insert() print(numbers)
► Syntax: list.insert(index, value) # Output: [10, 20, 25, 30, 40,
50]
Page 28 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
List Methods
Method Description Example
numbers = [10, 20, 30]
# Adds 40 to the end of the
► Adds an element at the end of the list. list
append()
► Syntax: list.append(value) numbers.append(40)
print(numbers)
# Output: [10, 20, 30, 40]
numbers = [10, 20, 30]
# Adds all elements from the
► Extends the list by appending elements from second list
numbers.extend([40, 50, 60])
extend() another list (or any iterable).
print(numbers)
► Syntax: list.extend(iterable) # Output: [10, 20, 30, 40, 50,
60]
Page 29 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
List Methods
Method Description Example
► Removes the first occurrence of a specified numbers = [10, 20, 30, 40, 30]
value. # Removes the first occurrence of
30
remove() ► Syntax: list.remove(value)
numbers.remove(30)
► Raises: ValueError if the specified value is print(numbers)
not found. # Output: [10, 20, 40, 30]
► Returns the number of occurrences of a numbers = [10, 20, 30, 10, 20, 10]
print(numbers.count(10))
count() specified value.
# Output: 3
► Syntax: list.count(value)
Page 30 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
List Methods
Method Description Example
numbers = [10, 20, 30, 40, 50]
Page 31 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
List Methods
Method Description Example
numbers = [50, 10, 40, 30, 20]
# Sorts in ascending order
numbers.sort()
► Sorts the list in ascending order by default print(numbers)
(can be customized to sort in descending # Output: [10, 20, 30, 40, 50]
sort()
order).
► Syntax: list.sort(reverse=False) # Sort in descending order
numbers.sort(reverse=True)
print(numbers)
# Output: [50, 40, 30, 20, 10]
Page 32 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
List Methods
Method Description Example
numbers = [10, 20, 30, 40, 50]
► Reverses the elements of the list in place. # Reverses the list
reverse() numbers.reverse()
► Syntax: list.reverse() print(numbers)
# Output: [50, 40, 30, 20, 10]
Page 33 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Best Friends: Strings and Lists
s = "hello"
lst = list(s)
print(lst) # Output: ['h', 'e', 'l', 'l', 'o']
► The list() method converts an iterable (like a string) into a list of its
elements
► When used with a string, it breaks the string down into a list of individual
characters
Page 34 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Best Friends: Strings and Lists
Page 35 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Exercises - Create a List and Access Elements
► Objective: How to create lists, access elements using indexing, and modify list elements.
► Instructions:
► Create a list called fruits with the following elements: "apple", "banana", "cherry",
"date".
► Print the second element in the list.
► Modify the last element of the list to "dragonfruit".Print the modified list.
https://github.com/sarjus/Algorithemic-Thinking-with-Python-classroom-
exercises/blob/main/list_demo.py
Page 36 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Exercises - List Slicing
https://github.com/sarjus/Algorithemic-Thinking-with-Python-classroom-
exercises/blob/main/list_slicing.py
Page 37 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Exercises - List Methods (Append, Insert, Remove)
https://github.com/sarjus/Algorithemic-Thinking-with-Python-classroom-
exercises/blob/main/list_methods1.py
Page 38 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Exercises - List Methods (Append, Insert, Remove)
https://github.com/sarjus/Algorithemic-Thinking-with-Python-classroom-
exercises/blob/main/list_methods1.py
Page 39 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Exercises - Find the Largest Number in a List
► Objective: How to loop through a list and find the largest element.
► Instructions:
► Create a list of numbers: [12, 75, 34, 99, 45, 67].
https://github.com/sarjus/Algorithemic-Thinking-with-Python-classroom-
exercises/blob/main/list_find_largest_number.py
Page 40 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Exercises - Find the Largest Number in a List
► Objective: How to loop through a list and find the largest element.
► Instructions:
► Create a list of numbers: [12, 75, 34, 99, 45, 67].
https://github.com/sarjus/Algorithemic-Thinking-with-Python-classroom-
exercises/blob/main/list_find_largest_number.py
Page 41 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Exercises - Counting Occurrences in a List
https://github.com/sarjus/Algorithemic-Thinking-with-Python-classroom-
exercises/blob/main/list_count_method.py
Page 42 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Exercises - Reversing a List
► Objective: Use the reverse() method to reverse the order of elements in a list.
► Instructions:
► Create a list of numbers: [10, 20, 30, 40, 50].
https://github.com/sarjus/Algorithemic-Thinking-with-Python-classroom-
exercises/blob/main/list_reverse.py
Page 43 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Exercises - Remove Duplicates from a List
► Loop through the original list, and for each element, check if it is already in the new
list.
► If the element is not in the new list, add it.
https://github.com/sarjus/Algorithemic-Thinking-with-Python-classroom-
exercises/blob/main/list_remove_duplicate.py
Page 44 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Thank You
Prof. Sarju S
Department of Computer Science and Engineering
St. Joseph’s College of Engineering and Technology, Palai (Autonomous)
[email protected]
Page 45 Disclaimer - This document contains images/texts from various internet sources. Copyright belongs to the respective content creators.
Document is compiled exclusively for study purpose and shall not be used for commercial purpose.