? Syllabus

Lesson 2 � Python

Input and Output

~35 min

1. Why Input and Output Matter

Programs aren''t very useful if they just run silently. To make interactive applications, you need two things:

  • Output: Displaying information to the user on their screen
  • Input: Receiving information from the user through their keyboard

Together, input and output (I/O) create a conversation between your program and the user. This lesson teaches you how to make that conversation happen.

2. The print() Function � Displaying Output

We''ve already used print() to display text. Let''s dive deeper into how powerful this function really is.

Basic Output

The simplest way to display something is:

print("Hello, World!")

Output:

Hello, World!

Printing Variables

You can print the value stored in a variable:

name = "Alice"
age = 18
gpa = 3.85

print(name)
print(age)
print(gpa)

Output:

Alice
18
3.85

Printing Multiple Items

Separate items with commas, and print() automatically adds spaces between them:

name = "Bob"
age = 20

print("My name is", name, "and I am", age, "years old.")

Output:

My name is Bob and I am 20 years old.

Why This Matters

Output is how your program communicates with the user. Without it, the user wouldn''t know what your program is doing.

3. The input() Function � Receiving Input

The input() function pauses your program, waits for the user to type something, and then stores what they typed as a string.

Basic Input

Here''s the simplest form:

name = input()
print("You entered:", name)

When you run this, the program waits silently for the user to type. If the user types "Charlie", the program displays:

You entered: Charlie

Adding a Prompt

Usually, you should tell the user what to type. Use a prompt string inside input():

name = input("Enter your name: ")
print("Hello,", name)

Output (when user types "David"):

Enter your name: David
Hello, David

Important: input() Returns a String

Critical point: input() always returns text (a string), even if the user types numbers:

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

Output (when user types "25"):

Enter your age: 25
<class ''str''>

The age is stored as the string "25", not the number 25. This matters when doing math!

4. Converting Input to Numbers

If you need to perform calculations with user input, you must convert the string to a number first.

Converting to Integer with int()

age_text = input("Enter your age: ")
age_number = int(age_text)
birth_year = 2026 - age_number

print("You were born in approximately", birth_year)

Output (when user enters "20"):

Enter your age: 20
You were born in approximately 2006

Converting to Float with float()

Use float() for decimal numbers:

height_text = input("Enter your height (in meters): ")
height_number = float(height_text)

print("Your height is", height_number, "meters")

Output (when user enters "1.75"):

Enter your height (in meters): 1.75
Your height is 1.75 meters

Shortcut: Convert Inside input()

You can convert directly to save a line:

age = int(input("Enter your age: "))
height = float(input("Enter your height: "))

5. Working with Input and Output Together

The real magic happens when you combine input and output to create interactive programs.

Example: Simple Calculator

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

sum_result = num1 + num2

print("The sum is:", sum_result)

Output (when user enters "10" and "5"):

Enter first number: 10
Enter second number: 5
The sum is: 15

Example: Personal Information Program

name = input("What is your name? ")
city = input("What city are you from? ")
hobby = input("What is your favorite hobby? ")

print("\n--- Your Profile ---")
print("Name:", name)
print("City:", city)
print("Hobby:", hobby)

Output (with sample input):

What is your name? Emma
What city are you from? Boston
What is your favorite hobby? Reading

--- Your Profile ---
Name: Emma
City: Boston
Hobby: Reading

Note: The \n in the string creates a blank line (newline character).

6. String Formatting � Making Output Look Nice

Python offers clean ways to insert variables into text using f-strings.

F-Strings (Modern Python)

Place an f before the string and put variable names inside curly braces:

name = "Frank"
age = 22

print(f"My name is {name} and I am {age} years old.")

Output:

My name is Frank and I am 22 years old.

F-Strings with Calculations

You can perform operations directly inside the braces:

birth_year = 2004
current_year = 2026

age = current_year - birth_year
print(f"You are {age} years old.")

Output:

You are 22 years old.

Why Format Your Output?

F-strings make your code cleaner and easier to read. Compare:

  • Without f-string: print("Hello " + name + "! You are " + str(age) + " years old.")
  • With f-string: print(f"Hello {name}! You are {age} years old.")

The f-string version is much cleaner.

Interactive Practice

Mini Practice: Create an Interactive Greeting Program

Write a Python script that asks for the user''s name and greets them. Use input() to ask, and f-strings to display a personalized greeting. Save as greet.py and run it.

0% complete
Check when done
Quiz

Test Your Understanding

1. What does the input() function return?

2. You need to get a user''s age as a number to perform calculations. What should you do?

3. What will be printed by this code?

4. Which f-string correctly displays a user''s name and age?

?? Progression

By mastering input and output, you can now build interactive programs that communicate with users. Next, you''ll learn about **Data Types and Variables** to store and manipulate different kinds of information.