Lecture 4
Lecture 4
1
4/12/2023
Programming
Algorithm
- A set of rules or steps used to solve a problem
Data Structure
- A particular way of organizing data in a computer
$ python
>>> x = 2
>>> x = 4
>>> print(x)
4
2
4/12/2023
3
4/12/2023
CONTENT
Concept of a collection Slicing lists
Lists and definite loops List methods: append, remove
Indexing and lookup Sorting lists
List mutability Splitting strings into lists of words
Functions: len, min, max, sum Using split to parse strings
LEARNING OUTCOMES
4
4/12/2023
INTRODUCTION
INTRODUCTION
10
5
4/12/2023
INTRODUCTION
11
CREATING LIST:
12
6
4/12/2023
LISTS EXAMPLE
List containing string type data
13
LISTS EXAMPLE
A list containing another list known as nested list.
14
7
4/12/2023
LISTS EXAMPLE
15
REPRESENTATION OF LIST OR
STATE DIAGRAM OF LIST
16
8
4/12/2023
Forward Indexing 0 45 -4
1 56 -3
2 67 -2
3 20 -1 Backward Indexing
LIST INDEX
LIST elements
17
Forward Indexing
0 Mouse -3
1 Pendrive -2
LIST INDEX
LIST elements
18
9
4/12/2023
19
List index works the same way as String index, which is:
An integer value/expression can be used as index.
An Index Error appears, if you try and access element that
does not exist in the list.
An index can have a negative value, in that case counting
happens from the end of the list.
20
10
4/12/2023
LIST - Example
21
LIST - Example
22
11
4/12/2023
23
24
12
4/12/2023
25
26
13
4/12/2023
Creating a list
and mapping to
other lists.
27
LIST CONCATENATION
28
14
4/12/2023
LIST SLICING
Like String slicing, Lists can also be sliced, as you have seen
in previous slides.
29
30
15
4/12/2023
append () method is used to add elements to the end of list. Every time
new element is assigned using append it will be added at the rear side
of list
Syntax:
Listobject.append(element)
Example
L1.append(587) OR
element=int(input())
L1.append(element)
31
Elements
read during
run time
32
16
4/12/2023
OUTPUT
33
34
17
4/12/2023
TRAVERSING LIST
35
TRAVERSING LIST
36
18
4/12/2023
TRAVERSING LIST
37
TRAVERSING LIST
OUT PUT
38
19
4/12/2023
39
1. pop ( ) Method
2. del Method
3. remove ( ) Method
40
20
4/12/2023
1. pop ( ) Method
List.pop ([index])
41
1. pop ( ) Method
42
21
4/12/2023
2. del Method
Syntax :
del Listobject[index]
43
3. remove ( ) Method
In case, we know the element to be deleted not the index, of the
element, then remove () can be used.
Syntax is:
listobject.remove(value)
44
22
4/12/2023
45
46
23
4/12/2023
OTHER METHODS
1. insert () Method
This method allows us to insert
an element, at the given position
specified by its index, and the
remaining elements are shifted to
accommodate the new element.
insert () requires two arguments-
index value and item value.
Its syntax is:
list. insert (index, item)
47
OTHER METHODS
2. reverse ( ) Method
This method can be used to
reverse the elements of the list
in place
Its syntax is:
list.reverse ( )
Method does not return
anything as the reversed list is
stored in the same variable.
48
24
4/12/2023
OTHER METHODS
3. sort ( ) Method
For arranging elements in an
order, Python provides a
method sort ( ) and a function
sorted ( ). sort ( ) modifies the
list in place and sorted()
returns a new sorted list.
Default Ascending
Order sorting
49
OTHER METHODS
For Example:
Descending order
50
25
4/12/2023
OTHER METHODS
51
OTHER METHODS
4. clear ( ) Method
52
26
4/12/2023
OTHER METHODS
5. index ( ) Method
53
OTHER METHODS
6. extend ( ) Method
54
27
4/12/2023
PROGRAMS ON LISTS
55
PROGRAMS ON LISTS
At easy level
1.Write a Python program to sum all the items in a list.
def sum_list(items):
sum_numbers = 0
for x in items:
sum_numbers += x
return sum_numbers
print(sum_list([1,2,-8]))
56
28
4/12/2023
PROGRAMS ON LISTS
At easy level
2. Write a Python program to get the
largest number from a list.
57
58
29
4/12/2023
PROGRAMS ON LISTS
def check_list():
l = []
if not l:
print("List is empty")
print(check_list())
59
60
30
4/12/2023
PROGRAMS ON LISTS
def clone_list():
original_list = [10, 22, 44, 23, 4]
new_list = list(original_list)
print(original_list)
print(new_list)
61
AT AVERAGE LEVEL
5. Write a Python function that takes two lists and returns True if they have at
least one common member.
62
31
4/12/2023
5. Write a Python function that takes two lists and returns True if they have
at least one common member.
63
6. Write a Python program to generate and print a list of first and last 5
elements where the values are square of numbers between 1 and 30 (both
included).
def printValues():
l = list()
for i in range(1,21):
l.append(i**2)
print(l[:5])
print(l[-5:])
printValues()
64
32
4/12/2023
7 Write a Python program to generate and print a list except for the first 5
elements, where the values are square of numbers between 1 and 30 (both
included).
def printValues():
l = list()
for i in range(1,21):
l.append(i**2)
print(l[5:])
printValues()
65
def num_prn():
num = [7,8, 120, 25, 44, 20, 27]
num = [x for x in num if x%2!=0]
print(num)
num_prn()
66
33
4/12/2023
def prn_list():
color = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow’]
color = [x for (i,x) in enumerate(color) if i not in (0,4,5)]
print(color)
prn_list()
67
68
34
4/12/2023
69
35