CP125
Topic 2: Lists
Learning Outcomes:
(a) Identify concepts of lists
Table of Contents
CP125
my_list = [1, 2, 3, "apple", 4.5]
CP125
List Syntax
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
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
3. What type of data that can be stored in a list?
Any data type can be stored in a list
CP125
3. What type of data that can be stored in a list?
Any data type can be stored in a list
Code |
numbers = [10, 20, 30, 40, 50] print(numbers) # Output: [10, 20, 30, 40, 50] |
CP125
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
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
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
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
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
4. List index
CP125
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
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
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
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
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
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
Exercise
2. How many elements are in each list above?
3. What is the size of each list?
CP125