-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path01_list_operations.py
More file actions
30 lines (24 loc) · 909 Bytes
/
Copy path01_list_operations.py
File metadata and controls
30 lines (24 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 01_list_operations.py
# Concept: Mastering Lists - Mutability and Sequence Operations
# 1. Creating a list
fruits = ["apple", "banana", "cherry"]
print(f"Initial list: {fruits}")
# 2. Append: Adding to the end
fruits.append("orange")
print(f"After append: {fruits}")
# 3. Insert: Adding at a specific index
fruits.insert(1, "blueberry")
print(f"After insert at index 1: {fruits}")
# 4. Remove: Deleting by value
fruits.remove("banana")
print(f"After removing 'banana': {fruits}")
# 5. Pop: Deleting by index (returns the removed item)
last_fruit = fruits.pop()
print(f"Popped item: {last_fruit}")
print(f"After pop: {fruits}")
# 6. Slicing: Accessing sub-parts of the list [start:stop:step]
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(f"\nNumbers: {numbers}")
print(f"Slice [2:7]: {numbers[2:7]}")
print(f"Every second number [::2]: {numbers[::2]}")
print(f"Reversed list [::-1]: {numbers[::-1]}")