Variables and Data Types

Variables and Data Types

Author
Admin
Last updated
Aug 25, 2026

Understanding How Python Stores and Manages Information

Difficulty: Beginner

Reading Time: 20–25 minutes

Prerequisites:

  • Completed Module 1: Getting Started
  • Basic understanding of running Python programs

Learning Objectives

After completing this lesson, you will be able to:

  • Understand what variables are.
  • Create and name variables correctly.
  • Work with different data types.
  • Display variables using the print() function.
  • Convert between data types.
  • Apply variables to simple medical imaging examples.

Introduction

Every Python program works with data. Whether you’re storing a patient’s name, recording a CT scanner’s slice thickness, or calculating the average Hounsfield Unit (HU) in an image, Python uses variables to store information.

One of the major purposes of a variable is to remember a value from one part of a program so that it can be used in another part of the program. A variable act like a labeled container that holds a value. That value can be a number, text, or another type of data, and it can change while your program is running.

Understanding variables and data types is one of the most important foundations of programming.


Why Variables Matter

Imagine you are analyzing a CT examination.

Instead of repeatedly typing the slice thickness throughout your program, you can store it in a variable.

slice_thickness = 1.25

Now, whenever you need the slice thickness, you simply use the variable name.

This makes your code:

  • Easier to read
  • Easier to update
  • Less prone to errors

Variables in Medical Imaging

               CT Examination

          Patient Information

Patient Name    Slice Thickness   Tube Voltage

              Python Variables

            Image Analysis Program


What is a Variable?

A variable is simply a named location in memory where Python stores a value.

Example:

patient_name = “Sarah Johnson”

Here:

  • patient_name is the variable.
  • “Sarah Johnson” is the value.

Creating Variables

Python creates variables automatically when you assign a value.

patient = "John Smith"
age = 57
slice_thickness = 1.0
Display them:
print(patient)
print(age)
print(slice_thickness)

Output

John Smith
57
1.0

Variable Naming Rules

A variable name:

✔ Can contain letters, numbers, and underscores.

✔ Must start with a letter or underscore.

✔ Is case-sensitive.

Examples:

patient_name
ct_image
pixel_spacing
study_date

Avoid names like:

1patient
patient-name
class

Common Data Types

Python supports several built-in data types.

Data TypeDescriptionExample
intWhole numbers512
floatDecimal numbers1.25
strText“CT Scan”
boolTrue/FalseTrue

Integer (int)

Integers represent whole numbers.

number_of_slices = 320
print(number_of_slices)

Float (float) Floating-point numbers contain decimals.

slice_thickness = 0.625
print(slice_thickness)

print(slice_thickness)


String (str)

Strings store text.

modality = "CT"
hospital = "University Hospital"

Boolean (bool)

Boolean values are either True or False.

contrast_used = True
print(contrast_used)

Checking Data Types

Use the type() function.

patient_name = "Emily"
age = 42
slice_thickness = 0.75
print(type(patient_name))
print(type(age))
print(type(slice_thickness))

Output

<class 'str'>
<class 'int'>
<class 'float'>

Converting Data Types

Sometimes you need to convert one type into another.

age = "45"
age = int(age)
print(age)

Output

12.0

Similarly:

scan_time = 12
scan_time = float(scan_time)
print(scan_time)

Suppose we want to store information about a CT examination.

patient_name = "John Doe"
age = 54
modality = "CT"
slice_thickness = 1.25
contrast = True
print(patient_name)
print(age)
print(modality)
print(slice_thickness)
print(contrast)

Output

John Doe
54
CT
1.25
True

Mini Project: CT Scan Summary

patient = "Sarah"
scanner = "Siemens SOMATOM"
slice_thickness = 0.6
number_of_slices = 450
print("Patient:", patient)
print("Scanner:", scanner)

print("Slice Thickness:", slice_thickness, "mm")

print("Slices:", number_of_slices)

This simple program demonstrates how variables can represent real clinical information.


Common Beginner Mistakes

Using Spaces in Variable Names

patient name = “John”

patient_name = “John”


Forgetting Quotes Around Text

modality = CT

modality = “CT”


Mixing Data Types

Trying to add a number and a string without conversion causes an error.

age = “45”

print(age + 5)

age = int(age)

print(age + 5)


Best Practices

✔ Use descriptive variable names.

✔ Follow the snake_case naming convention.

✔ Keep names short but meaningful.

✔ Use comments to explain complex variables.

✔ Avoid single-letter variable names unless used in simple loops or mathematical formulas.


Exercises

Exercise 1

Create variables for:

  • Patient name
  • Age
  • Modality
  • Slice thickness

Print each variable.


Exercise 2

Use type() to display the data type of each variable.


Exercise 3

Create a variable called hu_value and assign it the value 45.

Print the variable and its type.


Exercise 4

Convert the string “512” into an integer.

Print the result.


Exercise 5 (Medical Imaging)

Create variables for:

patient_name

modality

number_of_images

pixel_spacing

contrast_used

Display the information in a readable format.


Summary

In this lesson, you learned:

  • What variables are.
  • How Python stores information.
  • The four most common data types (int, float, str, and bool).
  • How to create, display, and convert variables.
  • How variables are used to represent clinical information in medical imaging.

Variables are the building blocks of every Python program. As you continue through PyMedLab, you’ll use them to store image metadata, perform calculations, analyze DICOM files, and develop AI applications.


What’s Next?

In the next lesson, Operators and Expressions, you’ll learn how to perform arithmetic calculations, compare values, and build logical expressions. These skills are essential for tasks such as calculating image dimensions, converting pixel values to Hounsfield Units, and evaluating conditions in medical imaging workflows.

References:

  1. Brian Heinold, A practical introduction Introduction to Python Programming
  2. Python Software Foundation. Python 3 Documentation. https://docs.python.org/3/

Leave a Reply

Your email address will not be published. Required fields are marked *