0% found this document useful (0 votes)
6 views

CS22-OOP2-Module-6

This document outlines a lesson on lists in Object Oriented Programming, focusing on list creation, indexing, and common operations such as appending and slicing. It also introduces nested lists and provides preliminary activities, practice exercises, and references for further learning. Key concepts include list modifiers and the use of ranges in list operations.

Uploaded by

Xander Pardico
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

CS22-OOP2-Module-6

This document outlines a lesson on lists in Object Oriented Programming, focusing on list creation, indexing, and common operations such as appending and slicing. It also introduces nested lists and provides preliminary activities, practice exercises, and references for further learning. Key concepts include list modifiers and the use of ranges in list operations.

Uploaded by

Xander Pardico
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

CS22-OOP2(Object Oriented Programming 2)

College of Computer Studies

I. INTRODUCTION
In this lesson.

II. OBJECTIVES
1. Identify what is a List. Understand the basic
Module 6 syntax for creating lists. Learn how to access
elements in a list using indexing
2. Explore common operations on lists, such as
LIST appending, extending, and removing
elements. Understand the concept of list
slicing and its applications.
3. Learn how to create and work with nested
lists. Understand the concept of a 2D list and
its applications

III. PRELIMINARY ACTIVITIES


Recap on previous topic and identify its relation with the next lesson.

IV. LESSON PROPER


List Operations
 List syntax [value1, value2, ..., valuek]
 List indexing L[index], L[-index]
 List slices L[from:to], L[from:to:step] or L[slice(from,to,step)]
 Creating a copy of a list L[:] or L.copy()
 List concatenation (creates new list) X + Y
 List repetition (repeated concatenation with itself) 42 * L
 Length of list len(L)
 Check if element is in list e in L
 Index of first occurrence of element in list L.index(e)
 Number of occurrences of element in list L.count()
 Check if element is not in list e not in L
 sum(L) min(L) max(L)
List Modifiers (List are mutable)

Page 1 of 3
CS22-OOP2(Object Oriented Programming 2)
College of Computer Studies

 Extend list with elements (X is modified) X.extend(Y)


 Append an element to a list (L is modified) L.append(42)
 Replace sublist by another list (length can differ) X[i:j] = Y
 Delete elements from list del L[i:j:k]
 Remove & return element at position L.pop(i)
 Remove first occurrence of element L.remove(e)
 Reverse lists L.reverse()
 L *= 42
 L.insert(i,x) same as L[i:i]=x
Nested List (Multi-dimensional list)
Lists can contain lists as elements, that can contain lists as elements, that ...
Can e.g. be used to store multi-dimensional data (list lengths can be non-uniform)

Note: For dealing with matrices the numpy module is a better choice.

Initializing a 2-dimensional list

Page 2 of 3
CS22-OOP2(Object Oriented Programming 2)
College of Computer Studies

Range (From, to, Step)


o range(from, to, else) generates a sequence of numbers smaller than to
starting with from, and with increments of step:
 range(5) : 0, 1, 2, 3, 4
 range(3,8) : 3, 4, 5, 6, 7
 range(2,11,3) : 2, 5, 8
o Ranges are immutable, can be indexed like a list, sliced, and compared (i.e.
generate the same numbers)
o list(range(...)) generates the explicit list of numbers

V. PRACTICE EXERCISES/ACTIVITIES
VI. ADDITIONAL RESOURCES
VII. ASSESSMENT
VIII. REFERENCES
Allen B. Downey, ``Think Python: How to Think Like a Computer Scientist‘‘, 2nd edition, Updated
for Python 3, Shroff/O‘Reilly Publishers, 2016
https://www.stat.berkeley.edu/~spector/python.pdf

Page 3 of 3

You might also like