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.
print("Hello Python")
print(10 + 20)
name = "Riya"
print("Welcome", name)Using sep and end in print()
sep decides what comes between multiple values. end decides what comes at the end of the print line.
print("A", "B", "C", sep="-")
print("Python", end=" ")
print("Tutorial")Input with input()
input() shows a message to the user and waits for typing. Whatever the user types is received as a string.
name = input("Enter your name: ")
print("Hello", name)Important: input() always returns text
If you want to perform calculation, convert the input into int or float.
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("Sum:", num1 + num2)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.
a, b = input("Enter two numbers: ").split()
a = int(a)
b = int(b)
print("Product:", a * b)Formatted Output with f-string
f-string is the cleanest way to show variables inside a sentence.
name = "Riya"
percentage = 86.5
print(f"{name} scored {percentage}% marks.")Complete Program: Student Marks Input
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
Common Mistakes
- Forgetting to convert input into
intorfloatbefore 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
- Take name, class and section from the user and print an ID-card style output.
- Take marks of 5 subjects and print total and percentage.
- Take price and quantity and print final bill amount.
- 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 होते हैं।
print() से Output
print("Hello Python")
print(10 + 20)
name = "Riya"
print("Welcome", name)input() से Input
input() user से value लेता है, लेकिन ध्यान रखें कि यह value हमेशा text/string के रूप में आती है।
name = input("Enter your name: ")
print("Hello", name)Calculation के लिए Type Casting जरूरी है
अगर input से calculation करनी है तो int() या float() से number में convert करें।
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("Sum:", num1 + num2)f-string से Clean Output
name = "Riya"
percentage = 86.5
print(f"{name} scored {percentage}% marks.")Complete Program: Student Marks Input
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
Common Mistakes
- Calculation से पहले input को
intयाfloatमें convert न करना। - User को clear prompt message न देना।
- Output को proper format में display न करना।
- Wrong input जैसे number की जगह letters handle न करना।
Practice Tasks
- User से name, class और section लेकर ID-card style output print करें।
- 5 subjects के marks input लेकर total और percentage print करें।
- Price और quantity input लेकर final bill बनाएं।
- f-string का use करके clean sentence में output दिखाएं।
सारांश
input() user से data लेता है, print() result दिखाता है, और type casting text input को number में बदलती है। Interactive Python programs के लिए यह सबसे जरूरी topic है।