← Back to Index

Python Input & Output Functions

Learn how Python receives information from users and displays results, with 10 practical real-time examples and complete code.

1. Understanding Input and Output

Input and output are fundamental programming operations. Python uses the input() function to receive data from a user and the print() function to display information.

input()

Reads data entered by the user. By default, the returned value is a string.

name = input("Enter your name: ")

print()

Displays text, values, variables, calculations, and formatted information on the screen.

print("Hello", name)
Important: If you need a number from input(), convert the returned string using int() or float().
Example 1

Student Name and Greeting

Collect a student's name and display a personalized greeting.

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

# Sample Input:
# Enter your name: Rahul

# Output:
# Welcome, Rahul
Real-time use: Student registration systems, learning applications, and personalized dashboards.
Example 2

Age Calculator

Read the user's birth year and calculate their approximate age.

current_year = 2026
birth_year = int(input("Enter your birth year: "))

age = current_year - birth_year

print("Your approximate age is:", age)

# Sample Input:
# Enter your birth year: 2000

# Output:
# Your approximate age is: 26
Real-time use: Registration forms, eligibility checks, membership applications, and profile systems.
Example 3

Shopping Bill

Take item price and quantity from a customer and calculate the total.

price = float(input("Enter item price: "))
quantity = int(input("Enter quantity: "))

total = price * quantity

print("Total amount:", total)

# Sample Input:
# Enter item price: 250
# Enter quantity: 3

# Output:
# Total amount: 750.0
Real-time use: Billing software, retail applications, shopping carts, and invoice generation.
Example 4

Employee Salary Calculation

Calculate an employee's annual salary from monthly salary input.

monthly_salary = float(input("Enter monthly salary: "))

annual_salary = monthly_salary * 12

print("Annual salary:", annual_salary)

# Sample Input:
# Enter monthly salary: 50000

# Output:
# Annual salary: 600000.0
Real-time use: Payroll applications, HR systems, salary reports, and financial planning.
Example 5

Temperature Converter

Convert a temperature from Celsius to Fahrenheit.

celsius = float(input("Enter temperature in Celsius: "))

fahrenheit = (celsius * 9 / 5) + 32

print("Temperature in Fahrenheit:", fahrenheit)

# Sample Input:
# Enter temperature in Celsius: 30

# Output:
# Temperature in Fahrenheit: 86.0
Real-time use: Weather applications, IoT devices, laboratory systems, and industrial monitoring.
Example 6

Simple Login Check

Read a username and password and check the entered credentials.

username = input("Enter username: ")
password = input("Enter password: ")

if username == "admin" and password == "1234":
    print("Login successful")
else:
    print("Invalid username or password")

# Sample Input:
# Enter username: admin
# Enter password: 1234

# Output:
# Login successful
Real-time use: Login forms, internal tools, learning portals, and basic authentication demonstrations.
Example 7

Student Marks and Result

Read marks for three subjects and calculate the average.

math = float(input("Enter Mathematics marks: "))
science = float(input("Enter Science marks: "))
english = float(input("Enter English marks: "))

average = (math + science + english) / 3

print("Average marks:", round(average, 2))

if average >= 40:
    print("Result: Pass")
else:
    print("Result: Fail")

# Sample Input:
# Mathematics: 80
# Science: 75
# English: 85

# Output:
# Average marks: 80.0
# Result: Pass
Real-time use: School management systems, examination software, report cards, and student dashboards.
Example 8

Restaurant Order Total

Calculate a restaurant bill using food price and quantity.

item = input("Enter food item: ")
price = float(input("Enter price: "))
quantity = int(input("Enter quantity: "))

subtotal = price * quantity
tax = subtotal * 0.05
total = subtotal + tax

print("\\nOrder Summary")
print("Item:", item)
print("Subtotal:", subtotal)
print("Tax:", tax)
print("Total:", total)

# Sample Input:
# Item: Pizza
# Price: 300
# Quantity: 2

# Output:
# Order Summary
# Item: Pizza
# Subtotal: 600.0
# Tax: 30.0
# Total: 630.0
Real-time use: Restaurant POS systems, food ordering applications, billing counters, and delivery platforms.
Example 9

Electricity Bill Estimate

Calculate an electricity bill using units consumed and a per-unit rate.

units = float(input("Enter electricity units used: "))
rate = float(input("Enter rate per unit: "))

bill = units * rate

print("Electricity bill:", bill)

# Sample Input:
# Units used: 250
# Rate per unit: 8

# Output:
# Electricity bill: 2000.0
Real-time use: Utility billing, smart-meter systems, household energy calculators, and expense tracking.
Example 10

ATM Withdrawal

Read an account balance and withdrawal amount and display the result.

balance = float(input("Enter account balance: "))
amount = float(input("Enter withdrawal amount: "))

if amount <= balance:
    balance = balance - amount
    print("Withdrawal successful")
    print("Remaining balance:", balance)
else:
    print("Insufficient balance")

# Sample Input:
# Account balance: 10000
# Withdrawal amount: 2500

# Output:
# Withdrawal successful
# Remaining balance: 7500.0
Real-time use: ATM simulations, banking applications, wallet systems, and transaction-processing exercises.

Quick Reference: Input & Output

Function / Technique Purpose Example
input() Read user input name = input("Name: ")
int() Convert input to integer age = int(input("Age: "))
float() Convert input to decimal number price = float(input("Price: "))
print() Display output print("Hello")
f-string Format output with variables print(f"Total: {total}")
sep Change separator in print() print("A", "B", sep="-")
end Change ending character in print() print("Hello", end=" ")
Key learning point: input() normally returns a string. Use int() or float() when numerical calculations are required. Use print() to present results clearly.