🟢 Beginner  ·  Lesson 06

Input and Output in Python

Python में Input और Output

What are Input and Output in Python?

Input means data given to a program by the user. Output means the result shown by the program on the screen. In Python, input() is used to take input and print() is used to display output.

Example: in a marks calculator, the student name and marks are input; total, percentage and grade are output.

Output with print()

print() displays text, numbers, variables or calculated results on the screen.

Python
print("Hello Python")
print(10 + 20)
name = "Riya"
print("Welcome", name)
Hello Python 30 Welcome Riya

Using sep and end in print()

sep decides what comes between multiple values. end decides what comes at the end of the print line.

Python
print("A", "B", "C", sep="-")
print("Python", end=" ")
print("Tutorial")
A-B-C Python Tutorial

Input with input()

input() shows a message to the user and waits for typing. Whatever the user types is received as a string.

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

Important: input() always returns text

If you want to perform calculation, convert the input into int or float.

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

print("Sum:", num1 + num2)
Enter first number: 10 Enter second number: 20 Sum: 30
⚠️ Common Error

input() gives text. If you write "10" + "20", Python joins them as 1020. For addition, convert them using int() or float().

Taking Multiple Inputs

split() can divide one line input into multiple parts.

Python
a, b = input("Enter two numbers: ").split()
a = int(a)
b = int(b)
print("Product:", a * b)
Enter two numbers: 5 6 Product: 30

Formatted Output with f-string

f-string is the cleanest way to show variables inside a sentence.

Python
name = "Riya"
percentage = 86.5
print(f"{name} scored {percentage}% marks.")
Riya scored 86.5% marks.

Complete Program: Student Marks Input

Python – input-output.py
name = input("Enter student name: ")
maths = int(input("Enter Maths marks: "))
english = int(input("Enter English marks: "))
computer = int(input("Enter Computer marks: "))

total = maths + english + computer
percentage = total / 3

print("\n----- Result -----")
print(f"Name: {name}")
print(f"Total Marks: {total}")
print(f"Percentage: {percentage:.2f}%")

Expected Output

Enter student name: Riya Enter Maths marks: 80 Enter English marks: 85 Enter Computer marks: 90 ----- Result ----- Name: Riya Total Marks: 255 Percentage: 85.00%

Common Mistakes

  • Forgetting to convert input into int or float before calculation.
  • Writing prompt messages that are not clear for the user.
  • Using too many print statements without formatting the output properly.
  • Not handling wrong input such as letters instead of numbers.

Practice Tasks

  1. Take name, class and section from the user and print an ID-card style output.
  2. Take marks of 5 subjects and print total and percentage.
  3. Take price and quantity and print final bill amount.
  4. Use f-string to display output in a clean sentence.

Summary

input() receives data from the user, print() displays results, and type casting converts text input into numbers for calculation. This topic is required for almost every interactive Python program.

Python में Input और Output क्या होते हैं?

Input का मतलब है user द्वारा program को दी गई data/value. Output का मतलब है program द्वारा screen पर दिखाया गया result. Python में input लेने के लिए input() और output दिखाने के लिए print() use होता है।

Example: marks calculator में student name और marks input होते हैं; total, percentage और grade output होते हैं।

Python
print("Hello Python")
print(10 + 20)
name = "Riya"
print("Welcome", name)
Hello Python 30 Welcome Riya

input() से Input

input() user से value लेता है, लेकिन ध्यान रखें कि यह value हमेशा text/string के रूप में आती है।

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

Calculation के लिए Type Casting जरूरी है

अगर input से calculation करनी है तो int() या float() से number में convert करें।

Python
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("Sum:", num1 + num2)
Enter first number: 10 Enter second number: 20 Sum: 30

f-string से Clean Output

Python
name = "Riya"
percentage = 86.5
print(f"{name} scored {percentage}% marks.")
Riya scored 86.5% marks.

Complete Program: Student Marks Input

Python – input-output.py
name = input("Enter student name: ")
maths = int(input("Enter Maths marks: "))
english = int(input("Enter English marks: "))
computer = int(input("Enter Computer marks: "))

total = maths + english + computer
percentage = total / 3

print("\n----- Result -----")
print(f"Name: {name}")
print(f"Total Marks: {total}")
print(f"Percentage: {percentage:.2f}%")

Expected Output

Enter student name: Riya Enter Maths marks: 80 Enter English marks: 85 Enter Computer marks: 90 ----- Result ----- Name: Riya Total Marks: 255 Percentage: 85.00%

Common Mistakes

  • Calculation से पहले input को int या float में convert न करना।
  • User को clear prompt message न देना।
  • Output को proper format में display न करना।
  • Wrong input जैसे number की जगह letters handle न करना।

Practice Tasks

  1. User से name, class और section लेकर ID-card style output print करें।
  2. 5 subjects के marks input लेकर total और percentage print करें।
  3. Price और quantity input लेकर final bill बनाएं।
  4. f-string का use करके clean sentence में output दिखाएं।

सारांश

input() user से data लेता है, print() result दिखाता है, और type casting text input को number में बदलती है। Interactive Python programs के लिए यह सबसे जरूरी topic है।

← Back to Python Tutorial