Python Programming from First Principles: Learn to Code, Think Computationally, and Solve Real-World Problems
PDFEPUB
BOOK 000 · CHAPTER 2 · Input and Output

Input and Output

On this pageOutput with print()Input with input()Mini Project: Greeting and Age Program

A program becomes truly useful when it can communicate with people. Communication happens in two directions: the program shows results (output) and receives information from the user (input).

Python makes this simple with two built-in functions:

  • print() for showing messages or results
  • input() for reading data that the user types

Output with print()

The print() function displays text or values on the screen. You can print messages, numbers, or even the results of calculations.

print("Hello, world!")
print("2 + 3 =", 2 + 3)
Listing 56 — Output with print()

Output

Hello, world!
2 + 3 = 5

This is how Python communicates results. Whether you are displaying a summary, a report, or a message,print() sends information to the user.

Input with input()

The input() function allows the program to ask the user for information. When it runs, it pauses and waits for the user to type something. Whatever the user enters is stored as text and can be used later in the program.

name = input("Enter your name: ")
print("Hello,", name)
Listing 57 — Input with input()

Example run (the text after the prompt is what the user types):

Enter your name: Ada
Hello, Ada

In this short example, the program asks for the user’s name, stores it in the variable name, and then prints a friendly greeting.

The text inside the parentheses of input() acts as a prompt message. It tells the user what to type. This is the same principle behind online forms, quizzes, or command-line tools where the user must enter details before continuing.

Mini Project: Greeting and Age Program

Write a short program that asks for a name and age, then displays how old the person will be next year. This uses int() to convert the age text into an integer; Type Conversion explains that step next.

Example run:

What is your name? Leo
How old are you? 19
Hello, Leo! Next year you will be 20.

The program gathers information from the user, performs a small calculation, and displays the result clearly. This same pattern appears in real systems, for example while filling a form online, updating a profile, or answering questions in a chatbot.