Skip to content

Module 2 · Python Syntax and Fundamentals — topic 10 of 14

Input and output

Programs take input, process it and produce output. Learn the input–process–output model and how print() and input() connect a Python program to its user.

Beginner 12 min read·Lesson 24 of 306 in Python Programming

Learning objectives

  • Describe the input–process–output model
  • Combine input() and print() in a small program
  • Explain that input() always returns text

Definition

Programs take input, process it and produce output. Learn the input–process–output model and how print() and input() connect a Python program to its user.

Introduction

Almost every useful program follows the same shape: it receives some input, processes it, and produces output. A calculator takes numbers, adds them and shows the sum; a grade calculator takes marks and shows a grade.

In a text-based Python program, input usually comes from the keyboard through input() and output goes to the screen through print().

Why it matters

Interactive programs make your projects usable by other people. The first projects in this course, such as the Student Grade Calculator and Number Guessing Game, are built entirely on this input–process–output loop.

Detailed explanation

Input: data entering the program, from the keyboard, a file, a network or another program.

Processing: calculations and decisions the program makes with that data.

Output: results leaving the program, shown on screen, written to a file or sent elsewhere.

input(prompt) shows the prompt, waits for the user to type a line and press Enter, then returns what they typed as a string.

print() writes to standard output, the screen by default. Behind the scenes these are called the standard input (stdin) and standard output (stdout) streams.

Sample input in these lessons

Examples that use input() show the sample input used to check them. When you run them, type your own values.

Syntax

Python
raw = input("Prompt: ")
result = process(raw)
print(result)
  • input() returns the typed text as a string.
  • Convert or process it as needed.
  • print() shows the result.

Code examples

Echo a name

Python
name = input("What is your name? ")
print("Hello,", name)
Input
Meera
Output
What is your name? Hello, Meera

When verified automatically the typed text isn’t echoed, so the prompt and reply appear on one line. On screen you would see your typing after the prompt.

Input is text

Python
value = input("Enter a number: ")
print(type(value).__name__)
Input
42
Output
Enter a number: str

More examples

Input, process, output

Python
item = input("Item: ")
price = float(input("Price: "))
qty = int(input("Quantity: "))
total = price * qty
print()
print(f"{qty} x {item} = Rs {total:.2f}")
Input
Pen
12.5
4
Output
Item: Price: Quantity: 
4 x Pen = Rs 50.00

Step-by-step explanation

  • Three input() calls collect the item name, price and quantity.
  • float() and int() convert the typed text into numbers so we can multiply.
  • total is the processing step.
  • print() with nothing inside prints an empty line, then the formatted result follows.

Expected output

Running “Input, process, output” with the sample input shown prints exactly:

TextInput
Pen
12.5
4
TextOutput
Item: Price: Quantity:
4 x Pen = Rs 50.00

Practical applications

  • ATMs, ticket machines and command-line tools all follow the input–process–output pattern.
  • Server programs receive input from web requests and send output as web pages.
  • Data scripts read input from files and write output reports.

Best practices

  • Always give input() a clear prompt ending in a space.
  • Convert input to the type you need straight away.
  • Separate the processing step from input and output so it can be tested.

Common mistakes

Doing maths on input directly

Avoid

age = input("Age: ")
print(age + 1)

Prefer

age = int(input("Age: "))
print(age + 1)

input() returns a string; adding a number to it raises a TypeError.

No prompt

Avoid

x = input()

Prefer

x = input("Enter your city: ")

Without a prompt the user sees a blank line and doesn’t know what to type.

Key takeaways

  • Programs follow input → process → output.
  • input() returns a string; print() writes to the screen.
  • Convert input with int() or float() before calculating.

Try it yourself

  1. 1
    Ask for the length and width of a room and print its area.
  2. 2
    Ask for a name and a year of birth, then print the person’s age in 2026.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

10 questions cover the “Python Syntax and Fundamentals” module. Answers and explanations are revealed after you submit.

  • BeginnerWhat happens when this code runs?
  • BeginnerWhich line is a comment in Python?
  • BeginnerWhich of these is a Python keyword?
Practise this module