3. Variables, Primitive Data Types & Type Casting in Lab Calculations
A variable is a named reference to a memory location that stores data. In Python, variables are created automatically when you assign a value using the assignment operator (=). Python has four fundamental primitive data types: int (whole numbers like tablet counts), float (decimal numbers like pH or weight in grams), str (text strings like drug names), and bool (logical True or False flags). Because console input via `input()` always returns a string, explicit type casting (converting string to float or integer) is mandatory before performing mathematical operations.
Capturing raw analytical data from user input, casting it to float values, and calculating percentage drug purity.
# Capturing analytical assay inputs
drug_name = "Amoxicillin Trihydrate" # str
batch_number = "BT2026-09" # str
label_claim_mg = 500.0 # float
tested_amount_str = "492.5" # str from instrument
# Explicit Type Casting
tested_amount_mg = float(tested_amount_str) # Cast str to float
purity_percent = (tested_amount_mg / label_claim_mg) * 100.0
is_within_limits = 90.0 <= purity_percent <= 110.0 # bool
print(f"Drug: {drug_name} (Batch: {batch_number})")
print(f"Assay: {purity_percent:.2f}% | IP Standard Met: {is_within_limits}")| Data Type | Python Syntax | Pharmaceutical Example | Memory Characteristics |
|---|---|---|---|
| Integer (int) | count = 100 | Number of vials, tablets, patient age in years | Arbitrary precision whole numbers |
| Float (float) | ph_val = 7.35 | Drug concentration (mg/mL), body weight (kg), melting point (°C) | 64-bit double precision (IEEE 754) |
| String (str) | drug = 'Aspirin' | Drug names, chemical formulas, batch codes | Immutable sequence of Unicode characters |
| Boolean (bool) | passed = True | Sterility pass/fail flags, QC inspection checks | Subtype of integer (True = 1, False = 0) |
- Variable Naming Rules: Must start with a letter (a-z, A-Z) or underscore (_); cannot start with a digit; case-sensitive (`dose` != `Dose`).
- Dynamic Typing: The variable's type is determined by the assigned object at runtime.
- Type Casting Functions: `int()` converts to whole number, `float()` converts to decimal, `str()` converts to text, `bool()` converts to boolean.
- F-Strings: Modern string formatting syntax `f'Dose: {val:.2f} mg'` allows clean, formatted output.
वेरिएबल कंप्यूटर मेमोरी में डेटा स्टोर करने का नाम है। int (पूर्ण संख्या), float (दशमलव), str (टेक्स्ट) और bool (सत्य/असत्य) मुख्य डेटा टाइप्स हैं। इनपुट से मिले टेक्स्ट को float() या int() से बदला जाता है।