Lesson · 40 min · Free
Python Variables & Data Types
Python Variables & Data Types 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; } code { fo
Python Variables & Data Types
Welcome to the first core programming lesson in "Python for Pharmaceutical Research." Today, we'll delve into the fundamental building blocks of any programming language: variables and data types. Understanding these concepts is crucial for effectively storing, manipulating, and interpreting the diverse datasets encountered in pharmaceutical research, from patient demographics to genomic sequences and molecular structures. In essence, variables are named storage locations in a computer's memory. Think of them as labeled containers where you can place different kinds of information. In Python, you don't need to explicitly declare a variable's type before using it; Python is dynamically typed, meaning it infers the type of data stored in the variable at the time of assignment. This flexibility makes Python highly approachable, but it also necessitates an understanding of the underlying data types to avoid unexpected behavior.
Understanding Python Data Types
Python categorizes data into various types, each with its own characteristics and applicable operations. For pharmaceutical research, some of the most commonly encountered types include: Numeric Types: int (Integers): Whole numbers, e.g., 10 , -5 , 0 . Useful for counts of patients, dose units, or experimental replicates. float (Floating-point numbers): Numbers with decimal points, e.g., 3.14 , 2.5e-3 (scientific notation). Essential for concentrations, p-values, molecular weights, or kinetic constants. Text Type: str (Strings): Sequences of characters enclosed in single or double quotes, e.g., "Acetaminophen" , 'ATGCGGTA' . Used for drug names, gene sequences, patient IDs, or textual descriptions. Boolean Type: bool (Booleans): Represents one of two values: True or False . Crucial for logical operations, conditional statements (e.g., "is patient allergic?", "is drug active?"). Collection Types (Introduced later in more detail): list : Ordered, mutable (changeable) sequences of items, e.g., [10, 20, 30] , ['DrugA', 'DrugB'] . tuple : Ordered, immutable (unchangeable) sequences of items, e.g., (10, 20, 30) . dict (Dictionary): Unordered collections of key-value pairs, e.g., {'patient_id': 'P001', 'age': 45} . Let's look at some examples of assigning values to variables and checking their types using the built-in type() function. # Assigning an integer to a variable patient_count = 150 print(f"Variable: patient_count, Value: {patient_count}, Type: {type(patient_count)}") # Assigning a float to a variable drug_concentration_mM = 2.75 print(f"Variable: drug_concentration_mM, Value: {drug_concentration_mM}, Type: {type(drug_concentration_mM)}") # Assigning a string to a variable compound_name = "Aspirin" print(f"Variable: compound_name, Value: {compound_name}, Type: {type(compound_name)}") # Assigning a boolean to a variable is_active_compound = True print(f"Variable: is_active_compound, Value: {is_active_compound}, Type: {type(is_active_compound)}") # Reassigning a variable (Python is dynamically typed) patient_count = "One hundred fifty" # Now patient_count holds a string print(f"Variable: patient_count (reassigned), Value: {patient_count}, Type: {type(patient_count)}") Notice how patient_count initially held an integer, but then we could reassign it to hold a string. While this flexibility is powerful, it's generally good practice to keep variable types consistent within a logical block of code to maintain clarity and prevent errors. Variables can also be used in expressions and operations. Understanding the data types involved in an operation is crucial, as attempting to perform incompatible operations (e.g., adding a string and an integer directly) will result in a Python error. # Numeric operations dose_mg_per_kg = 5.0 patient_weight_kg = 75.2 total_dose_mg = dose_mg_per_kg * patient_weight_kg print(f"Total dose needed: {total_dose_mg} mg") print(f"Type of total_dose_mg: {type(total_dose_mg)}") # It's a float # String concatenation gene_sequence_part1 = "ATGC" gene_sequence_part2 = "GGTA" full_gene_sequence = gene_sequence_part1 + gene_sequence_part2 print(f"Full gene sequence: {full_gene_sequence}") print(f"Type of full_gene_sequence: {type(full_gene_sequence)}") # It's a string # Attempting incompatible operation (will cause an error if uncommented) # error_example = "Number of trials: " + 10 # print(error_example) # This would raise a TypeError The example above demonstrates basic arithmetic with numbers and concatenation with strings. The commented-out line illustrates a common TypeError that occurs when Python tries to perform an operation on incompatible data types. You would need to convert the integer 10 to a string (e.g., str(10) ) before concatenating it with another string.
Key Takeaways
Variables are named storage locations for data. Python is dynamically typed; you don't declare a variable's type explicitly. Common data types include int (integers), float (decimal numbers), str (text), and bool (True/False). The type() function is useful for inspecting the data type of a variable. Understanding data types is critical for performing correct operations and avoiding errors.
Practice Exercise: Clinical Trial Data
Imagine you are analyzing preliminary data from a small clinical trial. Create Python variables to store the following information, choosing the most appropriate data type for each. Then, print the value and type of each variable: The name of the investigational drug: "PharmaCel" The number of participants in the trial: 25 The average age of participants: 58.7 years Whether the trial is double-blinded: True A participant ID: "P-007" The observed efficacy rate (as a percentage, e.g., 72.5%): 72.5 After creating these variables, perform a simple calculation: if each participant receives a fixed dose of 150 mg of PharmaCel daily, calculate the total daily amount of drug (in mg) administered across all participants. Store this result in a new variable and print its value and type.
Watch the full lesson — free
This topic is part of Python for Pharmaceutical Research, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →