Class 10 - Data Structures - List-Modified
Class 10 - Data Structures - List-Modified
དཔལ་ལྡན་འབྲུག་གཞུང་།
ཤེས་རིག་དང་རིག་རྩལ་གོང་འཕེལ་ལྷན་ཁག།
Department of School Education
Ministry of Education & Skills Development
Python Coding
Insert text here
January 2024
Key Concept # 5
Concept Overview
Main concepts Sub - concepts
● Introduction to List - Characteristics & creating a list
● Accessing elements of List
○ Using index - positive and negative indexing
○ Using slicing operator
List ● Traversing the elements of List
● List Methods
○ append(), insert(), clear(), remove(), pop(),
index(), sort(), reverse(), min(), max(), count(),
sum()
Objectives
● Explain the importance of using data structure in programming.
● Create list, emphasizing syntax and the dynamic nature of list creation.
● Identify characteristics that distinguish lists from other data types.
● Access elements from a list using both negative and positive indexing.
● Extract sublists or segments from a list using the slicing operator.
● Traverse through elements of list using for and while loops.
ACTIVITIES
2. Understanding List
List
Data
Set Mutable Immutable Tuple
Structure
Dictionary
Organization
2
They keep your data well-organized and easy to understand.
Reusability
3 Many structures are commonly used across different programs,
saving time and effort.
Understanding List
● In Python, a list is a built-in data type that stores an ordered collection of
items/elements, that can be of any data type such as numbers, strings,
pictures, other lists, and more.
● Lists are defined using square brackets [ ] and the elements/items are
separated by commas.
● List elements/items are indexed, the first item has index[0], the second
item has index[1], and so on.
Code
1 fruits = [“apple”, “banana”, “mango”] #creating list
2 fruits[0] #accessing apple element
3 fruits[1]# accessing banana element
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X
Characteristics of List
Lists maintain the order of elements as they are
inserted. This order remains unchanged unless
1. Ordered
deliberately rearranged or modified (delete or add)
by the programmer.
Code
1 # Creating a list of stationery items for school
2 stationery_items = ["Pencils", "Pens", "Notebooks", "Rulers"]
3 # Displaying the list of stationery items
4 print(stationery_items)
5 # Check the data type
6 print(type(stationery_items))
Output
['Pencils', 'Pens', 'Notebooks', 'Rulers']
<class 'list'>
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X
Code
1 subject=["Dzongkha","English","Maths","ICT","Science"]
2 print(subject)
Output
['Dzongkha', 'English', 'Maths', 'ICT', 'Science']
Types of
accessing
element
of List B. Slice
A. Index Operator
(:)
1
Indexes value starts from -1 for the last element
and values decreases sequentially moving
backward from the end of the list.
Positive index 0 1 2
Negative index -3 -2 -1
Output
Second fruit (Positive Index): banana
Last fruit (Negative Index): kiwi
Second last fruit (Negative Index): grape
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X
Code
1 festivals = ["Losar", "Tshechu", "Kuchoe", "Thruebaab", "Dashain"]
2 print(" Positive Index):",festivals[1])
3 print("Negative Index):", festivals[-4])
Output
Positive Index: Tshechu
Negative Index: Tshechu
syntax
variable_name[start : end : step] It refers to an incremental value. The default
value is 1.
It indicates the index where slice has to Start. The default value is 0.
It indicates the index where slice has to End. The default value is length of the List.
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X
Code
1 numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2 sliced_list_basic = numbers[2:6] # Basic slicing
3 sliced_list_start = numbers[:5] # Slicing without start index
4 sliced_list_end = numbers[5:] # Slicing without end index
5 sliced_list_step = numbers[1:9:2] # Slicing with a step of 2
6 sliced_list_negative = numbers[-5:-2] # Slicing using negative indices
7 print("Basic Slicing (numbers[2:6]):", sliced_list_basic)
8 print("Slicing without start index (numbers[:5]):", sliced_list_start)
9 print("Slicing without end index (numbers[5:]):", sliced_list_end)
Output
Basic Slicing (numbers[2:6]): [2, 3, 4, 5]
Slicing without start index (numbers[:5]): [0, 1, 2, 3, 4]
Slicing without end index (numbers[5:]): [5, 6, 7, 8, 9]
Slicing with step of 2 (numbers[1:9:2]): [1, 3, 5, 7]
Slicing with negative indices (numbers[-5:-2]): [5, 6, 7]
Create a list name bhutan_places and initialize the list with names of six
Dzongkhags in Bhutan. Perform the following slicing operations.
1. Slice the list to extract elements from index 2 to 4.
2. Slice the list from index 2 till the end.
3. Perform slicing with a step of 3, extracting elements using every third
element.
4. Slice the list from the 5th element from the end up to the element just
before the last one.
Write a Python program using slicing operators to divide the above students
into two groups. Group 1 will consist of students at odd indices, while group 2
will include students at even indices.
Output
Group presentation:
student Group 1:['Tara', 'Sonam', 'Karma']
student Group 2:['Wangmo', 'Nima', 'Dorji']
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X
ACTIVITIES
● List items can be traversed or iterated using for and while loops, either
through elements directly or through indices.
syntax
while loop continues until the
while index < len(list_name): index reaches the length of
the list, printing each
print(list_name[index])
element in the process.
index += 1
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X
Code 2
1 #traversing through each item using index Output
2 num = [1, 2, 3, 4, 5]
1,2,3,4,5
3 for index in range(len(num)):
4 print(num[index],end=",")
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X
Code
1 temperatures = [22, 23, 24, 22, 20, 18, 19]
2 total_temp=0
3 for temp in temperatures:
4 total_temp = total_temp + temp
Output
5 avg = total_temp/7
6 print(f"Avg temp: {avg}") Avg temp: 38
Output
—------Menu—-------
Dish 1 : Ema Datshi
Dish 2 : Jasha Maroo
Dish 3 : Phaksha Paa
Code
1 print("—------Menu—-------")
2 dishes = ["Ema Datshi", "Jasha Maroo", "Phaksha Paa"]
3 for i in range(len(dishes)):
4 print("Dish",i+1,":",dishes[i])
Code
1 marks = [70,64,63,49,64,68,57,54,60,78,99]
2 total_marks=0
3 for x in range(len(marks)):
4 total_marks + = marks[x]
5 print("Sonam’s average mark:",total_marks/11)
Output
Sonam’s average mark: 66.0
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X
Output
Sum of even numbers: 38
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X
Code
1 ch = input("Enter a character to check a vowel or consonant:")
2 if ch in ["a","e","i","o","u"]:
3 print(f'{ch} is a vowel')
4 else:
5 print(f'{ch} is a consonant')
Output
Enter a character to check a vowel or consonant:a
a is a vowel
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X
Code
1 number = [1,2,3,4,5]
2 result = 1
3 for x in number:
4 result = result * x
5 print(f'The product of first 5 numbers is {result}')
Output
The product of first 5 numbers is 120
Code Output
1 student_code = [20231, 20232, 20233] 20231
2 i = 0 20232
3 while i < len(student_code): 20233
4 print(student_code[i])
5 i += 1
0 3 True 20231 1
1 3 True 20232 2
2 3 True 20233 3
Code
1 colours = ["Red", "Blue", "Green"]
2 index = 0 # Initialize index variable
3 while index < len(colours):
4 print(colours[index])
5 index += 1
Output
Red
Blue
Green
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X
ACTIVITIES
1. Understanding List Methods
2. Demo 1 - Practicing List Methods
3. Activity 1 - Separating Even and
LIST METHODS
Odd Number
4. Activity 2 - Managing Friend List
5. Activity 3 - Managing a Saving
Account
List Methods
Python has a set of built-in methods that can be used to manipulate items in
the list. Here are some of the common list methods in Python:
SL# Method Description Syntax
Insert an element at a
4 insert() specific position
list_name.insert(index,element)
List Methods
SL# Method Description Syntax
List Methods
SL# Method Description Syntax
Output
Mark List : [78, 50, 63, 40, 70]
Popped element: 63
index value of 5 is 2
sorted list [40, 50, 60, 70, 79]
reversed list [79, 70, 60, 50, 40]
minimum value in list 40
maximum value in list 79
Total marks scored:299
Output
Output
100 is saved for 4 times
In 8th Pema saved Nu.1000
Amount after removing Nu. 560 from list:
[50, 100, 150, 100, 500, 10, 100, 1000, 50, 100, 1500]
Highest amount Pema saved 1500
final amount saved is Nu.3660
Key Points
Key Points
● List items can be traversed or iterated using for and while loops, either through
elements directly or through indices.
● List methods like append(), insert(), clear(), remove(), pop(), sort(), reverse(),
min(), max(), count(), and sum() are used for manipulation and analysis of items
in the list.
● The benefits of using list is it allows you to store and manage multiple items of
data in a single variable, making it easy to organize, access, and manipulate
information efficiently.
● Lists allow for ordered collection with indexed access (unlike dictionaries and
sets), mutability for dynamic updates (unlike tuples), and can contain duplicate
elements (unlike sets), providing a versatile and efficient way to store and
manipulate data.
January 2024 © ICT Curriculum, MoESD
PYTHON Key stage IV Class X
བཀྲིན་ཆེ།
THANK YOU