Lesson · 40 min · Free
Python Data Types Basics
Python Data Types Basics body { font-family: sans-serif; line-height: 1.6; margin: 20px; } h1, h2 { color: #2c3e50; } pre, code { background-color: #ecf0f1; padding: 10px; border-radius: 5px; overflow-x: auto; font-famil
Python Data Types Basics
Welcome to the "Python Data Types Basics" lesson, a foundational component of your "Python Programming - Basics" course. In any programming language, data types are crucial for understanding how information is stored and manipulated. For pharmacy and biotech students, this understanding is vital for tasks ranging from managing patient data and drug inventories to analyzing genomic sequences and experimental results. Python's dynamic typing system makes it relatively easy to work with data, but a clear grasp of its fundamental types will empower you to write robust and efficient code for scientific applications. At its core, a data type classifies the kind of value a variable can hold. This classification dictates what operations can be performed on that value and how it behaves within your program. Python provides several built-in data types, each suited for different purposes. We will focus on the most common and essential ones for scientific computing: numeric types (integers, floats), booleans, and strings.
Fundamental Python Data Types for Scientific Applications
Numeric Types: Integers and Floats
Numeric types are fundamental for quantitative analysis, dosage calculations, statistical modeling, and representing experimental measurements. Python distinguishes between two primary numeric types: Integers ( int ): These represent whole numbers, positive or negative, without decimal points. In Python, integers have arbitrary precision, meaning they can store very large numbers without overflow issues, which is beneficial for calculations involving large counts or indices. Floating-Point Numbers ( float ): These represent real numbers, including those with fractional parts. Floats are essential for measurements, concentrations, probabilities, and any data that requires decimal precision. Python's floats are typically implemented using double-precision (64-bit) floating-point numbers, offering a good balance of range and precision for most scientific tasks. Here's an example demonstrating basic numeric types and operations: # Example of Numeric Data Types in Pharmacy/Biotech Context # Integer examples patient_id = 12345 number_of_samples = 250 drug_batch_size = 100000 # Float examples drug_concentration_mM = 0.525 patient_weight_kg = 72.8 reaction_temperature_celsius = 37.5 p_value = 0.0012 # Basic arithmetic operations total_dose_mg = 25 * 3.5 # 25 tablets, each 3.5 mg print(f"Patient ID: {patient_id}") print(f"Drug concentration: {drug_concentration_mM} mM") print(f"Total dose: {total_dose_mg} mg") print(f"Type of patient_id: {type(patient_id)}") print(f"Type of drug_concentration_mM: {type(drug_concentration_mM)}") # Output: # Patient ID: 12345 # Drug concentration: 0.525 mM # Total dose: 87.5 mg # Type of patient_id: <class 'int'> # Type of drug_concentration_mM: <class 'float'>
Boolean Type: True/False Logic
The Boolean type ( bool ) represents truth values: True or False . This type is fundamental for control flow (e.g., if statements, loops), conditional logic, and representing binary states. In biotech, this could be whether a gene is expressed (True/False), if a sample passed quality control, or if a drug interaction is present.
String Type: Textual Data
Strings ( str ) are sequences of characters used to represent text. They are immutable, meaning once created, their content cannot be changed. Strings are enclosed in single quotes ( '...' ), double quotes ( "..." ), or triple quotes ( '''...''' or """...""" ) for multi-line strings. For pharmacy and biotech, strings are indispensable for storing patient names, drug names, gene sequences, experimental notes, file paths, and any other textual information. Let's look at an example combining booleans and strings: # Example of Boolean and String Data Types # String examples patient_name = "Alice Smith" gene_sequence = "ATGCGTACGTACGTAGCTAGCTAGCTACGTAGCTACGTAGCT" drug_name = "Paracetamol" lab_status = "In Progress" # Boolean examples is_genetically_modified = True has_allergies = False is_result_positive = True # Using booleans in conditional logic (will be covered in more detail later) if is_genetically_modified: print(f"{patient_name} is involved in a genetic study.") else: print(f"{patient_name} is a standard case.") # String concatenation full_report_title = drug_name + " - " + lab_status + " Report" print(f"Report Title: {full_report_title}") print(f"Type of gene_sequence: {type(gene_sequence)}") print(f"Type of is_result_positive: {type(is_result_positive)}") # Output: # Alice Smith is involved in a genetic study. # Report Title: Paracetamol - In Progress Report # Type of gene_sequence: <class 'str'> # Type of is_result_positive: <class 'bool'> Understanding these basic data types is the first step towards effectively manipulating and analyzing the diverse data encountered in pharmacy and biotechnology. Python's dynamic typing means you don't explicitly declare a variable's type, but Python assigns it based on the value. However, knowing the underlying type is crucial for predicting behavior and preventing errors.
Key Takeaways:
Data types classify values and dictate operations. Integers ( int ) are for whole numbers (e.g., patient IDs, sample counts). Floating-point numbers ( float ) are for decimal values (e.g., concentrations, weights). Booleans ( bool ) represent True or False states (e.g., gene expressed, QC passed). Strings ( str ) are for textual data (e.g., names, sequences, notes). Python automatically infers the data type based on the assigned value.
Practice Exercise: Data Type Identification
For each of the following variables, determine the most appropriate Python data type ( int , float , bool , or str ) and briefly explain your reasoning in the context of pharmacy or biotech: drug_shelf_life_years = 3.5 is_clinical_trial_active = True dna_barcode = "AGCTAGCTCGATCGATCGA" number_of_patients_enrolled = 150 assay_result_optical_density = 0.876
Watch the full lesson — free
This topic is part of Python Programming - Basics, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →