Input and output

Learning How Python Communicates with Users

Difficulty: Beginner

Reading Time: 20–25 minutes

Prerequisites:

  • Completed Module 1: Getting Started
  • Completed Lesson 2: Variables and Data Types
  • Completed Lesson 3: Operators and Expressions
  • Completed Lesson 4: Functions
  • Basic understanding of Python variables and Expressions

Learning Objectives

After completing this lesson, you will be able to:

  • Understand input and output in Python.
  • Use the print() function to display information.
  • Use the input() function to receive information from users.
  • Store user input in variables.
  • Understand that input() returns a string.
  • Convert user input into integers and floating-point numbers.
  • Format output using different techniques.
  • Create readable program output.
  • Use input and output in simple medical imaging examples.
  • Build a small interactive medical imaging program.

Introduction

A computer program is much more useful when it can communicate with the user.

So far, we have created programs such as:

patient_name = "Sarah"
age = 45

print(patient_name)
print(age)

The information is already stored in the program.

But what if we want the user to enter the patient’s name?

What if we want the user to enter:

  • Patient age
  • CT slice thickness
  • Number of slices
  • Image dimensions
  • HU values
  • Modality

Python provides two important functions for basic communication:

print()

and:

input()

The print() function produces output.

The input() function receives input from the user.

Together, they allow us to create interactive programs.


What is Input?

Input is information provided to a program.

For example, a user might enter:

Sarah

or:

45

or:

0.625

In a medical imaging program, input could represent information such as:

Patient name: Sarah

Age: 45

Modality: CT

Slice thickness: 0.625

Python can receive this information using the input() function.


What is Output?

Output is information produced or displayed by a program.

For example:

print("Hello")

Output:

Hello

We can print numbers:

print(45)

Output:

45

We can print decimal numbers:

print(0,625)

Output

0,625


Printing Variables

We can also print variables.

patient_name = “Sarah”

age = 45

print(patient_name)

print(age)

Output:

Sarah

45

This is useful when we want to display information stored in variables.


Printing Multiple Values

The print() function can display several values at once.

patient_name = “Sarah”

age = 45

modality = “CT”

print(patient_name, age, modality)

Output:

Sarah 45 CT

However, this output is not particularly easy to read.

We can make it clearer:

print("Patient:", patient_name)
print("Age:", age)
print("Modality:", modality)

Output:

Patient: Sarah
Age: 45
Modality: CT

Readable output is especially important in medical imaging applications.


Printing Text and Variables Together

For example:

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

Output:

Slice Thickness: 0.625 mm

We can do the same with other measurements:

number_of_slices = 400
print("Number of Slices:", number_of_slices)

Output:

Number of Slices: 400

The input() Function

The input() function allows the program to receive information from the user.

Example:

name = input(“Enter your name: “)

print(“Hello”, name)

The program displays:

Enter your name:

If the user enters:

Sarah

The output becomes:

Hello Sarah

The user’s input is stored in the variable name.


Understanding input()

Consider:

patient_name = input(“Enter patient name: “)

Here:

  • input() asks the user for information.
  • “Enter patient name: ” is the prompt.
  • The user’s response is stored in patient_name.

If the user enters:

John Smith

then:

patient_name

contains:

“John Smith”


Important: input() Returns a String

One of the most important things to remember is:

The input() function always returns a string.

For example:

age = input("Enter age: ")
print(type(age))

If the user enters:

45

the output is:

<class ‘str’>

Even though the user entered a number, Python initially treats it as text.

This is important when performing calculations.


The Problem with Numeric Input

Consider:

age = input("Enter age: ")
print(age + 5)

If the user enters:

45

Python will produce an error.

Why?

Because:

“45”

is a string, while:

5

is an integer.

Python cannot directly add a string and an integer.


Converting Input to an Integer

We can use int() to convert the input.

age = int(input(“Enter age: “))

print(age + 5)

If the user enters:

45

the output is:

50

The process is:

User enters “45”

        ↓

input() receives “45”

        ↓

int() converts it to 45

        ↓

Python performs the calculation


Converting Input to a Float

Medical imaging frequently uses decimal values.

For example:

0.625

1.25

2.5

We can use float().

slice_thickness = float(
    input("Enter slice thickness in mm: ")
)
print(slice_thickness)

If the user enters:

0,625

as a float.

We can check:

print(type(slice_thickness))

Output:

<class ‘float’>


Medical Imaging Example: Patient Age

Let’s create a simple program that asks for a patient’s age.

patient_name = input("Enter patient name: ")
age = int(input("Enter patient age: "))

print("Patient:", patient_name)
print("Age:", age)

Example interaction:

Enter patient name: Sarah

Enter patient age: 45

Patient: Sarah

Age: 45


Medical Imaging Example: CT Parameters

We can ask the user for CT examination information.

patient_name = input("Enter patient name: ")
slice_thickness = float(
    input("Enter slice thickness in mm: ")
)
number_of_slices = int(
    input("Enter number of slices: ")
)
print("Patient:", patient_name)
print("Slice Thickness:", slice_thickness, "mm")
print("Number of Slices:", number_of_slices)

Example:

Enter patient name: John

Enter slice thickness in mm: 0.625

Enter number of slices: 400

Patient: John

Slice Thickness: 0.625 mm

Number of Slices: 400


Combining Input with Calculations

Input becomes more useful when combined with operators.

For example, we can calculate scan coverage.

number_of_slices = int(
    input("Enter number of slices: ")
)

slice_thickness = float(
    input("Enter slice thickness in mm: ")
)

scan_coverage = number_of_slices * slice_thickness

print("Scan Coverage:", scan_coverage, "mm")

Example:

Enter number of slices: 400

Enter slice thickness in mm: 0.625

Scan Coverage: 250.0 mm

This is an example of an interactive medical imaging calculation.


Using Input with Functions

Input can also be passed to functions.

For example:

def calculate_coverage(number_of_slices, slice_thickness):
    return number_of_slices * slice_thickness


slices = int(input("Enter number of slices: "))
thickness = float(input("Enter slice thickness: "))

coverage = calculate_coverage(slices, thickness)

print("Scan Coverage:", coverage, "mm")

This program combines concepts from previous lessons:

  • Variables
  • Data types
  • Input
  • Output
  • Operators
  • Functions

This is an important step toward building larger Python programs.


Formatting Output

Readable output is important.

Instead of:

print(patient_name, age, modality, slice_thickness)
we can write:
print("Patient:", patient_name)
print("Age:", age)
print("Modality:", modality)
print("Slice Thickness:", slice_thickness, "mm")

This makes the information easier to understand.


Using f-Strings

One of the easiest and most useful ways to format output in modern Python is an f-string.

Example:

patient_name = "Sarah"
age = 45

print(f"Patient: {patient_name}")
print(f"Age: {age}")

Output:

Patient: Sarah
Age: 45

The f before the string tells Python that we want to insert variables inside {}.


Medical Imaging Example with f-Strings

patient = "Sarah"
slice_thickness = 0.625
number_of_slices = 400

print(f"Patient: {patient}")
print(f"Slice Thickness: {slice_thickness} mm")
print(f"Number of Slices: {number_of_slices}")

Output:

Patient: Sarah
Slice Thickness: 0.625 mm
Number of Slices: 400

Calculations Inside f-Strings

We can also place expressions inside an f-string.

number_of_slices = 400
slice_thickness = 0.625

print(
    f"Scan Coverage: {number_of_slices * slice_thickness} mm"
)

Output:

Scan Coverage: 250.0 mm

However, for complex calculations, it is often clearer to calculate the result separately.

coverage = number_of_slices * slice_thickness

print(f"Scan Coverage: {coverage} mm")

Controlling Decimal Places

Medical measurements often require a specific number of decimal places.

For example:

slice_thickness = 0.625

print(f"Slice Thickness: {slice_thickness:.2f} mm")

Output:

Slice Thickness: 0.62 mm

Here:

:.2f

means display the number with two decimal places.

For three decimal places:

print(f"Slice Thickness: {slice_thickness:.3f} mm")

Output:

Slice Thickness: 0.625 mm

Formatting Calculated Results

Consider:

number_of_slices = 333
slice_thickness = 0.625

coverage = number_of_slices * slice_thickness
print(f"Scan Coverage: {coverage:.2f} mm")

Output:

Scan Coverage: 208.12 mm

This produces cleaner output.

Remember that formatting changes how the value is displayed. It does not necessarily change the underlying numeric value.


Escape Characters

Python allows special characters inside strings.

For example:

print("Patient:\tSarah")

\t inserts a tab.

Another useful escape character is:

\n

which creates a new line.

Example:

print("Patient Information:\nSarah\n45\nCT")

Output:

Patient Information:

Sarah

45

CT


Creating Multi-Line Output

We can use multiple print() statements:

print("CT Examination")
print("----------------")
print("Patient: Sarah")
print("Age: 45")
print("Modality: CT")

Output:

CT Examination

—————-

Patient: Sarah

Age: 45

Modality: CT

This is useful for creating simple reports.


The sep Parameter

The print() function has a useful parameter called sep.

By default, Python separates multiple values with a space.

print("CT", "Sarah", 45)

Output:

CT Sarah 45

We can change the separator:

print("CT", "Sarah", 45, sep=" | ")

Output:

CT | Sarah | 45

This can be useful for displaying structured information.


The end Parameter

The print() function also has an end parameter.

Normally:

print("Hello")
print("World")

produces:

Hello

World

Each print() ends with a new line.

We can change this:

print("Hello", end=" ")
print("World")

Output:

Hello World

This is useful in some output formatting situations.


Input and Boolean Values

Suppose we ask whether contrast was used.

We might write:

contrast_used = input(“Was contrast used? “)

If the user enters:

True

the value is still a string:

“True”

It is not automatically the Boolean value:

True

For beginner programs, a simple approach is to ask for a clear response such as “yes” or “no”.

For example:

contrast_used = input(
    "Was contrast used? (yes/no): "
)

print("Contrast response:", contrast_used)

Later, when you learn conditional statements, you can use this input to make decisions.


Converting Other Data Types

Python provides several conversion functions.

FunctionConverts ToExample
int()Integerint(“45”)
float()Floatfloat(“0.625”)
str()Stringstr(45)
bool()Booleanbool(1)

Examples:

age = int("45")
slice_thickness = float("0.625")
patient_id = str(12345)

Common Beginner Mistakes

Mistake 1: Forgetting That input() Returns a String

age = input(“Enter age: “)

print(age + 5)

age = int(input(“Enter age: “))

print(age + 5)


Mistake 2: Forgetting to Convert Decimal Input

slice_thickness = input(

    “Enter slice thickness: “

)

coverage = 400 * slice_thickness

slice_thickness = float(

    input(“Enter slice thickness: “)

)

coverage = 400 * slice_thickness


Mistake 3: Using the Wrong Conversion

If the user enters:

0.625

this is not an integer.

slice_thickness = int(“0.625”)

slice_thickness = float(“0.625”)


Mistake 4: Forgetting Quotes Around Text

print(CT)

Python will interpret CT as a variable.

print(“CT”)


Mistake 5: Making Output Difficult to Read

Instead of:

print(patient, age, modality, thickness)

prefer:

print(f”Patient: {patient}”)

print(f”Age: {age}”)

print(f”Modality: {modality}”)

print(f”Slice Thickness: {thickness} mm”)


Best Practices

✔ Use clear prompts when asking for input.

✔ Convert numeric input immediately when appropriate.

✔ Use descriptive variable names.

✔ Make output easy to read.

✔ Use f-strings for clean formatted output.

✔ Include units when displaying medical measurements.

✔ Validate user input when building more advanced programs.

✔ Do not assume that user input is always correct.

✔ Separate calculations from presentation when programs become larger.

✔ Never use real patient-identifying information in beginner exercises or testing code.


Mini Project: Interactive CT Scan Summary

Let’s combine the concepts from the previous lessons.

The program will ask the user for:

  • Patient name
  • Age
  • Number of slices
  • Slice thickness
  • Image width
  • Image height

It will then calculate:

  • Scan coverage
  • Pixels per image
patient_name = input("Enter patient name: ")

age = int(
    input("Enter patient age: ")
)

number_of_slices = int(
    input("Enter number of slices: ")
)

slice_thickness = float(
    input("Enter slice thickness in mm: ")
)

image_width = int(
    input("Enter image width: ")
)

image_height = int(
    input("Enter image height: ")
)

scan_coverage = number_of_slices * slice_thickness
total_pixels = image_width * image_height

print()
print("CT Examination Summary")
print("----------------------")
print(f"Patient: {patient_name}")
print(f"Age: {age}")
print(f"Number of Slices: {number_of_slices}")
print(f"Slice Thickness: {slice_thickness:.3f} mm")
print(f"Scan Coverage: {scan_coverage:.2f} mm")
print(f"Image Dimensions: {image_width} x {image_height}")
print(f"Pixels per Image: {total_pixels}")

Example interaction:

Enter patient name: Sarah
Enter patient age: 45
Enter number of slices: 400
Enter slice thickness in mm: 0.625
Enter image width: 512
Enter image height: 512

CT Examination Summary

———————-

Patient: Sarah

Age: 45

Number of Slices: 400

Slice Thickness: 0.625 mm

Scan Coverage: 250.00 mm

Image Dimensions: 512 x 512

Pixels per Image: 262144

This project brings together several concepts:

Variables

    ↓

Data Types

    ↓

Input

    ↓

Type Conversion

    ↓

Operators

    ↓

Calculations

    ↓

Output


Mini Project 2: HU Measurement

We can also create an interactive program that asks the user for HU measurements.

hu_1 = float(
    input("Enter first HU value: ")
)
hu_2 = float(
    input("Enter second HU value: ")
)
difference = abs(hu_1 - hu_2)
print()
print("HU Analysis")
print("-----------")
print(f"First HU Value: {hu_1}")
print(f"Second HU Value: {hu_2}")
print(f"Absolute Difference: {difference}")

Example:

Enter first HU value: 45

Enter second HU value: 80

HU Analysis

———–

First HU Value: 45.0

Second HU Value: 80.0

Absolute Difference: 35.0

This is a simple educational example. Actual quantitative medical image analysis requires appropriate image data, calibration, acquisition parameters, and clinical context.


Exercises

Exercise 1 – Basic Output

Use print() to display:

Medical Imaging

Python Programming

PyMedLab

Each item should appear on a separate line.


Exercise 2 – Patient Information

Create variables for:

patient_name

age

modality

Display them using print().

Your output should look similar to:

Patient: John

Age: 54

Modality: CT


Exercise 3 – User Input

Ask the user to enter:

  • Patient name
  • Age
  • Modality

Display the information.

Remember that age should be converted to an integer.


Exercise 4 – CT Scan Coverage

Ask the user to enter:

  • Number of slices
  • Slice thickness

Calculate the scan coverage.

Display the result in millimeters.


Exercise 5 – Image Dimensions

Ask the user to enter:

Image width

Image height

Calculate the total number of pixels.

Example:

Image width: 512

Image height: 512

Total Pixels: 262144


Exercise 6 – HU Values

Ask the user to enter two HU values.

Calculate and display the absolute difference between them.


Exercise 7 – Formatted Output

Create a program that asks for:

Patient name

Age

Modality

Slice thickness

Number of slices

Display a formatted CT examination summary.

Use f-strings.


Exercise 8 – Medical Imaging Challenge

Create an interactive program that asks the user for:

patient_name

age

number_of_slices

slice_thickness

image_width

image_height

hu_value

Calculate:

  • Scan coverage
  • Pixels per image

Then display all information in a readable report.

Use appropriate data types and include units.


Summary

In this lesson, you learned:

  • What input and output mean.
  • How to use print() to display information.
  • How to use input() to receive information.
  • That input() returns a string.
  • How to convert strings to integers using int().
  • How to convert strings to floating-point numbers using float().
  • How to format output using f-strings.
  • How to control decimal places.
  • How to create readable program output.
  • How to combine input, calculations, functions, and output.
  • How to build simple interactive medical imaging programs.

The two most important functions from this lesson are:

print()

and:

input()

Remember:

input()  → Information enters the program

Processing → Python works with the information

print()  → Information leaves the program

A simple Python program can therefore follow this pattern:

Input

  ↓

Process

  ↓

Output

This pattern is fundamental to programming.


What’s Next?

In the next lesson, Conditional Statements, you will learn how Python can make decisions based on information.

You will learn how to use:

if

elif

else

For example, a program could receive a slice thickness from the user and determine whether it meets a selected requirement:

Enter slice thickness: 0.625

Slice thickness is acceptable.

You will also learn how to combine conditions with:

and

or

not

These concepts will allow your programs to move beyond simply calculating and displaying information.

They will allow Python to make decisions based on data—an essential foundation for medical image analysis, data processing, and later AI applications.


References

  1. Brian Heinold, A Practical Introduction to Python Programming.
  2. Python Software Foundation, Python Documentation.

Leave a Reply

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