Topic 3 / 5📁 Unit Index
BP101T • Unit 1Topic 3 of 5⏱ 5 Min Read

3. Variables, Primitive Data Types & Type Casting in Lab Calculations

💡 Core Concept & Clinical Context:

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.

Pharmaceutical Code & Calculation Examples:
1Laboratory Formulation Record with Type Casting

Capturing raw analytical data from user input, casting it to float values, and calculating percentage drug purity.

example_1.py
# 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}")
Comparison & Reference Table:
Data TypePython SyntaxPharmaceutical ExampleMemory Characteristics
Integer (int)count = 100Number of vials, tablets, patient age in yearsArbitrary precision whole numbers
Float (float)ph_val = 7.35Drug concentration (mg/mL), body weight (kg), melting point (°C)64-bit double precision (IEEE 754)
String (str)drug = 'Aspirin'Drug names, chemical formulas, batch codesImmutable sequence of Unicode characters
Boolean (bool)passed = TrueSterility pass/fail flags, QC inspection checksSubtype of integer (True = 1, False = 0)
📋Key Takeaways & Exam Highlights
  • 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.
🇮🇳 हिन्दी व्याख्या (3. Variables, Primitive Data Types & Type Casting in Lab Calculations)

वेरिएबल कंप्यूटर मेमोरी में डेटा स्टोर करने का नाम है। int (पूर्ण संख्या), float (दशमलव), str (टेक्स्ट) और bool (सत्य/असत्य) मुख्य डेटा टाइप्स हैं। इनपुट से मिले टेक्स्ट को float() या int() से बदला जाता है।

Need to see the complete unit overview, solved numericals, or exam checklist?Open Unit 1 Overview Hub →