Example 1: Print the first three elements
fruits = ["Apple", "Banana", "Mango", "Orange", "Grapes"]
print(fruits[0:3])
Output:
['Apple', 'Banana', 'Mango']
Example 2: Print from index 2 to the end
fruits = ["Apple", "Banana", "Mango", "Orange", "Grapes"]
print(fruits[2:])
Output:
['Mango', 'Orange', 'Grapes']
Example 3: Print from the beginning to index 2
fruits = ["Apple", "Banana", "Mango", "Orange", "Grapes"]
print(fruits[:3])
Output:
['Apple', 'Banana', 'Mango']
Example 4: Print the last two elements
fruits = ["Apple", "Banana", "Mango", "Orange", "Grapes"]
print(fruits[-2:])
Output:
['Orange', 'Grapes']
Example 5: Print all elements except the last one
fruits = ["Apple", "Banana", "Mango", "Orange", "Grapes"]
print(fruits[:-1])
Output:
['Apple', 'Banana', 'Mango', 'Orange']
Example 6: Print every second element
fruits = ["Apple", "Banana", "Mango", "Orange", "Grapes"]
print(fruits[::2])
Output:
['Apple', 'Mango', 'Grapes']
Example 7: Reverse the list
fruits = ["Apple", "Banana", "Mango", "Orange", "Grapes"]
print(fruits[::-1])
Output:
['Grapes', 'Orange', 'Mango', 'Banana', 'Apple']
Example 8: Print the middle three elements
numbers = [10, 20, 30, 40, 50, 60, 70]
print(numbers[2:5])
Output:
[30, 40, 50]
Slicing Syntax
list_name[start : stop : step]
start → Starting index (included)
stop → Ending index (excluded)
step → Gap between elements
Quick Revision Table
Slice Meaning
list[:] Copy the entire list
list[1:4] Elements from index 1 to 3
list[:3] First 3 elements
list[2:] From index 2 to the end
list[-3:] Last 3 elements
Slice Meaning
list[:-1] All except the last element
list[::2] Every second element
list[::-1] Reverse the list
(a) len()
fruits = ["Apple", "Banana", "Mango"]
print(len(fruits))
Output:
(b) append()
fruits = ["Apple", "Banana"]
[Link]("Mango")
print(fruits)
Output:
['Apple', 'Banana', 'Mango']
(c) extend()
list1 = [1, 2]
list2 = [3, 4]
[Link](list2)
print(list1)
Output:
[1, 2, 3, 4]
(d) pop()
numbers = [10, 20, 30]
[Link]()
print(numbers)
Output:
[10, 20]
(e) remove()
colors = ["Red", "Blue", "Green"]
[Link]("Blue")
print(colors)
Output:
['Red', 'Green']
(f) del
numbers = [10, 20, 30, 40]
del numbers[1]
print(numbers)
Output:
[10, 30, 40]
(g) sort()
numbers = [5, 2, 8, 1]
[Link]()
print(numbers)
Output:
[1, 2, 5, 8]
(h) sorted()
numbers = [5, 2, 8, 1]
new_list = sorted(numbers)
print(new_list)
print(numbers)
Output:
[1, 2, 5, 8]
[5, 2, 8, 1]
(i) copy()
list1 = [10, 20, 30]
list2 = [Link]()
print(list2)
Output:
[10, 20, 30]
(j) count()
numbers = [1, 2, 2, 3, 2]
print([Link](2))
Output:
(k) max()
numbers = [10, 50, 20]
print(max(numbers))
Output:
50
(l) min()
numbers = [10, 50, 20]
print(min(numbers))
Output:
10
(m) sum()
numbers = [10, 20, 30]
print(sum(numbers))
Output:
60
CREATE TABLE Student5 ( No INT PRIMARY KEY, Name VARCHAR(30), Stipend DECIMAL(8,2), Stream
VARCHAR(20), AvgMark DECIMAL(5,2), Grade CHAR(1), Class VARCHAR(5) );
INSERT INTO Student5 (No, Name, Stipend, Stream, AvgMark, Grade, Class) VALUES (1, 'Karan',
400.00, 'Medical', 78.50, 'B', '12B'), (2, 'Divakar', 450.00, 'Commerce', 89.20, 'A', '11C'), (3, 'Divya',
300.00, 'Commerce', 68.60, 'C', '12C'), (4, 'Arun', 350.00, 'Humanities', 73.10, 'B', '12C'), (5, 'Sabina',
500.00, 'Nonmedical', 90.60, 'A', '11A'), (6, 'John', 400.00, 'Medical', 75.40, 'B', '12B'), (7, 'Robert',
250.00, 'Humanities', 64.40, 'C', '11A'), (8, 'Rubina', 450.00, 'Nonmedical', 88.50, 'A', '12A'), (9,
'Vikas', 500.00, 'Nonmedical', 92.00, 'A', '12A'), (10, 'Mohan', 300.00, 'Commerce', 67.50, 'C', '12C');
P Y T H O N __ P R O G R A M M I N G
print(z[1:16:3])