1.Create a list and print the third item
Concept: Lists store multiple values. Index starts from 0.
items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"]
print(items[2])
Explanation:
Index 2 refers to the third element → "Eraser".
- Add “Glue Stick” to the list Concept: Use append() to add at the end.
items.append("Glue Stick")
print(items)
Explanation:
append() adds a new item to the end of the list.
- Insert “Highlighter” Concept: Use insert(index, value).
items.insert(2, "Highlighter")
print(items)
Explanation:
“Highlighter” is inserted at index 2 (between 2nd and 3rd items).
- Remove “Ruler” Concept: Use remove().
items.remove("Ruler")
print(items)
Explanation:
Removes the specified value from the list.
5.Print first three items
Concept: List slicing.
print(items[:3])
Explanation:
[:3] returns first three elements.
- Convert to uppercase Concept: List comprehension.
upper_items = [item.upper() for item in items]
print(upper_items)
Explanation:
Each item is converted to uppercase.
- Check if “Marker” exists Concept: Membership operator in.
if "Marker" in items:
print("Marker is found")
else:
print("Marker is not found")
Explanation:
Checks if the item exists in the list.
8.Count number of items
Concept: Use len().
print(len(items))
Explanation:
Returns total number of elements.
- Sort the list Concept: Use sort().
items.sort()
print(items)
Explanation:
Sorts list alphabetically.
10.Reverse the list
Concept: Use reverse().
items.reverse()
print(items)
Explanation:
Reverses the order of elements.
- List with item and delivery time Concept: Nested lists.
delivery = [["Notebook", "10 AM"], ["Pencil", "11 AM"]]
print(delivery[0])
Explanation:
Prints first item and its time.
12.Count occurrences of “Ruler”
Concept: Use count().
print(items.count("Ruler"))
Explanation:
Counts how many times it appears.
- Find index of “Pencil” Concept: Use index().
print(items.index("Pencil"))
Explanation:
Returns position of the item.
- Extend list with new items Concept: Use extend().
new_items = ["Pen", "Sharpener"]
items.extend(new_items)
print(items)
Explanation:
Adds multiple items to list.
15.Clear the list
Concept: Use clear().
items.clear()
print(items)
Explanation:
Removes all elements.
16.Repeat item three times
Concept: List multiplication.
items = ["Notebook"] * 3
print(items)
Explanation:
Repeats the item three times.
17.Nested list comprehension (item + length)
Concept: Advanced list comprehension.
items = ["Notebook", "Pencil", "Eraser"]
result = [[item, len(item)] for item in items]
print(result)
Explanation:
Each sublist contains item and its length.
18.Filter items with letter “e”
Concept: Conditional list comprehension.
filtered = [item for item in items if "e" in item]
print(filtered)
Explanation:
Only items containing “e” are included.
19.Remove duplicates
Concept: Use set().
items = ["Notebook", "Pencil", "Notebook", "Eraser"]
unique_items = list(set(items))
print(unique_items)
Explanation:
Set removes duplicate values.
Top comments (0)