Unit I · Basics of Python Programming for Pharmaceutical Sciences (BP101T) · As per PCI B.Pharmacy Syllabus, NEP 2020
Every Python program, no matter how advanced, is built out of a handful of foundational ideas: places to store data, categories of data, ways to convert between those categories, symbols that act on the data, and a way to talk to the person running the program. In pharmaceutical computing this foundation is not academic decoration — a dosage calculator, a pharmacokinetic model, or a script that screens adverse drug reaction (ADR) records all begin with variables holding patient weight, drug concentration, or reaction counts. This article walks through Python variables, data types, type casting, operators, and input/output (I/O) operations in the depth expected for the BP101T examination, with pharmacy-oriented examples throughout.

Variables: Labelled Containers for Data
A variable in Python is a name used to store a value, much like a labelled container on a pharmacy shelf that holds a specific substance which can be read, checked, or replaced as needed. When you write count = 10, Python creates an object holding the value 10 and lets the name count refer to it. From that point on, using count anywhere in the program retrieves that stored value.
drug_name = "Paracetamol"
dose_mg = 500
unit_price = 2.50
in_stock = True
print(drug_name, dose_mg, unit_price, in_stock)
Dynamic Typing
Unlike languages such as C or Java, Python does not require you to declare a variable’s type in advance. The interpreter works out the type automatically from the value assigned to it. This behaviour is called dynamic typing, and it means the same variable name can hold different kinds of values at different points in a program — something a beginner must use carefully, since reusing a name for an unrelated type can make code harder to read.
x = 10 # x is currently an integer
x = "Hello" # now x refers to a string instead
Rules for Naming Variables
Python enforces a small set of rules when you choose variable names:
- A variable name must begin with a letter (a–z, A–Z) or an underscore (
_). - A variable name cannot begin with a digit (so
1doseis invalid, butdose1is fine). - A variable name should not contain special characters such as
@,#, or$. - A variable name cannot be one of Python’s reserved keywords, such as
if,for, orwhile. - Variable names are case-sensitive, so
doseandDoseare treated as two completely different variables.
Python Data Types
A data type tells Python what kind of value a variable is holding, and, just as with variable declaration, you never have to state the type yourself — Python infers it from the value you assign. Python organises its built-in types broadly into numeric types, boolean, sequence types (string, list, tuple), sets, and dictionaries. For BP101T, the four types below are the essential starting point.
1. Integer (int)
An integer stores a whole number with no decimal component — useful for counts such as the number of tablets dispensed or patients enrolled in a trial.
tablets_dispensed = 30
adverse_events = -0 # counts are never negative, shown only for illustration
2. Float (float)
A float stores a number that includes a decimal point, which is exactly the kind of value pharmaceutical calculations depend on — drug concentrations, body weight in kilograms, or a calculated dose in milligrams per kilogram.
plasma_conc_mg_l = 12.75
patient_weight_kg = 68.4
3. String (str)
A string stores text, or any sequence of characters, and can be written using either single quotes or double quotes. Drug names, batch numbers, and patient identifiers are typically stored as strings.
drug_name = "Ibuprofen"
batch_no = 'BN2024A17'
4. Boolean (bool)
A boolean can only ever hold one of two values, True or False, and is used whenever a program needs to make a decision or evaluate a condition — for instance, flagging whether a patient’s dose exceeds a safe threshold.
max_safe_dose = 1000
prescribed_dose = 1200
exceeds_limit = prescribed_dose > max_safe_dose
print(exceeds_limit) # True
Checking a Variable’s Data Type
The built-in type() function reports the data type currently stored in a variable, which is useful for debugging when a calculation produces an unexpected result.
dose_mg = 500
print(type(dose_mg)) # <class 'int'>
conc = 3.14
print(type(conc)) # <class 'float'>
drug = "Aspirin"
print(type(drug)) # <class 'str'>
is_expired = False
print(type(is_expired)) # <class 'bool'>
type() for a given expression, or to identify which data type is best suited to a given pharmaceutical quantity (e.g., “which data type would you use to store a patient’s body temperature?”). Remember: whole counts use int, anything with a decimal (weight, concentration, dose per kg) uses float, and yes/no clinical flags use bool.Type Casting (Type Conversion)
Type casting, also called type conversion, is the act of changing a value from one data type into another. This matters constantly in pharmacy scripts because raw data entered by a user, or read from a file, often arrives in the wrong type for the calculation you need to perform.
Common Type Conversion Functions
int()— converts a given value into an integer.float()— converts a given value into a floating-point number.str()— converts a given value into a string.bool()— converts a given value into a boolean.
dose_str = "250"
dose_int = int(dose_str) # "250" -> 250
weight = 68
weight_f = float(weight) # 68 -> 68.0
conc = 5.9
conc_int = int(conc) # 5.9 -> 5 (decimal part is discarded)
count = 12
count_label = str(count) # 12 -> "12"
Important Points About Type Casting
- Type casting makes it possible to carry out operations that involve more than one data type — for example, adding a number entered as text to a number already stored as a float.
- Some conversions cause a loss of data. Converting a float to an int discards the decimal portion rather than rounding it, so
int(5.9)gives5, not6. - Not every conversion is valid. Attempting
int("high dose")will raise aValueError, because the text cannot be interpreted as a whole number.
Implicit vs Explicit Type Conversion
In implicit type conversion, Python automatically changes one data type into another without the programmer asking for it, typically when an expression mixes numeric types.
tablets = 5
strength_mg = 62.5
total = tablets + strength_mg
print(total) # 67.5 -- Python silently promotes tablets to a float
In explicit type conversion, the programmer deliberately converts a value’s data type by calling a conversion function such as int() or float(), exactly as shown in the examples above.
Basic Operators in Python
Operators are special symbols that act on variables and values to carry out a task. They are essential for performing arithmetic calculations, comparing values, and making logical decisions within a program — the building blocks of any dosage calculator or data-filtering script.
Arithmetic Operators
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 5 | 15 |
| – | Subtraction | 10 – 5 | 5 |
| * | Multiplication | 10 * 5 | 50 |
| / | Division | 10 / 3 | 3.33 (always float) |
| % | Modulus | 10 % 3 | 1 (remainder) |
| ** | Exponentiation | 2 ** 3 | 8 |
| // | Floor Division | 10 // 3 | 3 (rounded down) |
A practical pharmacy use of the modulus operator: if a pack contains 10 tablets and a prescription needs 47 tablets, 47 // 10 tells you how many full packs are needed (4), while 47 % 10 tells you how many loose tablets remain (7).
required_tablets = 47
pack_size = 10
full_packs = required_tablets // pack_size
leftover = required_tablets % pack_size
print("Full packs:", full_packs, "Loose tablets:", leftover)
Comparison Operators
Comparison operators compare two values against each other, and the outcome is always either True or False: == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to).
prescribed_dose = 500
max_safe_dose = 650
print(prescribed_dose <= max_safe_dose) # True -- safe to dispense
Logical Operators
Logical operators — and, or, and not — combine two or more conditions into a single expression. and is True only when both joined conditions are true; or is True when at least one is true; not reverses a condition’s truth value.
age = 65
weight_kg = 55
is_elderly_and_light = age > 60 and weight_kg < 60
print(is_elderly_and_light) # True -- may need dose adjustment
Assignment Operators
Assignment operators store or update the value held by a variable. Beyond the plain =, Python offers combined forms such as +=, -=, *=, /=, %=, **=, and //=, each of which performs the operation and reassigns the result back to the same variable in one step.
stock_count = 100
stock_count -= 12 # equivalent to stock_count = stock_count - 12
print(stock_count) # 88
Membership and Identity Operators
Membership operators, in and not in, test whether a value exists inside a sequence such as a string or a list — for example, checking whether a particular drug appears in a formulary list. Identity operators, is and is not, check whether two variables refer to the exact same object in memory, which is a different question from whether they merely hold equal values.
formulary = ["Paracetamol", "Ibuprofen", "Amoxicillin"]
print("Amoxicillin" in formulary) # True
Input and Output Operations
Input and output operations let a program communicate with the person using it: input refers to collecting data from the user, while output refers to displaying results back to the user — the two operations that turn a static script into an interactive dosage calculator.
Taking Input with input()
The input() function reads a value typed at the keyboard. Whatever the user types is always stored by Python as a string, regardless of whether it looks like a number, so numeric input must be explicitly type-cast before it can be used in arithmetic.
patient_name = input("Enter patient name: ")
weight_kg = float(input("Enter patient weight in kg: "))
dose_per_kg = float(input("Enter dose per kg (mg): "))
total_dose = weight_kg * dose_per_kg
print(patient_name, "requires a total dose of", total_dose, "mg")
Displaying Output with print()
Displaying data or results back to the user is called an output operation, and in Python this is done chiefly with the print() function. It can accept several values in a single call, separated by commas, and will display them one after another separated by spaces. The end parameter controls what is written after the given output instead of the default newline, and f-strings offer a convenient way to weave variable values directly into a piece of text.
drug = "Amoxicillin"
dose = 500
print(f"Prescribed drug: {drug}, Dose: {dose} mg")
Escape Characters
Escape characters are special two-character sequences, starting with a backslash, that let you include characters in a string’s output that would otherwise be difficult to type directly, or that control how the output is laid out — for instance \n for a new line, \t for a tab, and \" for a literal double quote inside a double-quoted string.
print("Drug:\tParacetamol\nDose:\t500 mg")
input() always needs to be wrapped in int() or float() before arithmetic. The correct explanation is that input() returns a string type by default regardless of what the user types, and Python cannot add or multiply strings the way it can numbers — attempting arithmetic directly on unconverted input raises a TypeError.Frequently Asked Questions
Why does 10 / 3 give a decimal answer while 10 // 3 does not?
The single slash / is true division in Python 3 and always returns a float, even when the numbers divide evenly. The double slash // is floor division: it performs the division and then rounds the result down to the nearest whole number, discarding any remainder.
What happens if I try int(“50mg”)?
This raises a ValueError, because the string contains non-numeric characters and cannot be interpreted as a whole number. Only strings that represent a clean number, like "50", can be converted successfully with int().
Is dose_mg the same variable as Dose_mg?
No. Python variable names are case-sensitive, so dose_mg and Dose_mg are treated as two entirely separate variables that can hold two different values at the same time.
Summary
Variables give Python programs named places to store data, and Python’s dynamic typing means the type of a variable is inferred automatically from its value rather than declared up front. The four core data types — integer, float, string, and boolean — cover the vast majority of values a pharmacy script needs to handle, and type casting functions (int(), float(), str(), bool()) allow safe conversion between them, whether implicitly by Python or explicitly by the programmer. Arithmetic, comparison, logical, assignment, membership, and identity operators provide the tools to calculate, compare, and make decisions on this data, while input() and print() complete the loop by letting a program collect information from, and report results back to, the user.
References
- Pharmacy Council of India (PCI), B.Pharm Regulations, NEP 2020 – BP101T Syllabus (Basics of Python Programming for Pharmaceutical Sciences)
- Python Software Foundation, “The Python Tutorial — An Informal Introduction to Python” – docs.python.org/3/tutorial/introduction.html
- Python Software Foundation, “Built-in Functions” (covers int(), float(), str(), bool(), input(), print(), type()) – docs.python.org/3/library/functions.html
