Learn how Python receives information from users and displays results, with 10 practical real-time examples and complete code.
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.
Reads data entered by the user. By default, the returned value is a string.
name = input("Enter your name: ")
Displays text, values, variables, calculations, and formatted information on the screen.
print("Hello", name)
input(),
convert the returned string using int() or float().
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
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
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
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
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
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
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
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
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
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
| 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=" ") |
input() normally returns a string. Use
int() or float() when numerical calculations
are required. Use print() to present results clearly.