Indexing & Slicing in Python for Pharmacy Students (Extracting Patient Data & Dose Trends)
Indexing and slicing are essential techniques used to access and extract specific data from lists, tuples, and strings. In pharmaceutical applications, they are widely used for analyzing patient data, extracting drug information, and studying dose trends.
๐ท What is Indexing?
Indexing is used to access individual elements in a data structure using their position.
doses = [500, 650, 400] print(doses[0]) # Output: 500
๐ Index starts from 0 in Python.
๐ท Negative Indexing
Negative indexing allows accessing elements from the end.
print(doses[-1]) # Output: 400
๐ท What is Slicing?
Slicing extracts a portion of data using start and end index.
doses = [500, 650, 400, 550] print(doses[1:3]) # Output: [650, 400]
๐ท Slicing with Step
print(doses[0:4:2]) # Output: [500, 400]
Step value helps in skipping elements.
๐ท Indexing & Slicing in Strings
drug = "Paracetamol" print(drug[0]) # P print(drug[0:4]) # Para
๐ Patient Dose Trend Analysis
doses = [500, 650, 400, 550, 600] # First 3 doses print(doses[0:3]) # Last 2 doses print(doses[-2:])
Useful for analyzing dosage trends over time.
๐ Extracting Patient Data
patients = ["Alice", "Bob", "Charlie", "David"] # First patient print(patients[0]) # Last patient print(patients[-1])
๐ท Accessing Dictionary Data
patient = {
"name": "John",
"age": 45,
"drug": "Aspirin"
}
print(patient["name"])
๐ง Memory Tricks
- Index = Position
- Slice = Range
- -1 = Last element
๐งช Practice Exercise
Create a list of drug doses and:
- Print first 2 doses
- Print last 2 doses
- Print alternate doses
๐งช Mini Project
Analyze patient doses:
doses = [500, 650, 400, 550, 600]
average_first_half = doses[0:3]
print("First half doses:", average_first_half)
๐ MCQs
- Index starts from:
a) 1
b) 0
c) -1
d) None
Answer: b - Which index gives last element?
a) 0
b) 1
c) -1
d) 2
Answer: c - Slicing syntax:
a) [start:end]
b) (start:end)
c) {start:end}
d)
Answer: a
โ FAQs
Why is slicing important in pharmacy?
It helps extract specific patient or drug data for analysis.
What is negative indexing?
Accessing elements from the end using negative numbers.
๐ฅ Download Data Extraction Practice Sets
Includes patient datasets and dose analysis problems.
โก Next Topic: NumPy Arrays in Python โ
Recommemded readings
- Lists, Tuples & Dictionaries (with pharma data examples)
- Indexing, Slicing & Operations (Extracting patient/drug data) : Current page
- NumPy Arrays (Dose calculations, concentration arrays)
- CSV File Handling (Reading ADR datasets, writing reports)
- Understanding Healthcare Datasets (Structure, columns, patient data interpretation)
- Data Access & Manipulation (Filter, select, basic operations)
Question Bank Unit 3: Data Structures & File Handling
For more details: Basics of Python Programming for Pharmaceutical Sciences