Sample Output Questions for Practice
Q1: What will be the output of the following Python code?
fruits = ["apple", "banana", "cherry"]
fruits[1] = "orange"
[Link]("grape")
[Link](2)
print(fruits)
Output:
['apple', 'banana', 'grape']
['apple', 'orange', 'grape']
['apple', 'orange', 'cherry', 'grape']
['apple', 'orange', 'cherry']
Q2:What will be the output of the following Python code?
numbers = [2, 4, 6, 8, 10]
print(numbers[1:4])
numbers[2:4] = [12, 14]
print(numbers)
Output:
[4, 6, 8] and [2, 4, 12, 14, 10]
[4, 6, 8] and [2, 4, 12, 14]
[4, 6, 8, 10] and [2, 4, 6, 12, 14]
[4, 6, 8] and [2, 4, 6, 8, 10]
Q3:What will be the output of the following Python code?
list1 = [1, 2, 3]
list2 = [4, 5, 6]
[Link](list2)
[Link](2)
print(list1)
Output:
[1, 2, 3, 4, 5, 6]
[1, 3, 4, 5, 6]
[1, 3, 4, 5, 6, 2]
[1, 2, 4, 5, 6]
Q4:What will be the output of the following Python code?
my_list = ["a", "b", "c", "d"]
my_list.reverse()
my_list.remove("c")
my_list.insert(1, "e")
print(my_list)
Output:
['d', 'e', 'c', 'b']
['d', 'e', 'a']
['d', 'e', 'b', 'a']
['c', 'd', 'b', 'e']
Q5: What will be the output of the following Python code?
nums = [10, 20, 30, 40, 50]
print(len(nums))
[Link]()
print(nums)
Output:
5 and []
5 and [0]
4 and []
5 and [10]
Q6: What will be the output of the following Python code?
numbers = [1, 2, 3, 4]
[Link](5)
numbers[1:3] = [10, 11, 12]
[Link]([13, 14])
print(numbers)
Output:
[1, 10, 11, 12, 4, 5, 13, 14]
[1, 10, 11, 12, 13, 14]
[1, 2, 3, 4, 5, 13, 14]
[1, 2, 10, 11, 12, 4, 5, 13, 14]
Q2: What will be the output of the following Python code?
letters = ['a', 'b', 'c']
[Link]('d')
letters[1:2] = ['x', 'y', 'z']
[Link](['e', 'f'])
print(letters)
Output:
['a', 'x', 'y', 'z', 'c', 'd', 'e', 'f']
['a', 'x', 'y', 'z', 'd', 'e', 'f']
['a', 'x', 'y', 'z', 'c', 'd']
['a', 'x', 'y', 'z', 'c', 'd', 'e', 'f']
Q3: What will be the output of the following Python code?
data = [10, 20, 30]
[Link](40)
data[1:2] = [15, 25, 35]
[Link]([45, 50])
print(data)
Output:
[10, 15, 25, 35, 30, 40, 45, 50]
[10, 15, 25, 35, 45, 50]
[10, 15, 25, 35, 30, 40]
[10, 20, 15, 25, 35, 40, 45, 50]
Q4: What will be the output of the following Python code?
colors = ['red', 'green']
[Link]('blue')
colors[1:1] = ['yellow', 'purple']
[Link](['orange', 'pink'])
print(colors)
Output:
['red', 'yellow', 'purple', 'green', 'blue', 'orange', 'pink']
['red', 'yellow', 'purple', 'green', 'blue', 'orange']
['red', 'yellow', 'purple', 'green', 'blue', 'pink']
['red', 'green', 'yellow', 'purple', 'blue', 'orange', 'pink']
Q5: What will be the output of the following Python code?
numbers = [5, 10, 15]
[Link](20)
numbers[1:3] = [8, 12, 16]
[Link]([25, 30])
print(numbers)
Output:
[5, 8, 12, 16, 20, 25, 30]
[5, 8, 12, 16, 10, 25, 30]
[5, 8, 12, 16, 25, 30]
[5, 8, 12, 16, 20, 25, 30]
Revision summary for list operations involving append(), list slicing, and extend():
1. append(): Adds a single element to the end of the list.
o Example: [Link](5) → Adds 5 to the end of the list.
2. List Slicing: Accesses or modifies a portion of the list using [start:end].
o Example: list[1:3] = [10, 11] → Replaces elements at indices 1 and 2.
3. extend(): Adds multiple elements (from an iterable) to the end of the list.
o Example: [Link]([6, 7]) → Adds 6 and 7 to the end of the list.
Combining Operations:
append() adds one element.
Slicing can replace part of the list.
extend() adds multiple elements from another iterable.
Example:
python
Copy code
numbers = [1, 2, 3]
[Link](4) # [1, 2, 3, 4]
numbers[1:3] = [5, 6, 7] # [1, 5, 6, 7, 4]
[Link]([8, 9]) # [1, 5, 6, 7, 4, 8, 9]