KnowledgeBoat Logo
|
OPEN IN APP

Part II: AI — Chapter 4.2

Lists in Python

Class 10 - Exploring Robotics & AI



Multiple Choice Questions

Question 1

What does the following pic depict?

What does the following pic depict. Lists in Python, APC ICSE Robotics & Artificial Intelligence Solutions Class 10.
  1. Number Representation
  2. List Representation
  3. Tuple Representation
  4. None of these

Answer

List Representation

Reason — The picture shows elements enclosed within square brackets [ ], which represents a List in Python. It also displays both forward (positive) indexing starting from 0 and backward (negative) indexing starting from –1, which are features of List Indexing.

Question 2

Which of the following is returned by using list() function?

  1. A list of integers
  2. A list of strings
  3. A list of symbols
  4. An empty list

Answer

An empty list

Reason — The list() function, when used without any argument, creates and returns an empty list.

Question 3

Given: L = ['a', 'b', 'c', 'd']

Which of the following list slice will produce ['b', 'c', 'd']?

  1. L[2:4]
  2. L[1:4]
  3. L[:4]
  4. L[3:4]

Answer

L[1:4]

Reason — In list slicing, the syntax is L[initial index : final index], where elements are extracted from the initial index up to (final index − 1).

For L = ['a', 'b', 'c', 'd']:

  • L[1:4] extracts elements from index 1 to 3.
  • Index 1 → 'b'
  • Index 2 → 'c'
  • Index 3 → 'd'

Thus, the result is ['b', 'c', 'd'].

Question 4

Which of the following represents a Python list?

  1. {1, 2, 3}
  2. (1, 2, 3)
  3. [1, 2, 3]
  4. "1, 2, 3"

Answer

[1, 2, 3]

Reason — A Python list is represented by elements separated by commas and enclosed within square brackets [ ]. Therefore, [1, 2, 3] is a valid list representation.

Question 5

What will the following relation result in?

[1, 2, 3, 4] > [1, 2, 8, 9]

  1. True
  2. Error
  3. False
  4. None

Answer

False

Reason — In list comparison, corresponding elements are compared starting from the 0th index, and the result is determined as soon as unequal elements are found.

Comparison:

  • 1 = 1
  • 2 = 2
  • 3 < 8

Since the first unequal elements are 3 and 8, and 3 is less than 8, the relation [1, 2, 3, 4] > [1, 2, 8, 9] becomes False.

Question 6

Which of the following functions will concatenate the two lists together?

  1. extend()
  2. append()
  3. merge()
  4. join()

Answer

extend()

Reason — The extend() function is used to add multiple elements of one list to another list, effectively concatenating two lists. The append() function adds only a single element, while merge() and join() are not list functions.

State True or False

Question 1

State whether the following statements are True or False:

  1. The list starts with an opening square bracket and ends with a closing square bracket.
  2. The list indexing starts with 0th index (for the first element) and ranges up to the (length - 1)th index.
  3. In forward indexing, the indexing of the last element is -1.
  4. The elements in a list can be input by using eval() function.
  5. Two or more lists can be concatenated with the help of '+' operators respectively.
  6. The remove() function is used to delete an element from the given index of a list.

Answer

  1. True
  2. True
  3. False
    Corrected Statement: In forward indexing, the indexing of the last element is (length − 1).
  4. True
  5. True
  6. False
    Corrected Statement: The remove() function is used to delete an element by its value from the list.

Name the Following

Question 1

Name the built-in list function for the following tasks.

  1. To remove some elements from the given list
  2. To join two or more lists together
  3. To count an element in the list
  4. Adding an element at the end of a list
  5. Arranging the elements of a list in a specific order

Answer

  1. del
  2. extend()
  3. count()
  4. append()
  5. sort()

Assertion and Reason Based Question

Question 1

Assertion (A): You can compare two lists together by using relational operators.

Reason (R): The relational operators such as ==, <, <=, >, >=, etc. are used to compare the list elements with each other starting with the 0th index. The result of comparison will either be true or false.

Based on the above discussion, choose an appropriate statement from the options given below:

  1. Both A and R are true and R is the correct explanation of A.
  2. Both A and R are true and R is not the correct explanation of A.
  3. A is true but R is false.
  4. A is false but R is true.
  5. Both A and R are false.

Answer

Both A and R are true and R is the correct explanation of A.

Reason — We can compare two lists together by using relational operators (i.e., ==, <, <=, >, >=, etc.). During the process, the corresponding elements of both the lists are compared with each other starting with the 0th index. The result is determined the moment elements are found different. In case, all the corresponding elements are same, both the lists are declared to be the same. The result of comparison will either be true or false.

Application Based Question

Question 1

After learning the lesson, you are expected to answer the following list operations. The two lists are shown below:

L1 = ['a', 'e', 'i', 'o', 'u']
L2 = [12, 13, 14]

Predict the output for the following snippets:

(a)

L3 = L1.extend(L2)
print(L3)

(b)

L3 = L1[1:4] + L2[::]
print(L3)

(c)

del L1[1:3]
print(L1)

(d)

print(2*L2)

Answer

(a)

Output
None
Explanation

The extend() function is used to add multiple elements within another list. It adds the elements of L2 to L1, but it does not return a new list. Since the returned value is assigned to L3, the value of L3 becomes None.

(b)

Output
['e', 'i', 'o', 12, 13, 14]
Explanation

Step 1: L1[1:4]

List slicing extracts elements from the initial index up to (final index − 1). Here, elements are extracted from index 1 to 3.

L1 = ['a', 'e', 'i', 'o', 'u']

Index positions:
0 → 'a'
1 → 'e'
2 → 'i'
3 → 'o'
4 → 'u'

So, L1[1:4] gives: ['e', 'i', 'o']

Step 2: L2[::]

When no initial and final index is given, all elements of the list are extracted.

L2 = [12, 13, 14]

So, L2[::] gives: [12, 13, 14]

Step 3: + Operator

The + operator is used for concatenation, i.e., to join two lists together to form a single list. Therefore, ['e', 'i', 'o'] + [12, 13, 14] = ['e', 'i', 'o', 12, 13, 14].

(c)

Output
['a', 'o', 'u']
Explanation

The del keyword is used to delete elements from a list either at a specified index or within a range of indexes. When a range is given, elements from the initial index up to (final index − 1) are deleted. Here, del L1[1:3] deletes elements from index 1 to 2, i.e., 'e' and 'i' from the list ['a', 'e', 'i', 'o', 'u']. Hence, the remaining list becomes ['a', 'o', 'u'].

(d)

Output
[12, 13, 14, 12, 13, 14]
Explanation

List replication is the system of duplicating the elements of the list (i.e., appending the list with the elements of the same list). It is done with the help of the * operator. Therefore, 2*L2 duplicates the elements of the list [12, 13, 14] and results in [12, 13, 14, 12, 13, 14].

Question 2(i)

Predict the output of the following list operation:

print([2, 4, 6, 8] < [5, 9])

Answer

Output
True
Explanation

In list comparison, the corresponding elements of both the lists are compared with each other starting with the 0th index. The result is determined the moment elements are found different. Here, the first elements are compared: 2 and 5. Since 2 is less than 5, the expression [2, 4, 6, 8] < [5, 9] becomes True.

Question 2(ii)

Predict the output of the following list operation:

print([1, 2, 8, 5] > [1, 2, 13, 5])

Answer

Output
False
Explanation

In list comparison, the corresponding elements of both the lists are compared with each other starting with the 0th index, and the result is determined the moment elements are found different.

Comparison:
1 = 1
2 = 2
8 < 13

Since 8 is less than 13, the expression [1, 2, 8, 5] > [1, 2, 13, 5] becomes False.

Question 2(iii)

Predict the output of the following list operation:

L1 = [1, 3, 5, 3, 7, 3, 9]
c = L1.count(3)
print(c) 

Answer

Output
3
Explanation

The count() function is used to find the frequency of an element among a set of elements stored in the list. In the list [1, 3, 5, 3, 7, 3, 9], the value 3 occurs three times. Therefore, L1.count(3) returns 3.

Question 2(iv)

Predict the output of the following list operation:

L1 = [23, 34, 51, 62, 45]
print(L1(1:4))

Answer

To access or slice elements from a list, the index or range of indexes must be enclosed within square brackets [ ] along with the list variable, as per the slicing syntax <List variable>[initial index : final index]. In the given code, parentheses ( ) are used instead of square brackets, i.e., L1(1:4), which is incorrect syntax for list slicing. Therefore, the statement results in an error.

Question 2(v)

Predict the output of the following list operation:

L1 = [11, 15, 24, 38, 82] 
print(L1.pop(1) + L1.pop(3))

Answer

Output
97
Explanation

The pop(index) function is used to delete and return the element available at the given index of the list. First, L1.pop(1) deletes and returns the element at index 1, i.e., 15. The list becomes [11, 24, 38, 82]. Then, L1.pop(3) deletes and returns the element at index 3 of the updated list, i.e., 82. Therefore, the expression becomes 15 + 82, which results in 97.

Programs on Python List

Question 1

Write a Python code to accept integers in a list. Find and display the sum of even and odd numbers separately.

Solution
sum_even = 0
sum_odd = 0
Lst = eval(input("Enter list elements within []:"))

for i in range(len(Lst)):
    if(Lst[i] % 2 == 0):
        sum_even = sum_even + Lst[i]
    else:
        sum_odd = sum_odd + Lst[i]

print("Sum of even integers:", sum_even)
print("Sum of odd integers:", sum_odd)
Output
Enter list elements within []:[12, 7, 9, 14, 6, 3]
Sum of even integers: 32
Sum of odd integers: 19

Question 2

Write a Python code to accept integers in a list. Find and display the sum and product of the numbers available at even and odd indexes of the list respectively.

Solution
sum_even_index = 0
prod_odd_index = 1
L1 = eval(input("Enter list elements within [ ]:"))

for i in range(len(L1)):
    if(i % 2 == 0):
        sum_even_index = sum_even_index + L1[i]
    else:
        prod_odd_index = prod_odd_index * L1[i]

print("Sum of elements at even indexes:", sum_even_index)
print("Product of elements at odd indexes:", prod_odd_index)
Output
Enter list elements within [ ]:[5, 4, 3, 2, 6, 7]
Sum of elements at even indexes: 14
Product of elements at odd indexes: 56

Question 3

Write a Python code to enter a set of numbers in a list. Enter a number and search in the given list. If it is found then display "Search successful" otherwise display "No such number is found in the list".

Solution
L1 = eval(input("Enter a set of numbers in the list within [ ]:"))

num = eval(input("Enter an element to be searched:"))
flag = False
p = len(L1)

for a in range(p):
    if(num == L1[a]):
        flag = True
        break

if(flag == True):
    print("Search successful")
else:
    print("No such number is found in the list")
Output
Enter a set of numbers in the list within [ ]:[21, 34, 45, 28, 91, 75]
Enter an element to be searched:91
Search successful

Question 4

Write a code in Python to accept a set of names in a list. Sort the names in alphabetical order by using sort() function. Finally, display the sorted names.

Solution
L1 = eval(input("Enter names in the list within [ ]:"))
L1.sort()
print("Names in alphabetical order:", L1)
Output
Enter names in the list within [ ]:['Riya', 'Ankit', 'Meera', 'Dev']
Names in alphabetical order: ['Ankit', 'Dev', 'Meera', 'Riya']

Question 5

Write a Python code to accept elements in two different lists. Create third list that should contain all the elements of first list followed by the elements of second list. Finally, display the elements of the third list.

Solution
L1 = eval(input("Enter elements of first list within [ ]:"))
L2 = eval(input("Enter elements of second list within [ ]:"))
L3 = L1 + L2
print("Elements of the third list:", L3)
Output
Enter elements of first list within [ ]:[1, 3, 5]
Enter elements of second list within [ ]:[2, 4, 6]
Elements of the third list: [1, 3, 5, 2, 4, 6]

Question 6

Write a program in Python to accept a set of integers in a list. Display only those numbers which are composite.

Hint: A number is said to be composite, if it has more than two factors including 1 (one) and the number itself.

Solution
L1 = eval(input("Enter numbers in the list within [ ]:"))
print("The composite numbers are:")
for a in L1:
    c = 0
    for b in range(1, a+1):
        if(a % b == 0):
            c = c + 1
    if(c > 2):
        print(a)
Output
Enter numbers in the list within [ ]:[21, 45, 29, 37, 82, 31]
The composite numbers are:
21
45
82

Answer the Following Questions

Question 1

Define a list with an example.

Answer

A list is defined as an ordered sequence of data (may be same or different data types) separated by commas and enclosed within opening and closing square brackets [ ].

For example: Lst = [1, 3, 5, 7, 9]

Question 2

What is a null or empty list?

Answer

An empty or null list is a list that does not enclose any value under opening and closing square brackets [ ].

For example: Lst = [ ]

Question 3

Can a list contain data of different types? Justify.

Answer

Yes, a list can contain data of different types. A list is defined as an ordered sequence of data (may be same or different data types) separated by commas and enclosed within opening and closing square brackets [ ].

For example: ["Robotics", "ICSE", 2025]

Question 4

What is meant by list slice?

Answer

List slicing is a technique to create a list after ignoring some elements of the existing list.

Question 5

Differentiate between del and pop() functions.

Answer

Differences between del and pop() functions are:

delpop()
This statement deletes a single element as well as a set of elements from the list.This function deletes a single element from the list.
It does not return the element(s) deleted from the list.It returns the element deleted from the list.

Question 6(a)

Define len() function.

Answer

The len() function is used to find the length of a list (i.e., the number of elements available in the list).

Question 6(b)

Define extend() function.

Answer

The extend() function is used to add multiple elements within another list. It is also called merging the elements of two lists.

Question 6(c)

Define sort() function.

Answer

The sort() function is used to arrange the list of elements in a specific order (ascending or descending).

Question 6(d)

Define insert() function.

Answer

The insert() function is used to add an element at a specified index used as an argument to the function.

Question 6(e)

Define append() function.

Answer

The append() function is used to add an element at the end of an existing list of elements. It adds a single element to a list.

Question 6(f)

Define count() function.

Answer

The count() function is used to find the frequency of an element among a set of elements stored in the list.

PrevNext