Python में Input और Output
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
Input और Output
Programs user से दो तरह बात करते हैं: output (print() से results दिखाना) और input (input() से data पढ़ना)। दोनों मिलकर program को interactive बनाते हैं।
print() से Output
print() आप जानते हैं। यह text, variables और comma से अलग किए कई items print कर सकता है।
name = "Aman"
print("Hello", name, "welcome!")input() से Input
input() program को रोककर user के type करने का इंतज़ार करता है। यह हमेशा string लौटाता है — numbers के लिए int() या float() से convert करें।
input() text देता है। "5" + "3" "53" बनता है, 8 नहीं। maths से पहले हमेशा convert करें।
Program 1: User का स्वागत
name = input("What is your name? ")
print("Nice to meet you,", name)input("What is your name? ") prompt दिखाकर typed text को name में रखता है।
Program 2: दो Numbers का Sum
a = int(input("First number : "))
b = int(input("Second number: "))
total = a + b
print("Sum =", total)int(input(...))text पढ़कर एक step में integer बनाता है।int()के बिनाa + bstrings जोड़ देता, जोड़ नहीं करता।
Program 3: Simple Interest
p = float(input("Principal: "))
r = float(input("Rate %: "))
t = float(input("Time (years): "))
si = (p * r * t) / 100
print("Simple Interest =", si)यहाँ float() use करते हैं क्योंकि rate और time में decimals हो सकते हैं।
f-strings (Modern Formatting)
Text और variables मिलाने का सबसे साफ तरीका f-string है — quote से पहले f लगाएं और variables को { } में रखें।
name = "Aman"
marks = 95
print(f"{name} scored {marks} marks")सामान्य गलतियाँ
input()कोint()/float()से convert करना भूलना।- दो inputs जोड़ने पर sum की जगह joined text मिलना।
- prompt text के अंत में space छोड़ना भूलना।
Practice Tasks
- User की age पूछकर उसका जन्म वर्ष print करें।
- तीन subjects के marks पढ़कर total और average print करें।
- Celsius में temperature पूछकर Fahrenheit में print करें।
सारांश
print()output दिखाता है;input()input पढ़ता है।input()हमेशा string लौटाता है — maths के लिए convert करें।- f-strings (
f"{var}") output format करने का सबसे साफ तरीका है।