Unit II · Basics of Python Programming for Pharmaceutical Sciences (BP101T) · As per PCI B.Pharmacy Syllabus, NEP 2020
Every pharmacy program, whether it is checking a patient’s fever, flagging a drug allergy, or classifying a blood sugar reading, has to make a decision at some point. Conditional statements are what give a Python program that decision-making ability. In this post, we cover the three forms of conditional logic that Python provides — if, if-else, and if-elif-else — and then extend them into nested conditions, where one decision is made only after another has already been confirmed. Throughout, we will connect each construct to a real pharmacy or clinical scenario, since this is exactly how the BP101T syllabus expects students to apply programming to pharmaceutical problem-solving.

Why Pharmacy Programs Need Control Structures
Without any decision-making ability, a Python script would simply execute every line in a fixed, linear order from top to bottom, exactly the way it is written, regardless of what data the program receives. That is rarely useful in a real pharmacy or healthcare context. A dosage calculator needs to behave differently for a senior citizen than for a young adult; a dispensing system needs to check for allergies before releasing a drug; a diagnostic tool needs to sort a lab value into one of several risk categories. Control structures are the building blocks that let a script branch its logic based on the values that variables hold when the program actually runs. Python organizes this decision-making into conditional statements and, when combined with loops, gives programmers everything needed to model these kinds of real clinical workflows.
The if Statement: A Single Decision
The simplest conditional construct in Python is the if statement. It tests a condition — an expression that evaluates to either True or False — and runs its indented block of code only when that condition is True. If the condition turns out False, the block is skipped entirely and the program simply moves on to whatever comes next.
x = 10
if x > 5:
print("x is greater than 5")
Since 10 is indeed greater than 5, this prints x is greater than 5. Now consider the same logic applied to a pharmacy setting: a program that checks a patient’s age to decide whether a dosage adjustment is warranted.
# Program to check dosage based on age
age = int(input("Enter patient age: "))
if age >= 60:
print("Reduced dosage recommended for senior citizen")
When the entered age is 65, the condition age >= 60 evaluates to True, so the program prints Reduced dosage recommended for senior citizen. If the entered age had been, say, 30, nothing would print at all, because there is no alternative path defined — that gap is exactly what the if-else statement solves.
The if-else Statement: Two Alternative Paths
The if-else statement gives a program two branches instead of one. When the condition is True, the if block executes; when it is False, the else block executes instead. Exactly one of the two blocks will always run.
if condition:
# code runs if condition is True
else:
# code runs if condition is False
A clinical example makes this concrete. Suppose a program needs to flag fever based on a recorded body temperature in degrees Celsius:
temperature = 38.0
if temperature > 37.5:
print("Patient has fever")
else:
print("Normal temperature")
Because 38.0 is greater than the 37.5°C threshold, the output is Patient has fever. Had the temperature been 36.8, the else branch would have run instead, printing Normal temperature. This pattern — comparing a measured value against a clinically meaningful cutoff — recurs throughout pharmaceutical data processing, whether the value is a temperature, a blood pressure reading, or a lab result.
The if-elif-else Statement: Choosing Among Many Outcomes
Real classification problems rarely have only two outcomes. A student’s marks might fall into grade A, B, C, or fail; a blood sugar reading might be normal, pre-diabetic, or diabetic. The if-elif-else statement (elif is short for “else if”) lets a program check any number of conditions in sequence, running the block for the first one that turns out True and falling back to else only if none of them match.
if condition1:
# code if condition1 is True
elif condition2:
# code if condition2 is True
elif condition3:
# code if condition3 is True
else:
# code if all conditions are False
Python evaluates these top to bottom and stops at the first match, so the order in which you write the conditions matters. Here is a grading example:
marks = 75
if marks >= 90:
print("Grade A")
elif marks >= 70:
print("Grade B")
elif marks >= 50:
print("Grade C")
else:
print("Fail")
Since 75 fails the first test (>= 90) but passes the second (>= 70), Python prints Grade B and skips every branch after it. Now apply the identical structure to a clinical pharmacy problem — classifying a fasting blood sugar reading:
sugar = 180
if sugar > 200:
print("High risk (Diabetes)")
elif sugar >= 140:
print("Pre-diabetic condition")
else:
print("Normal blood sugar level")
A reading of 180 mg/dL is not above 200, so the first branch is skipped, but it is at least 140, so Pre-diabetic condition is printed. Notice how the same three-way branching logic that classifies exam marks also classifies a physiological measurement — this is the essence of applying general-purpose programming constructs to domain-specific problems.
Nested Conditions: An if Inside Another if
Sometimes one condition is only meaningful to check after another condition has already been confirmed. This is where nested conditions come in — placing one if, if-else, or if-elif-else statement inside the body of another. In plain terms, it is “an if inside another if.”
if condition1:
if condition2:
statement1
else:
statement2
else:
statement3
Python evaluates nested conditions in a strict order. First, the outer if condition is checked. Only if the outer condition is True does control move on to check the inner condition, and the final outcome then depends on whether that inner condition is True or False. If the outer condition is False to begin with, the entire inner block is skipped entirely, and the outer else branch (if one exists) runs instead. The inner condition is never even evaluated in that case.
Nested Conditions in a Pharmacy Context
Drug dispensing is a natural fit for nested logic, because dispensing safely genuinely does depend on two separate facts being true together: a valid prescription and the absence of a relevant allergy.
prescription = "yes"
allergy = "no"
if prescription == "yes":
if allergy == "no":
print("Medicine can be dispensed")
else:
print("Avoid drug due to allergy")
else:
print("Prescription required")
Here, the outer condition checks whether a prescription exists at all. Only once that is confirmed does the program bother checking the allergy status — there would be no point checking for an allergy if there was never a valid prescription in the first place. With a valid prescription and no allergy, the output is Medicine can be dispensed. A similar structure works well for flagging hypertension risk by nesting a blood pressure check inside an age check for senior patients, since “senior with high BP” is a meaningfully different risk category from “senior with normal BP” or “non-senior.”
Complete Programming Examples
The following four short programs bring together everything covered above, moving from a single if to a fully nested safety check — the same progression BP101T practical exams typically test.
1. Drug Allergy Check Using a Simple if
# Drug allergy check
allergy = input("Enter patient allergy: ")
if allergy.lower() == "penicillin":
print("Warning: Avoid penicillin-based drugs")
Note the use of .lower() here — converting the input to lowercase before comparing it means the check works whether the patient or pharmacist types “Penicillin”, “PENICILLIN”, or “penicillin”. This kind of input normalization is a small habit that prevents a large class of bugs in real data entry.
2. Fever Detection Using if-else
# Fever detection
temp = float(input("Enter body temperature: "))
if temp > 37.5:
print("Patient has fever")
else:
print("Normal body temperature")
3. Blood Sugar Classification Using if-elif-else
# Blood sugar classification
sugar = float(input("Enter blood sugar level: "))
if sugar > 200:
print("Diabetic condition")
elif sugar >= 140:
print("Pre-diabetic condition")
else:
print("Normal blood sugar level")
4. Drug Dispensing Safety Check Using Nested if
# Drug dispensing safety check
prescription = input("Is prescription valid (yes/no): ")
allergy = input("Enter allergy (none/penicillin): ")
if prescription.lower() == "yes":
if allergy.lower() == "none":
print("Medicine can be safely dispensed")
else:
print("Do NOT dispense: Patient has allergy")
else:
print("Prescription required")
This last program mirrors, in miniature, exactly the kind of two-factor safety check a real dispensing system performs before releasing medication — confirming authorization first, then screening for contraindications.
Comparing the Three Conditional Forms
| Construct | Number of Paths | When to Use | Pharmacy Example |
|---|---|---|---|
if |
One (run or skip) | Only one outcome needs special handling | Warn only if allergy is penicillin |
if-else |
Two (mutually exclusive) | Exactly two possible outcomes | Fever vs. normal temperature |
if-elif-else |
Three or more | More than two categories to classify into | Normal / pre-diabetic / diabetic |
Nested if |
Conditional on a prior condition | One check is only relevant after another passes | Allergy check only after prescription is confirmed |
Frequently Asked Questions
What happens if I use indentation incorrectly inside an if block?
Python relies entirely on indentation to define which statements belong inside a block — there are no curly braces as in C or Java. Inconsistent indentation (mixing tabs and spaces, or indenting a line by the wrong amount) raises an IndentationError and stops the program before it runs at all. Always use a consistent number of spaces (commonly four) for each level of nesting.
Can I have an elif without a final else?
Yes. The else branch is always optional in Python. You can write any number of elif clauses after an if without ever adding an else; if none of the conditions match, the program simply skips the entire chain and continues with the next statement. However, in dispensing or diagnostic logic it is usually good practice to include a final else as a catch-all, so that unexpected input does not silently fall through unhandled.
How deep can conditions be nested in Python?
There is no hard syntactic limit on nesting depth, but readability suffers quickly beyond two or three levels. When pharmacy logic needs to check many factors together (age, allergy, prescription validity, dosage form, and so on), it is usually cleaner to combine conditions using logical operators such as and and or inside a single if, or to break the logic into separate functions, rather than nesting if-statements many levels deep.
Summary
Conditional statements are what let a Python program respond differently depending on the data it receives, and they map directly onto the kind of decision-making pharmacists and pharmacy software perform constantly. The if statement handles a single yes/no branch, if-else handles exactly two outcomes, and if-elif-else handles classification into three or more categories, always stopping at the first condition that matches. Nested conditions extend this further, letting one check be evaluated only after another has already passed — a natural fit for two-step safety checks such as verifying a prescription before screening for an allergy. Mastering these constructs is the foundation for everything that follows in Unit II, including loops and functions, both of which routinely rely on conditional logic inside their own bodies.
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 — “More Control Flow Tools”, The Python Tutorial
- Python Software Foundation, official documentation — “The if statement”, The Python Language Reference
