0% found this document useful (0 votes)
15 views7 pages

CDLC

Cdlc

Uploaded by

Ajinkya Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views7 pages

CDLC

Cdlc

Uploaded by

Ajinkya Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DAY : List in Python

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

2. Why Use Lists?


● Lists are great because they help you keep things organized. If you have a bunch of
things you want to keep track of, like a shopping list or all the names of your friends, you
can put them in a list in Python.
● List helps you:
○ Keep things in order: You know what comes first, second, third, and so on.
○ Change things easily: If you forgot to add something or added something by
mistake, you can add it or remove it easily.
○ Find out what's inside: You can look inside the list to see if something is there
or see everything you've put in the list.

3. How to Use Lists

3.1 Making 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.

my_list = [1, 'apple', 3.14]

This list has a number (1), a word ('apple'), and another number (3.14).

3.2 Adding and Changing Things

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'

3.3 Taking Things Out

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

3.4 Looking Inside

You can check what's inside your box without changing anything.

See everything: Just print the list.


print(my_list)

🚀🚀 Level-2 🚀🚀
1. Example:

Example 1: Creating a List

my_list = [1, 2, 3, 4, 5]
print(my_list)

Output:

[1, 2, 3, 4, 5]

Example 2: Accessing List Elements

print(my_list[1])

Output:

2
Example 3: Slicing a List

print(my_list[1:4])

Output:

[2, 3, 4]

Example 4: Appending to a List

my_list.append(6)
print(my_list)

Output:

[1, 2, 3, 4, 5, 6]

Example 5: Extending a List

my_list.extend([7, 8])
print(my_list)

Output:

[1, 2, 3, 4, 5, 6, 7, 8]

Example 6: Inserting into a List

my_list.insert(0, 0)
print(my_list)

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8]

Example 7: Removing from a List

my_list.remove(0)
print(my_list)

Output:

[1, 2, 3, 4, 5, 6, 7, 8]

Example 8: Popping from a List


my_list.pop()
print(my_list)

Output:

[1, 2, 3, 4, 5, 6, 7]

Example 9: Finding the Index of an Element

print(my_list.index(4))

Output:

Example 10: Counting Occurrences in a List

print(my_list.count(4))

Output:

Example 11: Reversing a List

my_list.reverse()
print(my_list)

Output:

[7, 6, 5, 4, 3, 2, 1]

Example 12: Sorting a List

my_list.sort()
print(my_list)

Output:

[1, 2, 3, 4, 5, 6, 7]

Example 13: Copying a List

copy_list = my_list.copy()
print(copy_list)
Output:

[1, 2, 3, 4, 5, 6, 7]

Example 14: Clearing a List

my_list.clear()
print(my_list)

Output:

[]

Example 15: List Multiplication

print([0] * 5)

Output:

[0, 0, 0, 0, 0]

Example 16: Concatenating Lists

print([1, 2, 3] + [4, 5, 6])

Output:

[1, 2, 3, 4, 5, 6]

Example 17: Checking for Existence in a List

print(1 in [1, 2, 3])

Output:

True

Example 18: Iterating Over List Elements (Using List Comprehension)

print([x * 2 for x in [1, 2, 3]])

Output:

[2, 4, 6]
Example 19: Nested Lists

nested_list = [[1, 2], [3, 4], [5, 6]]


print(nested_list)

Output:

[[1, 2], [3, 4], [5, 6]]

Example 20: List Length

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

You might also like