CDLC
CDLC
🚀🚀 Level-1 🚀🚀
1. What is a List?
● Imagine a list as a box where you can store different things.
● You can put toys, books, or chocolates in the box, and they will stay in the order you put
them in.
● In Python, a list works like this box. You can put numbers, words, or even other boxes
(lists) inside a list.
Creating a list is like making a new box and deciding what to put in it. You use square brackets
[] and put whatever you want inside, separated by commas.
This list has a number (1), a word ('apple'), and another number (3.14).
If you want to put something new in your box or change something that's already there, you can
do that easily.
Add something to the end: You tell Python, "Put this at the end of my list."
my_list.append('banana')
●
Change something: You tell Python, "Change this thing in my list to something else."
my_list[1] = 'orange'
●
If you want to take something out of your box, you can do that too.
Remove something specific: You tell Python, "Take this out of my list."
my_list.remove('apple')
●
Remove the last thing: You tell Python, "Take out the last thing I put in."
last_item = my_list.pop()
●
You can check what's inside your box without changing anything.
🚀🚀 Level-2 🚀🚀
1. Example:
my_list = [1, 2, 3, 4, 5]
print(my_list)
Output:
[1, 2, 3, 4, 5]
print(my_list[1])
Output:
2
Example 3: Slicing a List
print(my_list[1:4])
Output:
[2, 3, 4]
my_list.append(6)
print(my_list)
Output:
[1, 2, 3, 4, 5, 6]
my_list.extend([7, 8])
print(my_list)
Output:
[1, 2, 3, 4, 5, 6, 7, 8]
my_list.insert(0, 0)
print(my_list)
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8]
my_list.remove(0)
print(my_list)
Output:
[1, 2, 3, 4, 5, 6, 7, 8]
Output:
[1, 2, 3, 4, 5, 6, 7]
print(my_list.index(4))
Output:
print(my_list.count(4))
Output:
my_list.reverse()
print(my_list)
Output:
[7, 6, 5, 4, 3, 2, 1]
my_list.sort()
print(my_list)
Output:
[1, 2, 3, 4, 5, 6, 7]
copy_list = my_list.copy()
print(copy_list)
Output:
[1, 2, 3, 4, 5, 6, 7]
my_list.clear()
print(my_list)
Output:
[]
print([0] * 5)
Output:
[0, 0, 0, 0, 0]
Output:
[1, 2, 3, 4, 5, 6]
Output:
True
Output:
[2, 4, 6]
Example 19: Nested Lists
Output:
print(len([1, 2, 3, 4, 5]))
Output:
🚀🚀 Level-3 🚀🚀
MCQs on Python Lists
1. How do you add an element '7' to the end of a list [1, 2, 3, 4, 5, 6]?
○ A. [1, 2, 3, 4, 5, 6].insert(7)
○ B. [1, 2, 3, 4, 5, 6].append(7)
○ C. [1, 2, 3, 4, 5, 6] + 7
○ D. [1, 2, 3, 4, 5, 6].extend(7)
2. What will [1, 2, 3] + [4, 5, 6] output?
○ A. [5, 7, 9]
○ B. [1, 2, 3, 4, 5, 6]
○ C. [1, 4, 2, 5, 3, 6]
○ D. TypeError
3. How do you remove the first occurrence of '2' from the list [1, 2, 3, 2, 4]?
○ A. .pop(2)
○ B. .delete(2)
○ C. .remove(2)
○ D. .discard(2)
4. Which method is used to reverse the order of elements in [1, 2, 3, 4, 5]?
○ A. .reverse()
○ B. .reversed()
○ C. .sort(reverse=True)
○ D. .flip()
5. What is the output of len([1, 2, 3, 4, 5])?
○ A. 4
○ B. 5
○ C. 6
○ D. None
6. How do you concatenate two lists [1, 2, 3] and [4, 5, 6]?
○ A. [1, 2, 3].append([4, 5, 6])
○ B. [1, 2, 3] + [4, 5, 6]
○ C. [1, 2, 3].extend([4, 5, 6])
○ D. B and C
7. What does [0] * 5 produce?
○ A. [0, 0, 0, 0, 0]
○ B. [5]
○ C. [0, 5]
○ D. TypeError
8. How do you check if '4' exists in the list [1, 2, 3, 4, 5]?
○ A. [1, 2, 3, 4, 5].contains(4)
○ B. 4 in [1, 2, 3, 4, 5]
○ C. [1, 2, 3, 4, 5].index(4)
○ D. [1, 2, 3, 4, 5].find(4)
9. Which method returns the index of the first element with the specified value '3' in [1, 2,
3, 4, 3]?
○ A. .index(3)
○ B. .find(3)
○ C. .locate(3)
○ D. .search(3)
10. What is the correct way to copy a list [1, 2, 3, 4, 5] to another list?
○ A. new_list = [1, 2, 3, 4, 5].copy()
○ B. new_list = copy([1, 2, 3, 4, 5])
○ C. new_list = [1, 2, 3, 4, 5]
○ D. new_list = list([1, 2, 3, 4, 5])
Answers:
1. B
2. B
3. C
4. A
5. B
6. D
7. A
8. B
9. A
10. A