KnowledgeBoat Logo
|
OPEN IN APP

Chapter 14

Lists and Tuples in Python

Class 10 - KIPS Robotics & AI



Activity 14.1

Question 1

What should be the output of the following program and why?

my_list=[2,3,4,5,6,7] 
print(my_list[-7]) 

Answer

The program will produce an error.

The list my_list has 6 elements, so valid negative indices range from -1 to -6. The index -7 is out of range, hence Python cannot access any element at that position.

Activity 14.2

Question 1

What should be the output of the following program and why?

list_new=[34, "Hi", 56.7, "Hello"] 
list_new.extend(["India", "Happy"]) 
print(list_new) 

Answer

Output
[34, 'Hi', 56.7, 'Hello', 'India', 'Happy']
Explanation

The extend() function is used to add multiple elements at the end of a list. Here, the elements "India" and "Happy" are added to the existing list list_new as separate elements, not as a single list.

State True or False

Question 1

You can create a list with different data items by placing them inside single quotes.

Answer

False

Reason — A list is created by placing data items inside square brackets [ ], separated by commas.

Question 2

The value of a list item can be easily updated.

Answer

True

Reason — A list is mutable, so its elements can be modified or updated after the list is created.

Question 3

Each data item in the list is assigned a unique index value.

Answer

True

Reason — Each element in a list is assigned a unique index value that helps in identifying and accessing the data items in the list.

Question 4

In Python, you cannot change the sequence of the index value of a list.

Answer

True

Reason — The index values of a list are fixed and are automatically assigned starting from 0 in sequence. While the elements of a list can be changed or rearranged, the sequence of index values itself cannot be changed.

Question 5

You cannot remove the elements of a tuple.

Answer

True

Reason — A tuple is immutable, so its elements cannot be removed or modified individually. Only the entire tuple can be deleted.

Question 6

You can sort the data items of the list using the sort() function.

Answer

True

Reason — The sort() function is used to arrange the elements of a list in ascending or descending order, so it can be used to sort the data items of a list.

Question 7

You can use the index() method to return the index value of the tuple element.

Answer

True

Reason — In Python, a tuple supports the index() method, which returns the position (index) of the first occurrence of the given element in the tuple (and raises an error if the element is not found).

Question 8

You can place an element in a list at your desired place using append() function.

Answer

False

Reason — The append() function adds an element only at the end of a list. To place an element at a desired position, the insert() function is used.

Question 9

The in operator checks whether some elements are present in the tuple.

Answer

True

Reason — The in operator is used to check whether an element is present in the tuple.

Question 10

To print all the elements of the tuple, you cannot use looping statements.

Answer

False

Reason — Looping statements can be used to print all the elements of a tuple.

Select the correct option

Question 1

You can get the ............... value of an element using the index() function.

  1. First
  2. Second
  3. Index
  4. Face

Answer

First

Reason — The index() method returns the index (position) of the first occurrence of the given element in a tuple/list. So, if the element appears multiple times, index() gives the first index value where it is found.

Question 2

Python provides ............... function to place an element at a desired place in the list.

  1. extend()
  2. insert()
  3. append()
  4. add()

Answer

insert()

Reason — The insert() function is used to place an element at a desired position in a list.

Question 3

The ............... function in Python is used to convert a list into a tuple.

  1. list()
  2. convert()
  3. tuple()
  4. list()

Answer

tuple()

Reason — The tuple() function is used to convert a list into a tuple.

Question 4

A list created within another list is called a .............. .

  1. Substring
  2. Sub
  3. Nested list
  4. Tuple

Answer

Nested list

Reason — A list created within another list is called a nested list.

Question 5

The ............... function in Python is used to add elements to an empty list.

  1. add()
  2. append()
  3. concatenate()
  4. fill()

Answer

append()

Reason — The append() function is used to add elements to an empty list.

Question 6

To print an individual item of the tuple, you need to specify the ................ value of the item inside the square brackets.

  1. Order
  2. Index
  3. Zero
  4. Negative

Answer

Index

Reason — To access or print an individual element of a tuple, its index value is specified inside square brackets.

Question 7

The ............... keyword is used to print the list elements in descending order.

  1. Sort
  2. Index
  3. Descend
  4. Reverse

Answer

Reverse

Reason — The keyword reverse=True is used with the sort() function to arrange and print list elements in descending order.

Question 8

Tuples are ............... objects.

  1. Mutable
  2. Immutable
  3. Non-static
  4. Infinite

Answer

Immutable

Reason — Tuples are immutable objects, which means their elements cannot be changed after creation.

Fill blanks

Question 1

Fill in the blanks:

  1. Python offers a wide range of data structures often referred to as ................ .
  2. In an ................ list, the square brackets [] do not hold any value.
  3. The ................ function in Python can be used when we want to know the number of elements stored in a list.
  4. A tuple is an ................ sequence of items having any number of elements of different data types.
  5. Sorting is the process of ................ and displaying the elements of a list in ascending or descending order.

Answer

  1. Python offers a wide range of data structures often referred to as sequences.
  2. In an empty list, the square brackets [] do not hold any value.
  3. The len() function in Python can be used when we want to know the number of elements stored in a list.
  4. A tuple is an ordered sequence of items having any number of elements of different data types.
  5. Sorting is the process of arranging and displaying the elements of a list in ascending or descending order.

Short answer type questions

Question 1

Write a Python program to demonstrate the difference between lists and tuples.

Answer

# Demonstrating difference between List and Tuple

# Creating a list
my_list = [10, 20, 30]
print("Original List:", my_list)

# Updating list element
my_list[1] = 25
print("Updated List:", my_list)

# Creating a tuple
my_tuple = (10, 20, 30)
print("Original Tuple:", my_tuple)

my_tuple[1] = 25
# Trying to update tuple element (will cause error)

Question 2

Write a Python program to display all the elements of a tuple using the for loop.

Solution
my_tuple = (23, 45, 67, 89, 12)

for i in my_tuple:
    print(i)
Output
23
45
67
89
12

Question 3

Write a Python program to create a list of first ten natural numbers. Add the square of each element to another empty list.

Solution
numbers = [1,2,3,4,5,6,7,8,9,10]
squares = []
for i in numbers:
    squares.append(i * i)
print("Natural numbers:", numbers)
print("Squares of numbers:", squares)
Output
Natural numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Squares of numbers: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Long answer type questions

Question 1

Write a Python program to convert a tuple into a list and a list into a tuple.

Solution
marks_tuple = (82, 64, 91, 79, 94)
marks_list = list(marks_tuple)
print("Tuple:", marks_tuple)
print("List after conversion:", marks_list)
numbers_list = [10, 20, 30, 40, 50]
numbers_tuple = tuple(numbers_list)
print("List:", numbers_list)
print("Tuple after conversion:", numbers_tuple)
Output
Tuple: (82, 64, 91, 79, 94)
List after conversion: [82, 64, 91, 79, 94]
List: [10, 20, 30, 40, 50]
Tuple after conversion: (10, 20, 30, 40, 50)

Question 2

Write a Python program to search an element in the list.

Solution
number = [45, 67, 12, 56, 78]
e = int(input("Enter the element to search: "))
if e in number:
    print("Element is found in the list.")
else:
    print("Element is not found in the list.")
Output
Enter the element to search: 67
Element is found in the list.

Question 3

Write a Python program to generate the square of all the elements of a list.

Solution
numbers = [2, 3, 4, 5, 7]
for i in numbers:
    print(i * i)
Output
4
9
16
25
49

Question 4

Write a Python program to add all the elements of the list.

Solution
numbers = [2, 3, 4, 5, 7]
total = 0
for i in numbers:
    total = total + i
print("Sum of elements:", total)
Output
Sum of elements: 21

Higher Order Thinking Skills (HOTS)

Question 1

Write a program in Python to create a list of your classmates containing their names and roll numbers. Access the roll number of the student whose name is stored at 2nd index.

Solution
classmates = [
    ["Amit", 1],
    ["Neha", 2],
    ["Rohit", 3],
    ["Sneha", 4]
]
print("Roll number:", classmates[2][1])
Output
Roll number: 3

Question 2

Find out one situation where lists are not suitable but the tuples are suitable.

Answer

Lists are not suitable when the data is fixed and should not be changed during program execution. In such cases, tuples are suitable because they are immutable (elements cannot be modified, added, or removed).

Example: Storing the days of the week.

days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

Benefit: A tuple prevents accidental changes, making the data safer and more reliable (also slightly more memory-efficient than a list).

PrevNext