What does the following pic depict?

- Number Representation
- List Representation
- Tuple Representation
- 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.
Which of the following is returned by using list() function?
- A list of integers
- A list of strings
- A list of symbols
- An empty list
Answer
An empty list
Reason — The list() function, when used without any argument, creates and returns an empty list.
Given: L = ['a', 'b', 'c', 'd']
Which of the following list slice will produce ['b', 'c', 'd']?
- L[2:4]
- L[1:4]
- L[: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'].
Which of the following represents a Python list?
- {1, 2, 3}
- (1, 2, 3)
- [1, 2, 3]
- "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.
What will the following relation result in?
[1, 2, 3, 4] > [1, 2, 8, 9]
- True
- Error
- False
- 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.
Which of the following functions will concatenate the two lists together?
- extend()
- append()
- merge()
- 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 whether the following statements are True or False:
- The list starts with an opening square bracket and ends with a closing square bracket.
- The list indexing starts with 0th index (for the first element) and ranges up to the (length - 1)th index.
- In forward indexing, the indexing of the last element is -1.
- The elements in a list can be input by using eval() function.
- Two or more lists can be concatenated with the help of '+' operators respectively.
- The remove() function is used to delete an element from the given index of a list.
Answer
- True
- True
- False
Corrected Statement: In forward indexing, the indexing of the last element is (length − 1). - True
- True
- False
Corrected Statement: Theremove()function is used to delete an element by its value from the list.
Name the built-in list function for the following tasks.
- To remove some elements from the given list
- To join two or more lists together
- To count an element in the list
- Adding an element at the end of a list
- Arranging the elements of a list in a specific order
Answer
- del
- extend()
- count()
- append()
- sort()
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:
- Both A and R are true and R is the correct explanation of A.
- Both A and R are true and R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
- 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.
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)
None
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)
['e', 'i', 'o', 12, 13, 14]
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)
['a', 'o', 'u']
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)
[12, 13, 14, 12, 13, 14]
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].
Predict the output of the following list operation:
print([2, 4, 6, 8] < [5, 9])Answer
True
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.
Predict the output of the following list operation:
print([1, 2, 8, 5] > [1, 2, 13, 5])Answer
False
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.
Predict the output of the following list operation:
L1 = [1, 3, 5, 3, 7, 3, 9]
c = L1.count(3)
print(c) Answer
3
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.
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.
Predict the output of the following list operation:
L1 = [11, 15, 24, 38, 82]
print(L1.pop(1) + L1.pop(3))Answer
97
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.
Write a Python code to accept integers in a list. Find and display the sum of even and odd numbers separately.
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)Enter list elements within []:[12, 7, 9, 14, 6, 3]
Sum of even integers: 32
Sum of odd integers: 19
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.
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)Enter list elements within [ ]:[5, 4, 3, 2, 6, 7]
Sum of elements at even indexes: 14
Product of elements at odd indexes: 56
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".
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")Enter a set of numbers in the list within [ ]:[21, 34, 45, 28, 91, 75]
Enter an element to be searched:91
Search successful
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.
L1 = eval(input("Enter names in the list within [ ]:"))
L1.sort()
print("Names in alphabetical order:", L1)Enter names in the list within [ ]:['Riya', 'Ankit', 'Meera', 'Dev']
Names in alphabetical order: ['Ankit', 'Dev', 'Meera', 'Riya']
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.
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)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]
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.
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)Enter numbers in the list within [ ]:[21, 45, 29, 37, 82, 31]
The composite numbers are:
21
45
82
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]
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 = [ ]
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]
What is meant by list slice?
Answer
List slicing is a technique to create a list after ignoring some elements of the existing list.
Differentiate between del and pop() functions.
Answer
Differences between del and pop() functions are:
| del | pop() |
|---|---|
| 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. |
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).
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.
Define sort() function.
Answer
The sort() function is used to arrange the list of elements in a specific order (ascending or descending).
Define insert() function.
Answer
The insert() function is used to add an element at a specified index used as an argument to the function.
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.
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.