## Python Basics - List Manipulation (Part-1)

CBSE Class 11 - Informatics Practices & Computer Science

Q1: What is a list in Python?

Answer: List is a collections of items and each item has its own index value. It is the most versatile datatype available in core Python which can be written as a list of comma-separated values (items) between square brackets.

e.g.
[] # empty list
[1,2,3,4] #list of integers
[2.0, 4.56, 8.91, ] #list of floats
['Sunday', 'Monday', 'Tuesday'] #list of floats
['Nikita', 16, 'Class 11', True, 86.7] # list of mixed values

Q2: What are different ways to create a list?

Answer: Creating a list is as simple as putting different comma-separated values between square brackets or you can use list( ) function.
e.g.
list1 = [3,5,9] #stores list of numbers
list2 = ['Hello', 123, True, 17.89] #stores list of mixed values
list3 = [] #creates empty list
list4 = list() #creates empty list
list5 = list("aeiou") #creates list as ['a', 'e', 'i', 'e', 'o']

Q3: Are lists mutable or immutable?

Answer: Lists are mutable i.e. you can change the elements of a list.

Q4: Can a list have another list? Give an example.

Answer: Yes, a list can contain other lists. Such a list is called nested list.
e.g.
>>> list1 = [1,2,[3,4],5,6]
Here list1 i.e. at position 2, stores a list [3,4]

Q5: How can an element of a list be accessed? Give an example to access values in lists.

Answer: List items can be accessed using its index position. To access individual elements of a list using the variable name for the list with an integer in square brackets. It supports zero-based indexing i.e. the first element of a list is position 0, the second position is 1 and so on.
e.g.
list1 = [3,4,5,6,7]
print(list) #displays 3 at 0-position
print(list) #displays 5 at 2-position.

It also supports negative indexing.
e.g.
print(list[-1]) #displays 7 i.e. first element from the last position.

Q6: What is slicing in lists? Give an example.

Answer: List elements can be accessed in subparts called slicing.
e.g.
list1 = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
list1[2:4] #displays ['Mar', 'Apr']

Q7: How are lists different from strings when both are sequences?

Answer: The lists and strings are different in the following ways:
(i) The lists are mutable sequences while strings are immutable.

(ii) Strings store single type of elements, all characters while lists can store elements belonging to different types.

(iii) Strings store the individual characters in consecutive memory locations while list stores the references of its elements.

Q8: Give an example to iterate items of a list using for statement.

Answer: List elements can be accessed using looping statement.
e.g.
mylist = [10, 30, 19, 21, 29]
for item in mylist:
print(item)

Q9: Name the function used to find the number of items in a list. Give an example.

e.g.
>>>list1 = [2,3,4,5,6]
>>>print(len(list1))
5

Q10: What is the output of the following code?
l1 = [1,3,5]
l2 = [1,3,5]
l3 = [1,3,6]
l1 == l2
l1 == l3
l2 < l3
l2 > l3

True
False
True
False

Python - String Manipulation (Part-1) Q & A
Python - String Manipulation (Part-2) Q & A  