Input & Output | Dataplexa

Input and Output in Python

In every real Python program, you will need two things: taking information from the user and showing results back to them. These actions are called Input and Output. Python provides simple and powerful functions to handle both. In this lesson, you will learn how to use input() to receive user data and print() to display messages.

What Is Output in Python?

Output simply means showing information on the screen. In Python, we use the print() function to display text, numbers, variables, and results of calculations.

print("Welcome to Dataplexa!")
print(10 + 20)
print("Total:", 45 + 30)

The print() function is one of the most commonly used functions in Python and is extremely useful for debugging and displaying results.

Using print() with Multiple Values

You can pass several values inside print(). Python will automatically separate them with a space.

name = "Sreekanth"
age = 24
print("Name:", name, "| Age:", age)

What Is Input in Python?

Input means receiving information from the user. We use the input() function for this. It always returns the value as a string, even when the user enters numbers.

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

Anything typed by the user becomes the value stored in the variable.

Converting User Input to Numbers

Since input() returns a string, we must convert it to int or float when doing calculations.

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

print("Sum:", num1 + num2)

If the user enters decimals, use float() instead of int().

Formatting Output (Pretty Printing)

Python allows you to format messages using f-strings, making the output clean and professional.

name = "Sreekanth"
score = 95

print(f"Student: {name} | Score: {score}")

Real-World Example: Simple Billing System

Here is a small program that asks the user for item details and calculates the total bill.

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

total = price * qty

print("\n----- BILL SUMMARY -----")
print(f"Item: {item}")
print(f"Quantity: {qty}")
print(f"Total Amount: {total}")

This is how user input and program output work together in practical applications.


📝 Practice Exercises


  1. Ask the user to enter their first name and last name. Print them together in one sentence.
  2. Write a program that asks the user for two numbers and prints their product.
  3. Ask the user for their age and print how many years until they turn 100.
  4. Create a small input-output program that calculates the area of a rectangle.

✅ Practice Answers


Answer 1:

first = input("Enter first name: ")
last = input("Enter last name: ")

print("Full Name:", first, last)

Answer 2:

a = float(input("Enter first number: "))
b = float(input("Enter second number: "))

print("Product:", a * b)

Answer 3:

age = int(input("Enter your age: "))
years_left = 100 - age

print("Years until 100:", years_left)

Answer 4:

length = float(input("Enter length: "))
width = float(input("Enter width: "))

area = length * width

print("Area of Rectangle:", area)