fruits = ['apple', 'banana', 'orange', 'mango']
print(fruits[0]) # Output: apple
print(fruits[2]) # Output: orangefruits = ['apple', 'banana', 'orange', 'mango']
print(fruits[-1]) # Output: mango
print(fruits[-2]) # Output: orangelist1 = [1, 2, 3]
list2 = [4, 5, 6]
result = list1 + list2
print(result) # [1, 2, 3, 4, 5, 6]list1 = [1, 2]
result = list1 * 3
print(result) # [1, 2, 1, 2, 1, 2]fruits = ['apple', 'banana', 'orange']
print('banana' in fruits) # True
print('grape' in fruits) # Falsenumbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(numbers[2:6]) # [2, 3, 4, 5]
print(numbers[:4]) # [0, 1, 2, 3]
print(numbers[5:]) # [5, 6, 7, 8, 9]numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(numbers[1:8:2]) # [1, 3, 5, 7]
print(numbers[::3]) # [0, 3, 6, 9]
print(numbers[::-1]) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]fruits = ['apple', 'banana', 'orange', 'mango']
# Direct iteration
for fruit in fruits:
print(fruit)
# Using range and len
for i in range(len(fruits)):
print(f"Index {i}: {fruits[i]}")
# Using enumerate
for i, fruit in enumerate(fruits):
print(f"Index {i}: {fruit}")numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
i = 0
while i < len(numbers):
if numbers[i] % 2 == 0:
print(f"{numbers[i]} is even")
i += 1fruits = ['apple', 'banana', 'orange', 'mango']
print(len(fruits)) # Output: 4
numbers = [1, 2, 3]
print(len(numbers)) # Output: 3
empty_list = []
print(len(empty_list)) # Output: 0# String to list
text = "hello"
char_list = list(text)
print(char_list) # ['h', 'e', 'l', 'l', 'o']
# Tuple to list
tuple_data = (1, 2, 3, 4)
list_data = list(tuple_data)
print(list_data) # [1, 2, 3, 4]
# Range to list
range_list = list(range(1, 6))
print(range_list) # [1, 2, 3, 4, 5]fruits = ['apple', 'banana']
fruits.append('orange')
print(fruits) # ['apple', 'banana', 'orange']
fruits.append('mango')
print(fruits) # ['apple', 'banana', 'orange', 'mango']fruits = ['apple', 'banana']
more_fruits = ['orange', 'mango']
fruits.extend(more_fruits)
print(fruits) # ['apple', 'banana', 'orange', 'mango']
# Can also use with strings
fruits.extend('grape')
print(fruits) # ['apple', 'banana', 'orange', 'mango', 'g', 'r', 'a', 'p', 'e']fruits = ['apple', 'orange', 'mango']
fruits.insert(1, 'banana')
print(fruits) # ['apple', 'banana', 'orange', 'mango']
fruits.insert(0, 'kiwi')
print(fruits) # ['kiwi', 'apple', 'banana', 'orange', 'mango']numbers = [1, 2, 3, 2, 4, 2, 5]
print(numbers.count(2)) # Output: 3
print(numbers.count(3)) # Output: 1
print(numbers.count(6)) # Output: 0
fruits = ['apple', 'banana', 'apple', 'orange']
print(fruits.count('apple')) # Output: 2
print(fruits.count('grape')) # Output: 0fruits = ['apple', 'banana', 'orange', 'mango', 'banana']
print(fruits.index('banana')) # Output: 1
print(fruits.index('orange')) # Output: 2
print(fruits.index('apple')) # Output: 0
# Can also search in a specific range
print(fruits.index('banana', 2)) # Output: 4 (searches from index 2 onwards)
# This will raise ValueError
# print(fruits.index('grape'))fruits = ['apple', 'banana', 'orange', 'banana']
fruits.remove('banana')
print(fruits) # ['apple', 'orange', 'banana']
fruits.remove('apple')
print(fruits) # ['orange', 'banana']fruits = ['apple', 'banana', 'orange', 'mango']
last_fruit = fruits.pop()
print(last_fruit) # mango
print(fruits) # ['apple', 'banana', 'orange']
second_fruit = fruits.pop(1)
print(second_fruit) # banana
print(fruits) # ['apple', 'orange']fruits = ['apple', 'banana', 'orange']
fruits.clear()
print(fruits) # []
numbers = [1, 2, 3, 4, 5]
numbers.clear()
print(numbers) # []numbers = [3, 1, 4, 1, 5, 9, 2]
numbers.sort()
print(numbers) # [1, 1, 2, 3, 4, 5, 9]
fruits = ['banana', 'apple', 'orange', 'mango']
fruits.sort()
print(fruits) # ['apple', 'banana', 'mango', 'orange']
# Reverse sorting
numbers.sort(reverse=True)
print(numbers) # [9, 5, 4, 3, 2, 1, 1]numbers = [3, 1, 4, 1, 5, 9, 2]
sorted_numbers = sorted(numbers)
print(sorted_numbers) # [1, 1, 2, 3, 4, 5, 9]
print(numbers) # [3, 1, 4, 1, 5, 9, 2] (unchanged)
# Reverse sorting
reversed_numbers = sorted(numbers, reverse=True)
print(reversed_numbers) # [9, 5, 4, 3, 2, 1, 1]numbers = [1, 2, 3, 4, 5]
numbers.reverse()
print(numbers) # [5, 4, 3, 2, 1]
fruits = ['apple', 'banana', 'orange']
fruits.reverse()
print(fruits) # ['orange', 'banana', 'apple']