Loops in Python for Pharmacy Students (Repeated Dosing, Patient Monitoring & Dataset Iteration)
Loops in Python are used to repeat a set of instructions multiple times. In pharmaceutical sciences, loops are essential for repeated dosing schedules, monitoring patients over time, and processing datasets.
๐ท What is a Loop?
A loop executes a block of code repeatedly until a condition is met.
๐ท for Loop
The for loop is used when the number of repetitions is known.
for i in range(5):
print("Dose administered")
๐ Repeated Dosing Example
for day in range(1, 6):
print(f"Day {day}: Give 500 mg Paracetamol")
This simulates a 5-day dosing schedule.
๐ท while Loop
The while loop runs as long as a condition is true.
count = 1
while count <= 3:
print("Monitoring patient")
count += 1
๐ Patient Monitoring Example
temperature = 101
while temperature > 100:
print("Patient has fever")
temperature -= 1
This loop continues until fever subsides.
๐ Dataset Iteration Example
doses = [500, 650, 400]
for dose in doses:
print("Dose:", dose)
This is useful for processing pharmaceutical datasets.
๐ท range() Function
range() generates a sequence of numbers.
range(1, 5)
Output: 1, 2, 3, 4
๐ง Memory Trick
- for โ Fixed repetition
- while โ Condition-based repetition
- range() โ Generates numbers
๐งช Practice Exercise
Create a program that:
- Prints dose for 7 days
- Monitors patient until temperature becomes normal
๐ MCQs
- Which loop is used for fixed repetitions?
a) while
b) for
c) if
d) def
Answer: b - while loop runs:
a) Once
b) Fixed times
c) Until condition is true
d) Never
Answer: c - range(1,5) gives:
a) 1 to 5
b) 1 to 4
c) 0 to 5
d) Error
Answer: b
โ FAQs
Why are loops important in pharmacy?
They help automate repeated tasks like dosing schedules and patient monitoring.
Which loop is better?
for loop for known repetitions, while loop for condition-based tasks.
๐ฅ Download Loop-Based Pharma Exercises
Includes dosing schedules and patient monitoring scenarios.
โก Next Topic: Break & Continue Statements โ
Unit 2 Blog:
- Conditional Statements (if, if-else, if-elif)
- Nested Conditions (clinical decision logic)
- Loops (for, while) โ repetition in pharma data (You are on this page)
- Break & Continue (control flow)
- Functions (definition, arguments, return values)
- Modular Programming (Dose + BMI calculator)
Question Bank Unit 2: Control Structures & Functions
For more details: Basics of Python Programming for Pharmaceutical Sciences