Lesson · 40 min · Free
Python Variables
Python Variables body { font-family: sans-serif; line-height: 1.6; margin: 20px; } h1, h2 { color: #2c3e50; } pre { background-color: #ecf0f1; padding: 15px; border-radius: 5px; overflow-x: auto; margin-bottom: 20px; } c
Python Variables
Welcome to this crucial lesson on Python Variables! In any programming language, variables are fundamental for storing and manipulating data. Think of a variable as a labeled container or a named storage location in your computer's memory where you can keep a piece of information. This information can be anything from a patient's age to a drug's concentration, or even the result of a complex pharmacokinetic calculation. In Python, creating a variable is incredibly straightforward. You don't need to explicitly declare its type (like int or float as in some other languages); Python dynamically infers the type based on the value you assign to it. This flexibility is one of Python's strengths, making it quicker to write and test code, which is particularly beneficial in research and rapid prototyping scenarios common in pharma and biotech. The assignment operator in Python is the single equals sign ( = ). When you write variable_name = value , you are telling Python to create a variable named variable_name and store value inside it. You can then refer to this stored value simply by using the variable's name.
Understanding Variable Assignment and Data Types
Let's look at some examples relevant to our field. We can store numerical data, text data (strings), and even true/false values (booleans) in variables. Python handles these different data types seamlessly. # Storing patient demographic data patient_id = "P102345" # String type patient_age_years = 68 # Integer type patient_weight_kg = 72.5 # Float type (decimal number) is_diabetic = True # Boolean type # Storing drug information drug_name = "Insulin Glargine" drug_dose_mg = 100 half_life_hours = 24.0 # Printing the values to see what's stored print(f"Patient ID: {patient_id}") print(f"Patient Age: {patient_age_years} years") print(f"Patient Weight: {patient_weight_kg} kg") print(f"Is Diabetic: {is_diabetic}") print(f"Drug Name: {drug_name}") print(f"Drug Dose: {drug_dose_mg} mg") print(f"Half-life: {half_life_hours} hours") Variables can also be updated. When you assign a new value to an existing variable, the old value is overwritten. This is a common operation when, for example, tracking changes in a patient's condition or updating experimental parameters. # Initial measurement of a compound's concentration initial_concentration_mM = 15.75 print(f"Initial concentration: {initial_concentration_mM} mM") # After a reaction, the concentration changes initial_concentration_mM = 12.30 print(f"New concentration after reaction: {initial_concentration_mM} mM") # You can also use variables in calculations and assign the result to another variable volume_ml = 500 moles = initial_concentration_mM * volume_ml / 1000 # Convert mM to M and ml to L for moles print(f"Moles of compound in solution: {moles} moles") Choosing meaningful variable names is crucial for writing readable and maintainable code, especially in complex scientific projects. Avoid single-letter variable names unless their context is absolutely clear (e.g., x, y in mathematical formulas). Python typically uses snake_case for variable names, where words are lowercase and separated by underscores.
Key Takeaways
Variables are named storage locations for data in your program. You assign values to variables using the = operator (e.g., my_variable = 10 ). Python is dynamically typed, meaning you don't declare a variable's type; it's inferred from the assigned value. Variables can store different data types: integers, floats, strings, booleans, and more. Variable values can be updated by re-assigning them. Use descriptive snake_case variable names for clarity and readability.
Practice Exercise: Drug Dispensing Simulation
Imagine you are developing a simple script to manage drug dispensing. Create three variables: drug_inventory_units : An integer representing the current stock of a drug (e.g., 500 units). patient_dose_units : An integer representing the dose for a single patient (e.g., 25 units). patients_to_treat : An integer representing the number of patients you need to treat (e.g., 10 patients). Then, write Python code to calculate the total units needed for all patients and update the drug_inventory_units variable to reflect the remaining stock after dispensing. Finally, print the remaining inventory.
Watch the full lesson — free
This topic is part of The Complete Python Bootcamp: Basics to Pharma & Data Science, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →