Python Basic Input and Output

Let’s look at basic input and output

  1. Notice

    input() returns text. Convert numeric text before doing arithmetic, then use print() to display the result.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
response = '20'  # A sample response from input()
age = int(response)
print("Next year:", age + 1)
Live state

Top-level scope

response→Object 1
age→Object 2
Object 1 · str'20'Object 2 · int20
This step

input() returns text. Convert numeric text before doing arithmetic, then use print() to display the result.

Picture basic input and output

input() returns text. Convert numeric text before doing arithmetic, then use print() to display the result.

Image brief

Alt text
A diagram for Python Basic Input and Output. Instructions: response = '20' # A sample response from input() age = int(response) print("Next year:", age + 1). Displayed result: Next year: 21. The final example bindings are response --> object 1 (str); age --> object 2 (int). object 1: '20'; object 2: 20
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Basic Input and Output". Teaching focus: input() returns text. Convert numeric text before doing arithmetic, then use print() to display the result. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS response = '20' # A sample response from input() age = int(response) print("Next year:", age + 1) | v DISPLAYED RESULT Next year: 21 FINAL BINDINGS response --> object 1 (str) age --> object 2 (int) object 1: '20' object 2: 20 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python Basic Input and Output. Instructions: response = '20' # A sample response from input() age = int(response) print("Next year:", age + 1). Displayed result: Next year: 21. The final example bindings are response --> object 1 (str); age --> object 2 (int). object 1: '20'; object 2: 20

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
response = '20'  # A sample response from input()
age = int(response)
print("Next year:", age + 1)
Live state

Top-level scope

response→Object 1
age→Object 2
Object 1 · str'20'Object 2 · int20
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
Next year: 21

What do you expect this program to print?

What does this example display?

response = '20'  # A sample response from input()
age = int(response)
print("Next year:", age + 1)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

Next year: 21

Follow the printed values in order. The output is: Next year: 21

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

input() returns text. Convert numeric text before doing arithmetic, then use print() to display the result.

input() returns text. Convert numeric text before doing arithmetic, then use print() to display the result.

Input and Output

In Python, you can interact with users using the input() and print() functions.

1. Using print() – Displaying Output

Let’s start by displaying something to the screen. The print() function sends information from your program to the user.

print("Hello, World!")
Hello, World!

What’s Happening?

Python simply prints the string "Hello, World!" to the console. This is your first output.

Printing Multiple Values

You can print more than one item by separating them with commas:

name = "Arjun"
age = 25
print("Name:", name, "Age:", age)
Name: Arjun Age: 25

Explanation

Each argument is converted to a string if needed, and Python automatically inserts spaces between them. You don’t need to manually add spaces unless you want specific formatting.

2. Using input() – Getting User Input

The input() function pauses the program and waits for the user to type something and hit enter.

name = input("What is your name? ")
print("Hello", name)
What is your name? Arjun
Hello Arjun

Important Notes:

  • input() always returns a string. Even if you type numbers, Python treats them as strings.
  • Use type() to check the data type if you're unsure.

Type Conversion (Casting)

To convert user input to another type (like integer or float), use casting functions:

age = input("Enter your age: ")
print("Type before conversion:", type(age))

age = int(age)
print("Type after conversion:", type(age))
Enter your age: 20
Type before conversion: <class 'str'>
Type after conversion: <class 'int'>

Verifying and Validating Input

Always check and validate user input to prevent errors. Here's a basic numeric check:

user_input = input("Enter a number: ")

if user_input.isdigit():
    number = int(user_input)
    print("You entered:", number)
else:
    print("That’s not a valid number.")
Enter a number: 45
You entered: 45
Enter a number: hello
That’s not a valid number.

Tips

  • print() is for output. Use sep and end to customize formatting.
  • input() is for input. It always returns a string.
  • Use int(), float(), or str() to convert types as needed.
  • Always validate user input to avoid runtime errors.

What to remember

Connect what you noticed in the example with the idea below.

Remember

input() returns text. Convert numeric text before doing arithmetic, then use print() to display the result.

Watch out for this idea

input() automatically returns an integer for numeric text.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python Operators →