# Episode 6: Exploring Data Types in Python

In the world of healthcare, we deal with **a wide range of data**, from patient names and lab values to imaging files and diagnosis reports. Before we can analyze or process this data using Python, we need to understand what **type of data** we are working with.

In this episode, we will explore:

* What Python **data types** are and **why they matter**
    
* The difference between **structured** and **unstructured medical data**
    
* Python's **basic data types**: `int`, `float`, `str`, `bool`
    
* Common Python collections: **lists**, **tuples**, and **dictionaries**
    
* How to check data types using `type()` and `isinstance()`
    

You will also see real-life **healthcare examples** that show how data types help organize and protect medical information.

---

## 🧠 **What are Python data types, and why do they matter in healthcare?**

In Python, every piece of data, like a number, a word, or a True/False value, has a **type**. Knowing the data type helps Python (and you) decide **what you can and cannot do** with that data. Python data types are the basic tools that help developers and analysts manage the large amounts of medical information created every day.

### 📋**Understanding structured vs unstructured medical data**

The medical field deals with **two primary categories** of information that require different processing approaches:

✅ **Structured medical data** is very organized and fits well into traditional databases. This includes:

* Patient demographics
    
* Vital signs measurements
    
* Laboratory test results
    
* Medication dosages
    

✅ **Unstructured data** doesn't have a set format but holds very valuable clinical insights. This includes:

* Clinical notes and discharge summaries
    
* Medical images (X-rays, MRIs, CT scans, ultrasounds)
    
* Transcribed conversations between doctors and patients
    
* Patient-generated health data from wearable devices
    
* Genetic sequencing reports
    

---

## 📌 Data Types in Python

Python provides several built-in data types that align perfectly with healthcare needs:

* Numeric types (`int`, `float`) handle quantitative medical data like patient ages, blood pressure readings, and lab values.
    
    ```python
    patient_age = 50     #This is an integer number
    body_temperature = 98.6      #This is a float number
    ```
    
    * **Integers (**`int`**)**: Perfect for whole numbers like patient age, heart rate, or step counts.
        
    * **Floating-point numbers (**`float`**)**: Ideal for measurements with decimal values such as body temperature (98.6°F), or medication dosages (2.5mg).
        
* String types (`str`) are **immutable**, meaning they **can’t be changed after they are created**. This makes them safer for storing sensitive patient information, as it prevents accidental changes.
    
    Strings are written inside single quotes `‘’` or double quotes `“”`.
    
    ```python
    patient_name = "Ali"
    diagnosis = 'Hypertension'
    ```
    
* Boolean types (`bool`) represent true/false values, ideal for test results and condition flags:
    
    * Laboratory test results (positive/negative)
        
    * Patient status flags (admitted/discharged)
        
    * Insurance verification (covered/not covered)
        
        ```python
        is_discharged = False
        covered_with_insurance = True
        ```
        

---

### Working with Python Collections: Lists, Tuples, and Dictionaries

In real-life healthcare and data science, we often need to manage **groups of related information**, like a list of symptoms, a fixed range of lab values, or detailed patient records. Python gives us special tools called **collections** to handle this kind of data efficiently.

In this section, you will learn about **Lists** for storing changeable data like medications, **Tuples** for storing fixed data sets like reference ranges, and **Dictionaries** for pairing labels with values like a patient’s name, age, and test results.

These structures help keep your data **organized, readable, and accessible**, which is especially important when working with medical information or building healthcare applications.

**Let’s explore each one and see how they work in simple, real-world examples:**

* Lists can be changed, which means they are **mutable**. They are written with square brackets `[]`. Lists are ordered collections, making them great for:
    
    1. Tracking symptoms that may change during treatment
        
        ```python
        symptoms = ["fever", "cough", "fatigue"]
        print(symptoms)
        ```
        
        2. Recording vital sign measurements over time
            
        3. Storing patient visit history
            
* Tuples store collections of related values, like symptoms or medications. They are **immutable** *(unchangeable)* ordered collections and are written with parentheses `()`.
    
    **Use tuples when you want to protect the data from being changed**. ideal for:
    
    * Lab result reference ranges that shouldn't change
        
    
    ```python
    hemoglobin_range = (12.0, 15.5)  # Lower and upper limit (g/dL)
    print("Hemoglobin normal range:", hemoglobin_range)
    ```
    
* Dictionaries map keys to values, making them perfect for linking patient IDs to records. They are written inside curly brackets.
    
    ```python
    patient_record = {
        "patient_id": "MRN12345",
        "name": "Sama Ahmed",
        "age": 25,
        "blood_type": "O+",
        "allergies": ["Penicillin", "Sulfa"],
        "vital_signs": {"temperature": 98.6, "blood_pressure": "120/80"}
    }
    ```
    
    **Don't worry about the dictionary syntax. We will cover this in more detail in future episodes.**
    

---

## 🧠 **How to check data types in Python**

1. You can use Python’s built-in function `type()` to find out the data type of any variable.
    
    ```python
    heart_rate = 85
    print(type(heart_rate))  # Output: <class 'int'>
    ```
    
    Note that the output shows you the 'class' type of the data, which is ‘int‘, an integer.
    
    Try this on your device:
    
    ```python
    # Patient details
    patient_name = "Amina Yusuf"          # str
    patient_age = 34                      # int
    patient_temp = 38.2                   # float
    is_discharged = False                 # bool
    
    # Checking data types
    print(type(patient_name))
    print(type(patient_age))
    print(type(patient_temp))
    print(type(is_discharged))
    ```
    
    For medical applications, `type()` offers a quick way to confirm that patient data is in the expected format before performing calculations or analysis.
    
2. Using `isinstance()` for Safer and Smarter Type Checking
    
    While `type()` tells you the exact type of a value, `isinstance()` is more flexible and often better when your program needs to handle different (but related) data types.
    
    Let’s say we want to know whether a piece of data is a number, a string, or something else.
    
    Here’s how you can do it:
    

```python
# Let's try with a number
heart_rate = 85

print(isinstance(heart_rate, int))  # This will print: True

# Now try with a word (a string)
blood_type = "O+"

print(isinstance(blood_type, int))  # This will print: False
print(isinstance(blood_type, str))  # This will print: True
```

* `isinstance(value, type)` This is the syntax for `isinstance`.  
    It checks: *“Is this value of this type?”*
    
    If **yes**, it prints `True`  
    If **no**, it prints `False`
    
* You can even check **two types at once**:
    
    ```python
    age = 30
    print(isinstance(age, (int, float)))  # Will be True if age is int or float
    ```
    
    ### ✅ Why This Is Helpful
    
    When working with real patient data, it’s important to:
    
    * Know **what type of data** you are dealing with (number, text, etc.)
        
    * Avoid mistakes like doing math on words
        
    
    So `isinstance()` is like asking:
    
    > "Is this value a number or text?"  
    > And Python answers: `True` or `False`
    
    Now, try this and let me know what the outputs are:
    
    ```python
    temperature = 98.6
    print(isinstance(temperature, float)) 
    
    patient_name = "Amina"
    print(isinstance(patient_name, str))
    ```
    

---

## 🧪 Practice Exercise: Organize and Check Patient Data

Create a Python program that stores the following information about a patient:

* Full name
    
* Age
    
* Body temperature
    
* Is the patient under observation? (yes/no)
    
* Allergies list
    
* Hemoglobin reference range
    
* Patient’s blood type
    

Use both `type()` and `isinstance()` to print the data types of each variable.

### 💻 What to do:

* Use `print()` to show each variable’s value
    
* Use `type()` to check its data type
    
* Try `isinstance()` on at least 3 variables to see if they match the expected type
    

📩 **Once you complete the exercise, please send your code and results to my email so I can review your progress and provide feedback. saja@sajamedtech.com**

---

## ✅ Conclusion: Why Data Types Matter in Medical Coding

Understanding **data types** is one of the most important skills you can learn as a beginner programmer, especially in **healthcare**, where working with sensitive and structured data is part of daily operations.

Knowing whether a piece of data is a number, a word, or a true/false value helps you:

* Use the right logic and operations
    
* Prevent errors in calculations
    
* Ensure patient data stays clean and accurate
    
* Work confidently with larger and more complex datasets in the future
    

In the next episode, we’ll begin a deeper exploration of each Python data type, starting with **strings**. You will learn how to work with text data, apply basic string operations, and understand why this is important in managing healthcare records.

See you tomorrow!
