Unit II · Basics of Python Programming for Pharmaceutical Sciences (BP101T) · As per PCI B.Pharmacy Syllabus, NEP 2020
Many tasks in pharmacy and pharmaceutical science are inherently repetitive: counting tablets in a strip, recording a dissolution reading every minute, generating dosage schedules across several days, or testing multiple samples across multiple batches. Writing separate lines of code for every repetition would be both tedious and error-prone. Python’s loops solve this problem by letting a block of code execute automatically, again and again, until a defined condition is met. This post covers the two primary loop types in Python — the for loop and the while loop — along with nested loops, and ties each to concrete pharmaceutical examples the way BP101T expects.

What a Loop Actually Does
A loop is a control structure that repeats a block of code until some condition is satisfied, so the programmer never has to retype the same instructions. Put simply, a loop tells the computer to “carry out the same task again and again, automatically.” Python provides three related tools for this: the for loop, used when the number of repetitions is already known in advance; the while loop, used when repetition depends on a condition rather than a fixed count; and nested loops, where one loop is placed inside another to handle multi-dimensional repetition, such as testing several samples within each of several batches.
The for Loop: Fixed, Known Repetition
A for loop steps through the items of a sequence or other iterable object — a list, tuple, dictionary, set, or string — executing its body once for every item. It is the loop of choice whenever the number of repetitions is already known in advance, because it automatically advances from one item to the next without the programmer having to manage a counter manually.
for variable in sequence:
# code
The most common way to generate a known number of repetitions is Python’s built-in range() function, which produces a sequence of numbers. Here it prints the numbers 1 through 5:
for i in range(1, 6):
print(i)
This prints each number on its own line: 1 2 3 4 5. Note that range(1, 6) produces 1, 2, 3, 4, 5 — it stops before the second argument, a detail that trips up many beginners. The same pattern extends naturally to pharmacy counting tasks, such as numbering the individual tablets in a strip:
for tablet in range(1, 11):
print("Tablet", tablet)
This produces one line per tablet, counting from 1 through 10 (Tablet 1, Tablet 2, … Tablet 10). Practical uses of the for loop in a pharmacy setting include counting the individual tablets present in a strip, generating labels for medicine units, and iterating over a fixed list of drug names to print or process each one in turn.
Looping Directly Over a List of Drugs
A for loop is not limited to numbers — it can iterate directly over a list of strings, which is extremely common in pharmacy programs that work with drug names, patient IDs, or batch codes.
#List of antibiotics used in pharmacy practice
antibiotics = ["Amoxicillin", "Ciprofloxacin", "Azithromycin",
"Doxycycline", "Metronidazole"]
print("List of antibiotics:")
for drug in antibiotics:
print(drug)
Here, drug takes on each string in the antibiotics list in turn, so the output lists every antibiotic name on its own line, in the exact order they appear in the list — without the programmer ever writing a numeric index.
The while Loop: Condition-Based Repetition
A while loop keeps executing its body for as long as a specified Boolean condition stays True. Because Python checks the condition before the loop body runs on every pass, a while loop is described as an entry-controlled loop — if the condition is already False the very first time, the body never runs at all.
while condition:
# code
A simple illustration is filling a container with water until it is full:
water = 0
while water < 5:
print("Filling water...")
water += 1
Each pass through the loop prints a message and then increases water by one using water += 1; once water reaches 5, the condition water < 5 becomes False and the loop stops. This “increment a counter until a limit is reached” pattern is exactly how a dissolution test tracking loop works:
time = 0
while time <= 5:
print("Testing at minute:", time)
time += 1
Practical uses of the while loop include tracking drug release over a period of time and continuing an experiment or simulation until a defined endpoint is reached — both situations where the natural stopping point is a condition rather than a fixed, pre-known count.
Nested Loops: A Loop Inside a Loop
A nested loop is simply a loop placed inside the body of another loop. The inner loop runs through all of its own iterations every single time the outer loop advances by just one step. This arrangement is essential whenever a program needs to work with multi-dimensional data — grids, tables, or coordinate pairs, and in a pharmaceutical context, multiple samples drawn from multiple batches.
for outer_variable in sequence1:
for inner_variable in sequence2:
# code to execute
A classic demonstration is generating a small multiplication table:
for i in range(1, 3):
for j in range(1, 3):
print(i, "x", j, "=", i * j)
For every value that i takes (1, then 2), the inner loop runs completely through both of its own values of j before i is allowed to advance, producing:
1 x 1 = 1
1 x 2 = 2
2 x 1 = 2
2 x 2 = 4
Batch and Sample Testing With Nested Loops
The exact same nesting pattern models quality-control testing across multiple production batches, where each batch contains several samples that all need to be checked:
for batch in range(1, 3):
for sample in range(1, 4):
print("Batch", batch, "- Sample", sample)
This produces every sample number for batch 1, followed by every sample number for batch 2:
Batch 1 - Sample 1
Batch 1 - Sample 2
Batch 1 - Sample 3
Batch 2 - Sample 1
Batch 2 - Sample 2
Batch 2 - Sample 3
Nested loops of this kind are useful for generating tables, working with grid-like or repeating patterns, testing multiple samples across multiple batches, and carrying out quality-control analysis where every combination of two factors needs to be examined.
for Loop vs. while Loop: Choosing the Right Tool
| Aspect | For Loop | While Loop |
|---|---|---|
| Definition | Runs for a fixed number of repetitions | Keeps running as long as its condition is True |
| Best Use Case | Number of iterations is known beforehand | Number of iterations is not known in advance |
| Counter Management | Managed automatically by the loop itself | Must be initialised and updated manually |
| Risk of Infinite Loop | Low — the sequence naturally ends | Higher — if the condition is never updated to False |
| Typical Pharmacy Use | Listing a fixed set of drugs, tablets, or batch numbers | Tracking dissolution or drug release until an endpoint |
Worked Practice Problems
Problem 1: Print a List of Antibiotics (For Loop)
Write a program to print a list of 5 antibiotics used in pharmacy practice.
#List of antibiotics used in pharmacy practice
antibiotics = ["Amoxicillin", "Ciprofloxacin", "Azithromycin",
"Doxycycline", "Metronidazole"]
print("List of antibiotics:")
for drug in antibiotics:
print(drug)
Output:
List of antibiotics:
Amoxicillin
Ciprofloxacin
Azithromycin
Doxycycline
Metronidazole
Problem 2: Generate a Dosage Schedule (While Loop)
Write a program to generate dosage numbers for a patient (1 to 5 doses).
print("Patient dosage schedule:")
dose = 1
while dose <= 5:
print("Dose number:", dose)
dose += 1
Output:
Patient dosage schedule:
Dose number: 1
Dose number: 2
Dose number: 3
Dose number: 4
Dose number: 5
Frequently Asked Questions
What is the difference between range(5) and range(1, 6)?
range(5) generates the numbers 0, 1, 2, 3, 4 — it starts at 0 by default and stops before 5. range(1, 6) generates 1, 2, 3, 4, 5 — it starts at the first argument and stops before the second. Since pharmacy counting problems (tablet 1, tablet 2, …) usually start from 1, using range(1, n+1) is the standard way to count from 1 through n inclusive.
Can a while loop run zero times?
Yes. Because a while loop checks its condition before running the body even once, if the condition is already False at the very start (for example, starting a dissolution timer at time = 10 with the condition while time <= 5), the loop body never executes at all. This is different from some other languages that offer a “do-while” loop guaranteeing at least one execution — Python has no built-in do-while construct.
How many loops can be nested inside each other?
There is no strict limit imposed by Python, but each additional level of nesting multiplies the total number of iterations and makes the code harder to read and debug. For batch-and-sample testing, two levels (batch, then sample) is usually sufficient; going beyond three levels of nested loops is a sign that the logic may be better organised using functions or by restructuring the data.
Summary
Loops let a Python program repeat a task automatically instead of the programmer writing the same instructions over and over. The for loop is the right choice whenever the number of repetitions is already known, such as counting a fixed batch of tablets or listing a set number of drugs. The while loop is the right choice when repetition depends on a condition being met, such as continuing a dissolution test until a target time is reached, and it demands special care to ensure the loop’s condition is eventually updated to False. Nested loops combine two loops so that the inner loop completes fully for every single step of the outer loop, which is exactly the structure needed to test multiple samples drawn from multiple batches, a very common quality-control scenario in pharmaceutical manufacturing.
References
- Pharmacy Council of India (PCI), B.Pharm Regulations, NEP 2020 — BP101T: Basics of Python Programming for Pharmaceutical Sciences Syllabus
- Python Software Foundation, official documentation — “for Statements”, The Python Tutorial
- Python Software Foundation, official documentation — “The while statement”, The Python Language Reference
