0% found this document useful (0 votes)
1 views43 pages

Week 6 - Array and List

This document serves as a lab module for understanding arrays and lists in Python, detailing their characteristics, initialization, and various methods for manipulation. It includes practical exercises for students based on their student ID parity, focusing on creating, modifying, and accessing list items. Additionally, it provides guidance on submitting work and references for further learning.

Uploaded by

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

Week 6 - Array and List

This document serves as a lab module for understanding arrays and lists in Python, detailing their characteristics, initialization, and various methods for manipulation. It includes practical exercises for students based on their student ID parity, focusing on creating, modifying, and accessing list items. Additionally, it provides guidance on submitting work and references for further learning.

Uploaded by

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

LAB MODULE

Array/List in Python
Introduction to List
Outline Initializing a List
List Method
Practice I
Practice II
Practice III
Submission
Introduction to Lists
What is a List? A List is a built-in data type 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.
Why list and not array?
A list and an array have several similarities, the major difference is the data type that can be stored
inside. A list can contain different data types, meanwhile an array must contain the same data type. Lists
are more popular, on the other side arrays are optimized for arithmetic operations. The downside of
using arrays is Python doesn’t include them out of the box, you should import them from a library.
Imagining a list
Imagine a list as a train that has many carriages. Each carriage holds a certain data that can be accessed
easily. Each carriage has an index that is used as the access key. The index of a list starts at 0, so the first
carriage has the index 0. Take a look at the illustration below. It is a list of car brands.

0 1 2 n
...
“Toyota” “BMW” “Volvo” data
Initializing a list
How to initialize a list?
Using the list example from the previous slide, a list can be initialized with its items as shown below.

Code Output
List methods
List methods
Below are list methods that can be used to manipulate lists in python:
index() - Returns the index of the first element with the specified value
append()- Add an element to the end of the list
insert() - Adds an element at the specified position
pop() - Removes the element at the specified position
remove() - Removes the first item with the specified value
clear() - Removes all items from the list
copy() - Returns a copy of the list
count() - Returns the number of elements with the specified value
extend() - Add the elements of a list (or any iterable), to the end of the current list
reverse() - Reverses the order of the list
sort() - Sort the list
Accessing a list item
index()
How to access a list item?
Using the list example from the previous slide, a list can be accessed as shown below. This code will
print out the data that has index 1 from the cars list, which is the second item on the list.

Code Output
How to access a list item?
You can also determine the position/index of a specific item in a list such as below.

Code Output
Practice I
Desired Output
For students who have odd student ID, create a list of 10 beverage brands from around the world and
print out the data using the index that is the last digit of your student ID.

For students who have even student ID, create a list of 10 food brands from around the world and print
out the data using the index that is the last digit of your student ID.

For example, the last digit of my student ID is 7, thus I need to print the data with index 7 from the list.
Adding new list items
append() & insert()
How to add new items to a list?
Utilizing the built-in method of append() and insert(), one can add more items into a list. See the
example below.
Code Output

Append() adds a new item at the end of the list,


meanwhile insert() adds a new item depending on the
desired index.
Deleting list items
pop(), remove(), clear()
How to delete items from a list?
Utilizing the built-in method of pop() and remove(), one can delete items from a list. See the
example below.
Code Output

Pop() if not specified an index, will delete the last


item on a list. Meanwhile, remove() will delete
the specified item.
How to delete items from a list?
You can also delete the entire items that exist in a list using clear()

Code Output
Practice II
Desired Output
Initialize a list with these items 'Toyota', 'BMW', 'Volvo', 'Volkswagen', 'Fiat'. Manipulate the list using
adding and deleting items methods such that the output matches the desired output.

For students who have odd student ID, try and match this output.

For students who have even student ID, try and match this output.
Copying list items
copy()
How to copy items from a list?
We can use the copy() method to return a copy of a specified list

Code

Output
Counting the amount of items in a list
count()
How to count the number of items in a list?
The count() method returns the number of elements with the specified value.

Code Output
Combining the items of two lists
extend()
How to combine the items of two lists?
The extend() method adds the specified list elements (or any iterable) to the end of the current list.

Code

Output
Reversing the list order
reverse()
How to reverse the items order in a list?
The reverse() method reverses the sorting order of the items in a list.

Code Output
Sorting the list order
sort()
How to sort the items order in a list?
The sort() method sorts the list ascending by default (alphabetically).

Code Output
Editing and slicing lists
How to edit and slice a list?
To edit an item inside a list, items can be accessed using their indexes and apply changes directly. To apply
more than one changes at once, we can use slicing to make it easier.
Code

Slicing handles several indexes at once and the notation for


it can be understood as follows.

a[start:stop]# items start through stop-1


Output
a[start:]# items start through the rest of the array
a[:stop]# items from the beginning through stop-1
a[:]# a copy of the whole array
Practice III
Desired Output
For students who have odd student ID, initialize a list with these items [0, 1, 4, 6, 7, 9, 11, 14, 16, 17] and
match this output using slicing.

For students who have even student ID, initialize a list with these items [0, 3, 4, 6, 9, 11, 13, 14, 16, 19] and
match this output using slicing.
Additional Practice
(Optional)
Please create a program that will check whether the item is already on the list or not based on your
input user, if so, then determine the price

make 2 lists for example Grocery_list and Price_list. Include at least 5 items in this list.

Input: Input your item here: Milk


Output: This item is exist in these list and the price is 20000

You may use conditional statement but make sure to work with list
Expected Result
Submission
Screenshot & Upload to eCampus
Screenshot your source codes and terminals and put them into a pdf file. After that upload them to
eCampus on Practice Week 11.

For practice I, practice II, practice III please screenshot your source code and terminal.
Questions?
References
Python Lists - w3schools

Python List - programiz

Python Lists - tutorialspoint

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy