Python Input and Output
Python uses the input()
function to take user input and print()
function to display output. You can print strings, numbers, or formatted messages.
Python में input()
function यूज़र से input लेने के लिए और print()
function output दिखाने के लिए इस्तेमाल होता है। आप टेक्स्ट, नंबर और formatted message print कर सकते हैं।
Example 1: Simple Output
print("Hello, World!")
Output:
Hello, World!
Displays a basic greeting message.
एक सिंपल greeting message दिखाता है।
Example 2: Print Variables
name = 'Amit'
print("My name is", name)
Output:
My name is Amit
Prints variable along with text.
Text के साथ variable को print करता है।
Example 3: Taking User Input
age = input("Enter your age: ")
print("You entered:", age)
Output:
Enter your age: 18 You entered: 18
Takes user input and prints it.
User से input लेकर उसे print करता है।
Example 4: Input as Integer
num = int(input("Enter a number: ") )
print("Square =", num ** 2)
Output:
Enter a number: 4 Square = 16
Converts input to integer and prints square.
Input को integer में बदलकर उसका square print करता है।
Example 5: Input as Float
price = float(input("Enter product price: ") )
print("Price is ₹", price)
Output:
Enter product price: 49.99 Price is ₹ 49.99
Takes float input and prints it.
Float input लेकर उसे print करता है।
Example 6: Multiple Inputs
x, y = input("Enter two numbers: ").split()
print("x =", x)
print("y =", y)
Output:
Enter two numbers: 5 10 x = 5 y = 10
Takes two inputs from same line.
एक ही लाइन से दो input लेता है।
Example 7: Input Using map()
a, b = map(int, input("Enter two integers: ").split())
print("Sum =", a + b)
Output:
Enter two integers: 3 7 Sum = 10
Converts both inputs to int and adds.
दोनों input को int में बदलकर उनका योग करता है।
Example 8: Formatted Output (f-string)
name = 'Anjali'
age = 22
print(f"{name} is {age} years old.")
Output:
Anjali is 22 years old.
Uses f-string for neat formatting.
f-string का उपयोग करके formatted message print करता है।