Introduction to Input and Output (I/O) in Python

In programming, Input and Output (I/O) are how we interact with the computer.

  • Input is how we give information to the program (like typing your name or a number).
  • Output is how the program shows information back to us (like printing a message or result).

Understanding I/O is important because it helps us create programs that can talk to users — like calculators, games, or data-entry tools. In Python, we use simple functions to do this.


Using the print() Function (Output)

What is print()?

The print() function is used to display text or values on the screen. It is one of the first functions you’ll use in Python.

Basic Syntax

pythonCopyEditprint("Hello, world!")
  • Anything inside the quotation marks (" ") is shown as output.
  • You can also print numbers or variables.

Examples

pythonCopyEditprint("My name is Alex")
print(42)
name = "Sam"
print(name)

Optional Parameters: sep and end

sep – Separator between items

pythonCopyEditprint("Apple", "Banana", "Cherry", sep=", ")
# Output: Apple, Banana, Cherry

end – What to print at the end

pythonCopyEditprint("Hello", end=" ")
print("World!")
# Output: Hello World!

Using the input() Function (Input)

What is input()?

The input() function asks the user to type something. The program waits until the user types and presses Enter.

Basic Syntax

pythonCopyEditname = input("What is your name? ")
print("Hello,", name)
  • The message inside input() is called a prompt. It tells the user what to do.
  • The value the user types is always a string (text), even if it looks like a number.

Converting Input to Numbers

To do math, you must convert input to a number using int() or float().

pythonCopyEditage = input("Enter your age: ")
age = int(age)  # Convert string to integer
print("You will be", age + 1, "next year.")
  • int() turns the input into a whole number (like 23).
  • float() turns the input into a decimal number (like 23.5).

Combining Input and Output: Real-Life Examples

1. Simple Calculator

pythonCopyEditnum1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
result = num1 + num2
print("The sum is:", result)

2. Name and Age Input

pythonCopyEditname = input("What is your name? ")
age = int(input("How old are you? "))
print(f"Hi {name}, you are {age} years old.")

3. Temperature Converter (Celsius to Fahrenheit)

pythonCopyEditcelsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius}°C is equal to {fahrenheit}°F")

Best Practices for Clear I/O

  • ✅ Use descriptive prompts in input() so the user knows what to enter.
  • ✅ Add spaces at the end of prompts for readability.
  • ✅ Use f-strings for clean and readable output.

Example with best practices:

pythonCopyEditname = input("Please enter your full name: ")
print(f"Welcome, {name}!")

Common Errors and How to Avoid Them

ProblemWhat It MeansHow to Fix
TypeError when addingYou tried to add a number and a stringUse int() or float() to convert
Wrong input formatUser enters words instead of numbersUse try-except or remind user in prompt
Missing space in promptInput appears next to textAdd a space: "Enter age: "

Example Fix:

pythonCopyEdit# Incorrect
age = input("Enter your age:")
# Better
age = input("Enter your age: ")

Summary of Key Takeaways

  • print() is used to show messages or results.
  • input() is used to get data from the user.
  • Input is always a string — use int() or float() to do math.
  • Use clear prompts and f-strings for clean, readable code.
  • Be careful with types and formatting to avoid errors.

Your Turn! ✨

Try writing your own small programs using input() and print():

  • Ask the user their favorite food and say something nice.
  • Create a mini calculator that subtracts two numbers.
  • Build a greeting that includes name and age.
Posted in ai