Python for Data Science: Laboratory Programs in Python : Numpy and Pandas Libraries : Array Slicing and Indexing operations
Indexing
import numpy as np
arr = np.array([10,20,30,40,50])
print("Element at index 0: ",arr[0])
print("Element at last position: ",arr[‒1])
Element at index 0: 10
Element at last position: 50
import numpy as np
arr = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
print(arr[0,2])
print(arr[2,1])
3
8
Code explanation: In above code,
Using first print statement, we can access the element 3 which is at position 0th row and 2nd column.
Using second print statement, we can access element 8 which is at position at position 2nd row and first column.
Slicing
array[start: end: step]
where
start: Index where slice begins.
end: Index where slice ends.
step: How many elements to skip
Following python programs illustrate the use of slicing in NumPy.
import numpy as np
arr = np.array([10,20,30,40,50,60])
print("Displaying slice(elements from 1 to 3)")
print(arr[1:4])
print("Displaying first three elements as slice")
print(arr[:3])
print("Displaying every alternate element as slice")
print(arr[::2])
Displaying slice (elements from 1 to 3)
[20 30 40]
Displaying first three elements as slice
[10 20 30]
Displaying every alternate element as slice
[10 30 50]
The working of above code can be illustrated by following figure ‒

import numpy as np
arr = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
print("Slice second and third rows and first three columns")
print(arr[1:3,0:3])
slice second and third rows and first three columns
[[ 5 6 7]
[ 9 10 11]]
Following figure illustrates how to slice a two dimensional array ‒

Iterating
import numpy as np
arr = np.array([10,20,30,40,50])
for x in arr:
print(x)
10
20
30
40
50
Code explanation: In above code,
There is a for loop that iterates over each element in the arr array. For each element x, it prints the value to the console,
import numpy as np
arr = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
for row in arr:
print("Row: ",row)
Row: [1 2 3]
Row: [4 5 6]
Row: [7 8 9]
In above code, the code
for row in arr:
print("Row: ", row)
• This is a for loop that iterates over each row of the 2D array.
• In each iteration, the variable row holds one row of the matrix.
• It prints each row with the label "Row: ".
Python for Data Science: Laboratory Programs in Python : Tag: Computer Programming, Python, Data Science : Laboratory Programs in Python - Array Indexing and Slicing operations
Computer Programming Python Laboratory
CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Python for Data Science
AD25201 2nd Semester AIDS Dept, 4th Semester CSE, CSE(CY) Departments | 2025 Regulation | 2nd, 4th Semester 2025 Regulation
Python for Data Science - Laboratory
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Foundations of Data Science using Python
IT25201 2nd Semester IT Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials I
EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
தமிழர் மரபு - Heritage of Tamils
UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Calculus
MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I
PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Chemistry I
CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Makerspace
ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming C
CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming Python
CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Fundamentals of Electrical and Electronics Engineering
EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Mechanical Engineering
ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Civil Engineering
CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing
CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I Laboratory
PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Applied Chemistry I Laboratory
CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming C Laboratory
CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming Python Laboratory
CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Engineering Drawing
ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 1st, 2nd Semester 2025 Regulation
Basic Electronics and Electrical Engineering
EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing - Laboratory
CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation