🟢 Beginner  ·  Lesson 06

Input and Output in Python

Python में Input और Output

Input and Output

Programs talk to the user in two ways: output (showing results with print()) and input (reading data with input()). Together they make a program interactive.

Output with print()

You already know print(). It can print text, variables, and several items separated by commas.

Python – out.py
name = "Aman"
print("Hello", name, "welcome!")
Hello Aman welcome!

Input with input()

input() pauses the program and waits for the user to type. It always returns a string — convert it with int() or float() for numbers.

⚠️ Remember

input() gives text. "5" + "3" becomes "53", not 8. Always convert before doing maths.

Program 1: Greet the User

Python – greet.py
name = input("What is your name? ")
print("Nice to meet you,", name)
What is your name? Riya Nice to meet you, Riya

input("What is your name? ") shows the prompt and stores the typed text in name.

Program 2: Sum of Two Numbers

Python – sum.py
a = int(input("First number : "))
b = int(input("Second number: "))
total = a + b
print("Sum =", total)
First number : 12 Second number: 8 Sum = 20
  • int(input(...)) reads text and converts to integer in one step.
  • Without int(), a + b would join the strings instead of adding.

Program 3: Simple Interest

Python – interest.py
p = float(input("Principal: "))
r = float(input("Rate %: "))
t = float(input("Time (years): "))

si = (p * r * t) / 100
print("Simple Interest =", si)
Principal: 1000 Rate %: 5 Time (years): 2 Simple Interest = 100.0

We use float() here because rate and time can have decimals.

f-strings (Modern Formatting)

The cleanest way to mix text and variables is an f-string — put f before the quote and variables inside { }.

Python – fstring.py
name = "Aman"
marks = 95
print(f"{name} scored {marks} marks")
Aman scored 95 marks

Common Mistakes

  • Forgetting to convert input() with int()/float().
  • Adding two inputs and getting joined text instead of a sum.
  • Missing the space at the end of the prompt text.

Practice Tasks

  1. Ask the user's age and print the year they were born.
  2. Read three subject marks and print the total and average.
  3. Ask for a temperature in Celsius and print it in Fahrenheit.

Summary

  • print() shows output; input() reads input.
  • input() always returns a string — convert for maths.
  • f-strings (f"{var}") are the cleanest way to format output.

Input और Output

Programs user से दो तरह बात करते हैं: output (print() से results दिखाना) और input (input() से data पढ़ना)। दोनों मिलकर program को interactive बनाते हैं।

print() से Output

print() आप जानते हैं। यह text, variables और comma से अलग किए कई items print कर सकता है।

Python – out.py
name = "Aman"
print("Hello", name, "welcome!")
Hello Aman welcome!

input() से Input

input() program को रोककर user के type करने का इंतज़ार करता है। यह हमेशा string लौटाता है — numbers के लिए int() या float() से convert करें।

⚠️ याद रखें

input() text देता है। "5" + "3" "53" बनता है, 8 नहीं। maths से पहले हमेशा convert करें।

Program 1: User का स्वागत

Python – greet.py
name = input("What is your name? ")
print("Nice to meet you,", name)
What is your name? Riya Nice to meet you, Riya

input("What is your name? ") prompt दिखाकर typed text को name में रखता है।

Program 2: दो Numbers का Sum

Python – sum.py
a = int(input("First number : "))
b = int(input("Second number: "))
total = a + b
print("Sum =", total)
First number : 12 Second number: 8 Sum = 20
  • int(input(...)) text पढ़कर एक step में integer बनाता है।
  • int() के बिना a + b strings जोड़ देता, जोड़ नहीं करता।

Program 3: Simple Interest

Python – interest.py
p = float(input("Principal: "))
r = float(input("Rate %: "))
t = float(input("Time (years): "))

si = (p * r * t) / 100
print("Simple Interest =", si)
Principal: 1000 Rate %: 5 Time (years): 2 Simple Interest = 100.0

यहाँ float() use करते हैं क्योंकि rate और time में decimals हो सकते हैं।

f-strings (Modern Formatting)

Text और variables मिलाने का सबसे साफ तरीका f-string है — quote से पहले f लगाएं और variables को { } में रखें।

Python – fstring.py
name = "Aman"
marks = 95
print(f"{name} scored {marks} marks")
Aman scored 95 marks

सामान्य गलतियाँ

  • input() को int()/float() से convert करना भूलना।
  • दो inputs जोड़ने पर sum की जगह joined text मिलना।
  • prompt text के अंत में space छोड़ना भूलना।

Practice Tasks

  1. User की age पूछकर उसका जन्म वर्ष print करें।
  2. तीन subjects के marks पढ़कर total और average print करें।
  3. Celsius में temperature पूछकर Fahrenheit में print करें।

सारांश

  • print() output दिखाता है; input() input पढ़ता है।
  • input() हमेशा string लौटाता है — maths के लिए convert करें।
  • f-strings (f"{var}") output format करने का सबसे साफ तरीका है।
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

WhatsApp