Lists and Tuples in Python

ICSE Class 10 Robotics & AI · KIPS Robotics & AI · 30 solved questions

Exercise
1Activity 14.1

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

my_list=[2,3,4,5,6,7] 
print(my_list[-7]) 
Show SolutionHide Solution↓

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.

1Activity 14.2

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) 
Show SolutionHide Solution↓
[34, 'Hi', 56.7, 'Hello', 'India', 'Happy']

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.

1State True or False

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

Show SolutionHide Solution↓

False

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

2State True or False

The value of a list item can be easily updated.

Show SolutionHide Solution↓

True

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

3State True or False

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

Show SolutionHide Solution↓

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.

4State True or False

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

Show SolutionHide Solution↓

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.

5State True or False

You cannot remove the elements of a tuple.

Show SolutionHide Solution↓

True

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

6State True or False

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

Show SolutionHide Solution↓

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.

7State True or False

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

Show SolutionHide Solution↓

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).

8State True or False

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

Show SolutionHide Solution↓

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.

9State True or False

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

Show SolutionHide Solution↓

True

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

10State True or False

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

Show SolutionHide Solution↓

False

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

1Select the correct option

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

Show SolutionHide Solution↓

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.

2Select the correct option

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

Show SolutionHide Solution↓

insert()

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

3Select the correct option

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

Show SolutionHide Solution↓

tuple()

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

4Select the correct option

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

Show SolutionHide Solution↓

Nested list

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

5Select the correct option

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

Show SolutionHide Solution↓

append()

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

6Select the correct option

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

Show SolutionHide Solution↓

Index

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

7Select the correct option

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

Show SolutionHide Solution↓

Reverse

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

8Select the correct option

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

Show SolutionHide Solution↓

Immutable

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

1Fill blanks

Fill in the blanks:

Show SolutionHide Solution↓
  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.
1Short answer type questions

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

Show SolutionHide Solution↓
# 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)
2Short answer type questions

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

Show SolutionHide Solution↓
my_tuple = (23, 45, 67, 89, 12)

for i in my_tuple:
    print(i)
23
45
67
89
12
3Short answer type questions

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

Show SolutionHide 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)
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]
1Long answer type questions

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

Show SolutionHide 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)
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)
2Long answer type questions

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

Show SolutionHide 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.")
Enter the element to search: 67
Element is found in the list.
3Long answer type questions

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

Show SolutionHide Solution↓
numbers = [2, 3, 4, 5, 7]
for i in numbers:
    print(i * i)
4
9
16
25
49
4Long answer type questions

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

Show SolutionHide Solution↓
numbers = [2, 3, 4, 5, 7]
total = 0
for i in numbers:
    total = total + i
print("Sum of elements:", total)
Sum of elements: 21
1Higher Order Thinking Skills (HOTS)

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.

Show SolutionHide Solution↓
classmates = [
    ["Amit", 1],
    ["Neha", 2],
    ["Rohit", 3],
    ["Sneha", 4]
]
print("Roll number:", classmates[2][1])
Roll number: 3
2Higher Order Thinking Skills (HOTS)

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

Show SolutionHide Solution↓

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).

Press ↓ to reveal a solution and move to the next question. Back to KIPS Robotics & AI.

Free sample bank