1 of 45

དཔལ་ལྡན་འབྲུག་གཞུང་། ཤེས་རིག་དང་རིག་རྩལ་གོང་འཕེལ་ལྷན་ཁག།

Department of School Education

Ministry of Education & Skills Development

Python Coding

January 2024

Class X ICT Curriculum

January 2024

PYTHON

Key stage IV

Class X

2 of 45

DATA STRUCTURES - TUPLE

Key Concept # 8

January 2024

PYTHON

Key stage IV

Class X

3 of 45

Main concepts

Sub - concepts

Tuple

  • Introduction to Tuple
  • Definition, characteristics and creating a Tuple
  • Accessing elements of tuple
  • Using index - positive and negative indexing
  • Using slicing operator
  • Traversing the elements of tuple
  • Tuple methods
    • count(), index(), min(), max(), len()

Concept Overview

January 2024

PYTHON

Key stage IV

Class X

4 of 45

  • Define tuples in the context of data structures.
  • Describe the characteristics of tuples, including immutability and heterogeneous data storage.
  • Demonstrate the use of tuple methods for manipulating data within tuples.
  • Analyze numeric data using tuple methods, such as aggregation, sorting, and filtering.
  • Apply tuple concepts to solve practical problems, illustrating their relevance in real-life situations.

Objectives

January 2024

PYTHON

Key stage IV

Class X

5 of 45

ACTIVITIES

  1. Understanding Tuple Data Structure
  2. Demo - Student Information
  3. Activity 1 - Temperature in Celsius and Fahrenheit
  4. Activity 2 - Displaying Book Information
  5. Activity 3 - Check Your Understanding

TUPLE

January 2024

PYTHON

Key stage IV

Class X

6 of 45

Understanding Tuple Data Structure

  • A Python tuple is an ordered, immutable collection of elements/items.
  • Tuple is defined using parentheses, ().
  • Tuples are commonly used to group related data and provide an efficient way to store and access multiple values within a single variable.

tuple_name = (element1, element2,element3,…)

marks = (30,45.5,50,50.5,’a’)

syntax:

Example

January 2024

PYTHON

Key stage IV

Class X

7 of 45

Characteristics of Tuple

1

Order

Allow duplicate values

Heterogeneous data types

Immutable

Tuple maintains the order of items; the first item put in will be the first one out.

2

Since tuple is indexed, it can have items with the same value.

Tuple can hold different data types (eg. integer, float,string and even other tuples) within same tuple.

Tuple is unchangeable; items cannot be changed, added or removed after the tuple has been created.

3

4

January 2024

PYTHON

Key stage IV

Class X

8 of 45

Demo - Student Information

Create a Python tuple named student representing student information with the following attributes: name, age, and grade. Assign the values "Alice" for name, 17 for age, and 11 for grade. Print the tuple.

1

2

3

4

# Create a tuple named student

student = ("Alice", 17, 11)

# Print the tuple

print(student)

Code

('Alice', 17, 11)

Output

January 2024

PYTHON

Key stage IV

Class X

9 of 45

Activity 1 - Temperature in Celsius and Fahrenheit

Write a Python program to create a tuple named temp containing temperature values in Celsius and Fahrenheit. Assign the values (25, 77) to represent 25 degrees Celsius and its corresponding Fahrenheit equivalent. Print the tuple.

1

2

3

4

# Create a tuple named temp

temp = (25, 77)

# Print the tuple

print(temp)

Code

(25, 77)

Output

January 2024

PYTHON

Key stage IV

Class X

10 of 45

Activity 2 - Displaying Book Information

Create a Python program to create a tuple named book_info to store information about english novel ‘The Giver’. Include attributes such as title, author, and year of publication. Assign appropriate values and print the tuple.

1

2

3

4

# Define a tuple named book_info

book_info = ("The Giver", "Lois Lowry", 1993)

# Print the tuple

print(book_info)

Code

(‘The Giver’, ‘Lois Lowry’, 1993)

Output

January 2024

PYTHON

Key stage IV

Class X

11 of 45

Activity 3 - Check Your Understanding

  1. Which symbol is used to define a tuple in Python?
    1. { }
    2. [ ]
    3. ( )
    4. < >
  1. Which of the following is true about tuples?
    1. Tuples are mutable
    2. Tuples are indexed
    3. Tuples can only store elements of the same data type
    4. Tuples can be resized dynamically

January 2024

PYTHON

Key stage IV

Class X

12 of 45

Activity 3 - Check Your Understanding

tuple_name = (element1, element2,element3,…)

  1. Tuples are mutable data structures in Python.
  2. Tuples can contain elements of different data types.

  1. Give an example of a tuple containing heterogeneous elements.

ans: student_info = ("Alice", 15, "ICT", 67)

  1. Provide the syntax to define a tuple in Python program.

False

True

syntax:

January 2024

PYTHON

Key stage IV

Class X

13 of 45

ACTIVITIES

  1. Demo 1 - Displaying Even Number from Tuple
  2. Activity 1 - Displaying Employee Name from Tuple
  3. Demo 2 - Displaying Student Marks from Tuple
  4. Activity 2 - Displaying First Quarter Month of Year
  5. Activity 3 - Displaying Last Vowels
  6. Activity 4 - Displaying Age between 20 and 30

ACCESSING ITEMS OF THE TUPLE

January 2024

PYTHON

Key stage IV

Class X

14 of 45

Accessing Items of the Tuple

  • Accessing elements of a tuple in Python involves retrieving specific items stored within the tuple by their index positions.

Types of accessing element of Tuple

Index

1

Slice Operator (:)

2

January 2024

PYTHON

Key stage IV

Class X

15 of 45

1. Accessing Element using Index

  • An index is used to refer to particular elements of the tuple.
  • Tuple supports both positive and negative indexes.

tuple_name=(element1,element2,element3,...)

tuple_name[index_no]

syntax

Indexes value starts from 0 for the first element and value increases sequentially for each subsequent element in the tuple.

Indexes value starts from -1 for the last element and values decreases sequentially moving backward from the end of the Tuple.

Positive indexes

Negative indexes

January 2024

PYTHON

Key stage IV

Class X

16 of 45

Example

Positive index

0

1

2

school

Motithang HSS

Bajo HSS

Babesa MSS

Negative index

-3

-2

-1

Positive Indexing

Negative Indexing

Code

1

school=(“Motithang HSS”, ”Bajo HSS”, “Babesa MSS”)

school[0]

school[1]

school[2]

Motithang HSS

Bajo HSS

Babesa HSS

Motithang HSS

school[-1]

school[-2]

school[-3]

Babesa HSS

Bajo HSS

January 2024

PYTHON

Key stage IV

Class X

17 of 45

Demo 1 - Displaying Even Number from Tuple

Write a Python program using the tuple given below to display even number from the tuple using positive and negative indexing.

even_num= (100, 102, 104, 106, 108, 110, 112, 118)

    • Access specific elements such as 100 and 118 using the indexing concept.

#displaying even number at index 0 and 7

print(even_num[0])

print(even_num[7])

January 2024

PYTHON

Key stage IV

Class X

18 of 45

Cont

even_num= (100, 102, 104, 106, 108, 110, 112, 118)

b. Use a for loop to display all elements from the tuple.

#Traversing through Tuple for loop

for x in range(8):

print(even_num[x],end=",")

c. Implement a while loop to display elements until reaching 112.

#Traversing through Tuple using while loop until element 112

print()

x = 0

while even_num[x]!=112:

print(even_num[x],end=",")

x+=1

100

118

100,102,104,106,108,110,112,118,

100,102,104,106,108,110,

Output

January 2024

PYTHON

Key stage IV

Class X

19 of 45

Activity 1 - Displaying Employee Name from Tuple

Write a Python program to create a tuple named emp_name containing employee name: Pema, Subba, Karma, Tawjay, Karsel, and Timsina. Perform the following questions.

    • Access specific elements, Tawjay and Timsina using the negative indexing concept.
    • Use a for loop to display all elements from the tuple.
    • Use a while loop to display elements starting from karma to Timsina.

January 2024

PYTHON

Key stage IV

Class X

20 of 45

Activity 1 - Solution

1

2

3

4

5

6

7

8

9

10

11

12

13

emp_name=("Pema","Subb","karma","Tawjay","Karsel","Timsina")

#Displaying Tawjay and Timsina using -ve indexing

print(emp_name[-3])

print(emp_name[-1])

#Traversing through Tuple for loop

for x in range(len(emp_name)):

print(emp_name[x],end=",")

print()

#Traversing through Tuple Karma till Timsina

x=2

while x < len(emp_name):

print(emp_name[x],end=",")

x +=1

Code

Tawjay

Timsina

Pema,Subb,karma,Tawjay,Karsel,Timsina

karma,Tawjay,Karsel,Timsina,

Output

January 2024

PYTHON

Key stage IV

Class X

21 of 45

2. Accessing Element using Slice Operator

tuple_name[start : end : Step]

Slicing in tuple is used to access a range of elements in it.

syntax

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.

it refers to an incremental value. The default value is 1.

January 2024

PYTHON

Key stage IV

Class X

22 of 45

Example

1

2

odd_num=(1,3,5,7,9,11,13,15,17,19,21,23,25,27,29)

print(odd_num[1:13:2])

Code

(3, 7, 11, 15, 19, 23)

Output

Output will be starting from the element at index 1,up to, but not including, the element at index 13. It will select every second element within this range.

January 2024

PYTHON

Key stage IV

Class X

23 of 45

Demo 2 - Displaying Student Marks from Tuple

Create a Python program that accomplishes the following tasks:

  1. Create a tuple, named it as marks and store following marks in the tuple:

40, 45, 34, 56, 57, 78, 23, 48, 67, 90, 91, 88 and 99

#Define the tuple 'marks' with the given marks

marks = (40,45,34,56,57,78,23,48,67,90,91,88,99)

b. Access following marks from the tuple: 45, 34, 56, 57 and 78 using slice operator.

#Access marks 45, 34, 56, 57, and 78 using slice operator

selected_marks = marks[1:6]

print("Selected marks:\n", selected_marks)

January 2024

PYTHON

Key stage IV

Class X

24 of 45

Cont.

c. Used negative index to access all the marks from tuple, except elements of index 1 and 2.

#Use negative index to access all marks except elements #at index 1 and 2

all_except_1_and_2 = marks[-11:]

print("All marks except elements at index 1 and 2:\n", all_except_1_and_2)

d. Access all the elements of even index from the tuple using slice operator.

# Access all elements of even index using slice operator

even_index_marks = marks[::2]

print("Marks at even indices:\n", even_index_marks)

January 2024

PYTHON

Key stage IV

Class X

25 of 45

Demo 2 - Solution

Selected marks:

(45, 34, 56, 57, 78)

All marks except elements at index 1 and 2:

(34, 56, 57, 78, 23, 48, 67, 90, 91, 88, 99)

Marks at even indices:

(40, 34, 57, 23, 67, 91, 99)

Output

January 2024

PYTHON

Key stage IV

Class X

26 of 45

Activity 2 - Displaying First Quarter Month of Year

Write a Python program to create tuple named months containing the names of all the months. Using slice operators, print the first quarter of the year.

Tell the answer……………………………….

January 2024

PYTHON

Key stage IV

Class X

27 of 45

Activity 3 - Displaying Last Vowels

Write a Python Program to create a tuple named vowels containing the vowels of the english alphabet. Access and print the last vowel in the tuple.

Answer ????????????????????

January 2024

PYTHON

Key stage IV

Class X

28 of 45

Activity 4 - Displaying Age between 20 and 30

Write a Python Program to print the ages of people who are between 20 and 30 years old using slice operators using the tuple given below:

ages = (25, 30, 18, 22, 35, 28, 20, 31, 29, 27)

Home activity …………………

January 2024

PYTHON

Key stage IV

Class X

29 of 45

ACTIVITIES

  1. Understanding Tuple Methods
  2. Demo 1 - Analyzing the Student Data
  3. Activity 1 - English Mark Analysis
  4. Activity 2 - Calculating Total of Expenditure
  5. Activity 3 - Average of the First 5 Natural Numbers
  6. Activity 4 - Calculating Average Age of Female Classmates
  7. Activity 5 - Fetching Location of Number
  8. Activity 7 - Login system

TUPLE METHODS

January 2024

PYTHON

Key stage IV

Class X

30 of 45

Tuple Methods

SL#

Method

Description

Syntax

1

2

3

count()

index()

len()

Returns the number of times a specified value occurs in a tuple.

Searches the tuple for a specified value and returns the position of where it was found.

used to determine the length or the number of elements in a tuple

tuple_name.count(element)

tuple_name.index(element)

len(tuple_name)

Python has a set of built-in methods that can be used to manipulate items in the tuple. Here are some of the common tuple methods in Python:

January 2024

PYTHON

Key stage IV

Class X

31 of 45

Set Methods(cont.)

SL#

Method

Description

Syntax

4

5

min()

max()

used to find the minimum value occurs in a tuple

used to find the maximum value occurs in a tuple.

min(tuple_name)

max(tuple_name)

sum()

Return the sum of all elements in the tuple

result = sum(tuple_name)

6

January 2024

PYTHON

Key stage IV

Class X

32 of 45

Demo 1 - Analyzing the Student Data

Write a Python program that analyzes student data. Given two tuples:

names = ("Dawa", "Pema", "Dorji", "Pema")

marks = (45, 56, 78, 73)

Your program should perform the following tasks:

  1. Calculate and display the average mark of the students.

# Task 1: Calculate and display the average mark

average_mark = sum(marks) / len(marks)

print("Average mark:", average_mark)

2. Determine and print the highest mark achieved among the students.

# Task 2: Determine and print the highest mark achieved

highest_mark = max(marks)

print("Highest mark:", highest_mark)

January 2024

PYTHON

Key stage IV

Class X

33 of 45

Cont.

3. Determine and display the lowest mark among the students.

# Task 4: Determine and display the lowest mark

lowest_mark = min(marks)

print("Lowest mark:", lowest_mark)

Average mark: 63.0

Highest mark: 78

Lowest mark: 45

Output

January 2024

PYTHON

Key stage IV

Class X

34 of 45

Activity 1 - English Mark Analysis

Chimi Yoezer Wangmo has been tasked with presenting the result analysis of english marks of 2024 midterm examination. The dataset of english marks is as follows:

english_marks=(23,45,34,56,89,67,89,45,56,78,90)

Using Python tuple methods, perform the following analyses:

  1. Determine the total number of students who scored 45 marks in the English subject.
  2. Find the total number of students who appeared in the English examination.
  3. Identify the minimum and maximum marks in the English subject.

January 2024

PYTHON

Key stage IV

Class X

35 of 45

Activity 1 - Solution

1

2

3

4

5

6

7

8

9

10

11

english_marks=(23,45,34,56,89,67,89,45,56,78,90)

#Task 1:total number of students who scored 45 marks

mode_45 = english_marks.count(45)

print("Total student who score 45:",mode_45)

#Task 2: Find the total number of students

print("Total Student:",len(english_marks))

# Task 3: Identify the minimum and maximum marks

min_marks = min(english_marks)

max_marks = max(english_marks)

print("Minimum mark:",min_marks)

print("Maximum mark:",max_marks)

Total student who score 45: 2

Total Student: 11

Minimum mark: 23

Maximum marks: 90

Output

Code

January 2024

PYTHON

Key stage IV

Class X

36 of 45

Activity 2 - Calculating Total of Expenditure

Tashi went shopping for stationery before starting school. He bought various items with their respective costs: books for Nu.750, a geometry set for Nu.240, pens for Nu.100, a pencil for Nu.20, celotrip for Nu.115, and a calculator for Nu.650.

Write the Python program using the tuple to calculate and print the total expenses for the Tashi’s stationery shopping.

Solve it ………………….

January 2024

PYTHON

Key stage IV

Class X

37 of 45

Activity 3 - Average of the First 5 Natural Numbers

Write a Python program to find the average of the first 5 natural numbers from the tuple.

Solving time ………………………………….

January 2024

PYTHON

Key stage IV

Class X

38 of 45

Activity 4 - Calculating Average Age of Female Classmates

Denka is in grade 10 and she's organizing a class party. She wants to know the average age of the girls in her class so she can plan appropriate activities. She asked her female classmates for their ages and received the following data: 14, 16, 14, 17, 18, 16, 16, 15, 15, 14, 14, 17, 18, 14, 14, and 15 years old. Can you help Denka write a Python program to find the average age of her female classmates? The ages should be stored in a tuple.

Show your work …………………………….

January 2024

PYTHON

Key stage IV

Class X

39 of 45

Activity 5 - Fetching Location of Number

Dorji and his classmates are organizing a quiz competition for their grade 10 class. Each question in the quiz is assigned a unique number, and these numbers are stored in a tuple as follows:

unique_numbers = (101, 205, 309, 415, 520, 625, 730)

Dorji needs a tool to quickly find the position of each question based on its number in the list of quiz questions. He decides to use a Python program to assist him in this task. Write a Python program to help Dorji by allowing him to input a question number and then determining its position in the list of quiz questions.

Your solution time ……………………………

January 2024

PYTHON

Key stage IV

Class X

40 of 45

Activity 6 - Login system

Write a Python program to receive user input for User_ID and password. Validate the provided User_ID and password against predefined values stored in a tuple. If both conditions are true, display the message "Welcome to your page"; otherwise, show the message "Sorry, you entered incorrect User_ID and Password."

Show your work …………………………..

January 2024

PYTHON

Key stage IV

Class X

41 of 45

Difference between Data Structures

List is mutable

Tuple is immutable

Set is mutable

Dictionary is mutable

list

tuple

set

dictionary

01

Mutable

Defined using square brackets []

Defined using parentheses ()

Defined using curly braces {}

Defined using curly braces {} with key-value pairs separated by colons :

list

tuple

set

dictionary

02

Syntax

Elements are stored in a specific sequence and can be accessed using indices.

Elements are stored in a specific sequence and can be accessed using indices.

Elements are not stored in any particular sequence, and there is no concept of indexing. Set is unordered

Elements are not stored in any specific sequence, and there is no concept of indexing. Dictionary is unordered.

list

tuple

set

dictionary

03

Ordered

January 2024

PYTHON

Key stage IV

Class X

42 of 45

Difference between Data Structures(cont.)

Suitable for storing collections of items where the order matters and modification of elements is required.

Ideal for representing fixed collections of items, function arguments, and situations where data integrity is important.

Suitable for storing unique elements and performing set operations such as union, intersection, and difference.

Effective for mapping keys to values and for situations where fast lookup by key is required.

list

tuple

set

dictionary

04

Use cases

my_list = [1, 2, 3]

my_tuple = (1, 2, 3)

my_set = {1, 2, 3}

my_dict = {'a': 1, 'b': 2, 'c': 3}

list

tuple

set

dictionary

05

Example

January 2024

PYTHON

Key stage IV

Class X

43 of 45

  • A tuple is an ordered collection of elements. Tuples are immutable, meaning once they are created, their elements cannot be changed or modified.
  • Tuples are defined using parentheses () and elements are separated by commas.
  • Tuples can contain elements of different data types, including numbers, strings, and other tuples.
  • Tuple slicing allows you to create a subsequence (slice) of elements from a tuple based on specified indices or range of indices.

Key Points

January 2024

PYTHON

Key stage IV

Class X

44 of 45

  • Tuple items can be traversed or iterated using for and while loops, either through elements directly or through indices.
  • Tuples are often used in scenarios where the data should not be modified, such as representing coordinates, dates, or any fixed collection of items.
  • Tuples have limited methods compared to lists due to their immutability. Some common methods include count() for counting occurrences of a value, min() and max() for finding smallest and largest element in tuple, sum() for returning sum of all elements in tuple and index() for finding the index of a value.

Key Points

January 2024

PYTHON

Key stage IV

Class X

45 of 45

བཀྲིན་ཆེ།

THANK YOU

January 2024

PYTHON

Key stage IV

Class X