1 of 20

CP125

Topic 2: Lists

Learning Outcomes:

(a) Identify concepts of lists

2 of 20

Table of Contents

  1. What is a list?
  2. Comparison between list and primitive data type
  3. What data that can be stored in a list?
  4. List index

​

​

CP125

3 of 20

  1. What is a list?

​

  • A list is a collection of items stored in a single variable.
  • It allows storage of multiple data types (integers, strings, floats, etc.).

​

my_list = [1, 2, 3, "apple", 4.5]

​

​

​

​

​

CP125

4 of 20

  1. What is a list?

List Syntax

  • Lists are defined using square brackets [ ].
  • Items are separated by commas.

​

​

​

stringlist = ["apple", "banana", "kiwi"]

list name

list elements

List name = stringlist

Number of elements = 3

List size = 3

​

lists are created by using square brackets

CP125

5 of 20

2. Comparison between list and primitive data type

​

​

​

​

Feature

List

Primitive data type

Stores

multiple values

single value

Data type

can hold different data types

hold only 1 data type

Structure

use [ ] bracket

no bracket

Example

numbers = [1, 3, 5, 7]

random = [‘a’, 1, True]

number = 10

name = “Yusuf”

​

CP125

6 of 20

3. What type of data that can be stored in a list?

​

Any data type can be stored in a list

​

  1. Integers
  2. Floats
  3. Strings
  4. Booleans
  5. Mixed Data Type List
  6. List of Lists (Nested List)

​

​

​

​

​

CP125

7 of 20

3. What type of data that can be stored in a list?

​

Any data type can be stored in a list

​

  1. Integers

​

​

​

​

​

​

​

Code

numbers = [10, 20, 30, 40, 50]

print(numbers) # Output: [10, 20, 30, 40, 50]

CP125

8 of 20

3. What type of data that can be stored in a list?

​

Any data type can be stored in a list

​

B) Floats

​

​

​

​

​

​

​

Code

decimal_numbers = [1.5, 2.3, 3.7, 4.1]

print(decimal_numbers) # Output: [1.5, 2.3, 3.7, 4.1]

CP125

9 of 20

3. What type of data that can be stored in a list?

​

Any data type can be stored in a list

​

C) Strings

​

​

​

​

​

​

​

Code

fruits = ["apple", "banana", "cherry", "date"]

print(fruits) # Output: ['apple', 'banana', 'cherry', 'date']

CP125

10 of 20

3. What type of data that can be stored in a list?

​

Any data type can be stored in a list

​

D) Booleans

​

​

​

​

​

​

​

Code

status = [True, False, True, False]

print(status) # Output: [True, False, True, False]

CP125

11 of 20

3. What type of data that can be stored in a list?

​

Any data type can be stored in a list

​

E) Mixed Data Type List

​

​

​

​

​

​

​

Code

mixed_list = [25, "hello", 3.14, True]

print(mixed_list) # Output: [25, 'hello', 3.14, True]

CP125

12 of 20

3. What type of data that can be stored in a list?

​

Any data type can be stored in a list

​

F) List of Lists (Nested List)

​

​

​

​

​

​

​

Code

nested_list = [[1, 2, 3], ["a", "b", "c"], [True, False]]

print(nested_list)

# Output: [[1, 2, 3], ['a', 'b', 'c'], [True, False]]

CP125

13 of 20

4. List index

​

  • Refers to a position of an element in a list
  • Starts from the front, index 0 (positive index), read from left to right
  • Starts from the end to front, index -1 (negative index), read from right to left(Only in Python)

​

​

​

CP125

14 of 20

4. List index (Positive index)

Identify the index number corresponding to its element in a list

Example

fruitlist = ["apple", "banana", "kiwi", "durian", "guava", "orange"]

​

​

​

element

"apple"

"banana"

"kiwi"

"durian"

"guava"

"orange"

positive index (left to right)

0

1

2

3

4

5

CP125

15 of 20

4. List index (Positive index)

Identify the index number corresponding to its element in a list

​

Example:

cities = ["Kuala Lumpur", "Tokyo", "New York", "Paris", "Dubai"]

​

print(cities[0]) # Output: Kuala Lumpur

print(cities[2]) # Output: New York

print(cities[4]) # Output: Dubai

​

​

​

​

​

​

CP125

16 of 20

4. List index (Positive index)

Identify the index number corresponding to its element in a list

​

Example:

subjects = ["Math", "Science", "History", "English", "Computer Science"]

​

print(subjects[0]) # Output: Math

print(subjects[2]) # Output: History

print(subjects[4]) # Output: Computer Science

​

​

​

​

​

​

​

​

CP125

17 of 20

4. List index (Negative index)

Identify the index number corresponding to its element in a list

Example

fruitlist = ["apple", "banana", "kiwi", "durian", "guava", "orange"]

​

​

​

element

"apple"

"banana"

"kiwi"

"durian"

"guava"

"orange"

negative index (right to left)

-6

-5

-4

-3

-2

-1

CP125

18 of 20

4. List index (Negative index)

Identify the index number corresponding to its element in a list

​

Example:

cities = ["Kuala Lumpur", "Tokyo", "New York", "Paris", "Dubai"]

​

print(cities[-1]) # Output: Dubai (last element)

print(cities[-3]) # Output: New York (third last element)

​

​

​

​

​

​

​

​

CP125

19 of 20

4. List index (Negative index)

Identify the index number corresponding to its element in a list

​

Example:

subjects = ["Math", "Science", "History", "English", "Computer Science"]

​

print(subjects[-1]) # Output: Computer Science

print(subjects[-3]) # Output: History

print(subjects[-5]) # Output: Math

​

​

​

​

​

​

​

​

CP125

20 of 20

Exercise

  1. Identify the list name in the following examples:
    1. car = ["toyota", "honda", "masserati"]
    2. temperature = [1.5, 2.0, -1.0, 3.3, 1.5]
    3. variable = [‘x’, ‘y’, ‘z’, ‘a’, ‘b’, ‘c’, ‘i’ ,’jk’,23]

​

2. How many elements are in each list above?

3. What is the size of each list?

​

​

​

CP125