|
प्रश्न 4. Output दिखाने के लिए कौन-सा Function उपयोग होता है? |
|
प्रश्न 5. Python में सबसे अच्छा String Formatting तरीका कौन-सा है? |
अब तक आपने Python Install करना, Syntax, Variables, Data Types और Operators के बारे में सीखा। लेकिन किसी भी वास्तविक (Real-world) Program में केवल पहले से निर्धारित (Hardcoded) Values का उपयोग नहीं किया जाता। अधिकांश Programs में उपयोगकर्ता (User) से जानकारी ली जाती है, उस पर Processing की जाती है और फिर परिणाम (Output) स्क्रीन पर प्रदर्शित किया जाता है।
उदाहरण के लिए—
- Student का नाम पूछना
- दो संख्याएँ लेकर उनका योग निकालना
- कर्मचारी का वेतन दर्ज करना
- ग्राहक से उत्पाद की संख्या लेना
- परीक्षा के अंक लेकर परिणाम प्रदर्शित करना
इन सभी कार्यों के लिए Python में मुख्य रूप से दो Functions का उपयोग किया जाता है—
- input() → User से Input लेने के लिए
- print() → Output दिखाने के लिए
इस अध्याय में हम इन दोनों Functions के साथ-साथ Type Casting, String Formatting, Escape Characters और Practical Programs को विस्तार से समझेंगे।
जब Program चलने के दौरान User से कोई जानकारी ली जाती है, तो उसे Input कहते हैं।
Input Keyboard, Mouse, File, Database, API या अन्य स्रोतों से लिया जा सकता है, लेकिन शुरुआती स्तर पर सामान्यतः Keyboard Input का उपयोग किया जाता है।
- अपना नाम दर्ज करें।
- अपनी आयु दर्ज करें।
- दो संख्याएँ दर्ज करें।
Program द्वारा Processing के बाद User को जो परिणाम (Result) दिखाया जाता है, उसे Output कहते हैं।
Output स्क्रीन, Printer, File या अन्य माध्यमों पर प्रदर्शित किया जा सकता है।
- आपका नाम: Ravi
- कुल योग: 250
- परीक्षा परिणाम: Pass
Python में print() Function का उपयोग स्क्रीन पर Output प्रदर्शित करने के लिए किया जाता है।
print(value)
print("Welcome to Python")
Output
Welcome to Python
print(100)
print(25.75)
Output
100
25.75
name = "Rahul"
age = 20
print(name)
print(age)
Output
Rahul
20
name = "Rahul"
age = 20
print(name, age)
Output
Rahul 20
डिफ़ॉल्ट रूप से print() एक Space का उपयोग करता है।
इसे sep Parameter से बदला जा सकता है।
print("A", "B", "C", sep="-")
Output
A-B-C
print("2026", "08", "03", sep="/")
Output
2026/08/03
डिफ़ॉल्ट रूप से print() हर Output के बाद नई लाइन (New Line) में चला जाता है।
end Parameter से इसे बदला जा सकता है।
print("Hello", end=" ")
print("Python")
Output
Hello Python
print("Python")
print("Programming")
print("Language")
Output
Python
Programming
Language
Escape Characters विशेष प्रकार के Characters होते हैं जो Output को नियंत्रित करते हैं।
|
Escape Character |
कार्य |
|
\n |
नई लाइन |
|
\t |
Tab Space |
|
\\ |
Backslash |
|
\' |
Single Quote |
|
\" |
Double Quote |
print("Python\nProgramming")
Output
Python
Programming
print("Name\tMarks")
Output
Name Marks
print("\"Python\"")
Output
"Python"
Python में input() Function का उपयोग User से Input लेने के लिए किया जाता है।
input("Message")
name = input("Enter Your Name : ")
print(name)
Output
Enter Your Name : Rahul
Rahul
city = input("Enter City : ")
यह Message User को निर्देश देता है कि उसे क्या दर्ज करना है।
input() हमेशा String क्यों देता है?
Python में input() Function हमेशा String (str) Data Type Return करता है।
उदाहरण—
age = input("Enter Age : ")
print(type(age))
यदि User 20 लिखेगा, तब भी Output होगा—
<class 'str'>
इसी कारण संख्यात्मक गणनाओं से पहले Type Casting आवश्यक होती है।
एक Data Type को दूसरे Data Type में बदलने की प्रक्रिया Type Casting कहलाती है।
Python में मुख्य Type Casting Functions हैं—
- int()
- float()
- str()
- bool()
String को Integer में बदलने के लिए।
age = int(input("Enter Age : "))
print(age)
salary = float(input("Enter Salary : "))
number = 100
text = str(number)
print(text)
print(bool(1))
print(bool(0))
Output
True
False
a = int(input("Enter First Number : "))
b = int(input("Enter Second Number : "))
print("Sum =", a + b)
Output
Enter First Number : 15
Enter Second Number : 25
Sum = 40
a = int(input("First Number : "))
b = int(input("Second Number : "))
c = int(input("Third Number : "))
print("Total =", a+b+c)
name = input("Enter Name : ")
age = int(input("Enter Age : "))
city = input("Enter City : ")
print("Name :", name)
print("Age :", age)
print("City :", city)
a, b = input("Enter Two Numbers : ").split()
यदि Input हो—
10 20
तो—
- a = "10"
- b = "20"
a, b = map(int, input("Enter Two Numbers : ").split())
print(a+b)
यदि Input—
10 20
Output—
30
Python में Output को सुंदर और व्यवस्थित बनाने के लिए Formatting का उपयोग किया जाता है।
name = "Rahul"
print("Welcome", name)
Python 3.6+ में सबसे अच्छा तरीका।
name = "Rahul"
marks = 92
print(f"Student : {name}")
print(f"Marks : {marks}")
Output
Student : Rahul
Marks : 92
name = "Amit"
print("Welcome {}".format(name))
num = int(input("Enter Number : "))
print("Square =", num*num)
length = float(input("Length : "))
width = float(input("Width : "))
area = length * width
print("Area =", area)
p = float(input("Principal : "))
r = float(input("Rate : "))
t = float(input("Time : "))
si = (p*r*t)/100
print("Simple Interest =", si)
Program 4 – Celsius से Fahrenheit
c = float(input("Temperature : "))
f = (c * 9/5) + 32
print("Fahrenheit =", f)
age = input("Age : ")
print(age+10)
❌ Error क्योंकि age String है।
✔ सही—
age = int(input("Age : "))
print(age+10)
age = int(input())
यदि User लिखे—
ABC
तो ValueError आएगी क्योंकि "ABC" को Integer में नहीं बदला जा सकता।
- User को हमेशा स्पष्ट Message दें।
- Numeric Input के लिए int() या float() का उपयोग करें।
- Professional Programs में f-string का उपयोग करें।
- Input Validation करें।
- Error Handling के लिए try...except का उपयोग करें (आगे के अध्याय में सीखेंगे)।
परीक्षा की दृष्टि से महत्वपूर्ण तथ्य
- print() Output दिखाने के लिए उपयोग होता है।
- input() User से Input लेने के लिए उपयोग होता है।
- input() हमेशा String Return करता है।
- int(), float(), str(), bool() Type Casting Functions हैं।
- f-string आधुनिक और सबसे लोकप्रिय Formatting Method है।
- \n नई लाइन तथा \t Tab Space देता है।
Frequently Asked Questions (FAQs)
प्रश्न 1. Python में User से Input कैसे लेते हैं?
input() Function की सहायता से।
प्रश्न 2. input() किस प्रकार का Data Return करता है?
हमेशा String (str)।
प्रश्न 3. Integer Input लेने के लिए क्या करना होगा?
int(input()) का उपयोग करें।
प्रश्न 4. Output दिखाने के लिए कौन-सा Function उपयोग होता है?
print() Function।
प्रश्न 5. Python में सबसे अच्छा String Formatting तरीका कौन-सा है?
f-string (Python 3.6 और बाद के Versions में)।
- print() = Output
- input() = User Input
- input() हमेशा str Return करता है।
- int(), float(), str(), bool() = Type Casting Functions
- sep = Separator बदलने के लिए
- end = Line End बदलने के लिए
- \n = New Line
- \t = Tab
- f-string = आधुनिक Formatting Method
Python Decision Making (if, if-else, elif, Nested if और Match-Case Statement) — इसमें Conditional Statements, Decision Making, Flow Control, Logical Conditions, Practical Programs और Interview/Exam आधारित प्रश्नों को विस्तार से समझेंगे।