Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!

Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount

इनपुट और आउटपुट


इनपुट लेना और वांछित तरीके से आउटपुट दिखाना इंटरैक्टिव कोडिंग में एक महत्वपूर्ण भूमिका निभाता है। तो चलिए विभिन्न डेटा प्रकारों के इनपुट और आउटपुट पर ध्यान केंद्रित करते हैं।

raw_input()

यह तब इनपुट लेने के लिए उपयोग किया जाता है जब तक कि यह लाइन के अंत तक नहीं पहुंच जाता। ध्यान दें कि इसमें कोई स्पेस नहीं होना चाहिए। नया लाइन कैरेक्टर इनपुट लेने को रोक देता है और अगर इनपुट लाइन में कोई स्पेस है तो यह त्रुटि का कारण बनता है।

# स्टैंडर्ड इनपुट से प्राप्त इनपुट आउटपुट करता है
astring=raw_input()# इनपुट के रूप में hello दें
print raw_input()

इनपुट लेने के बाद हम उन्हें हमारी आवश्यक डेटा प्रकार में बदल सकते हैं जैसे कि int(), float(), str() का उपयोग करके।

num=int(raw_input())
print num
decimalnum=raw_input()
decimalnum=float(raw_input()
print decimalnum

input()

यह विशेष रूप से पूर्णांकों के लिए इनपुट लेने के लिए उपयोग किया जाता है। input() का raw_input() पर लाभ निम्नलिखित उदाहरण से स्पष्ट हो सकता है।

# 2*2 इनपुट के रूप में दें
a=input()
b=raw_input() # ध्यान दें कि int(raw_input()) त्रुटि का कारण बनता है
print a # 4 प्रिंट करता है
print b # 2*2 प्रिंट करता है

कैसे एकल लाइन से दो या दो से अधिक डेटा प्रकार अलग अलग स्पेस के साथ लिया जाए?

यहां हम split() और map() फ़ंक्शन्स का उपयोग करते हैं।

# पहली लाइन में दो पूर्णांक और तीसरी लाइन में दो से अधिक पूर्णांक दें
a, b = map(int, raw_input().split())
array = raw_input().split()
sum = 0
for each in array:
    sum = sum + int(each)
print(a, b, sum)  # पहले लाइन के पहले दो पूर्णांक और दूसरी लाइन के पूर्णांकों का योग प्रदर्शित करता है

आउटपुट फॉर्मेटिंग

आपने देखा होगा कि print स्टेटमेंट स्वतः एक नया लाइन जोड़ देता है। उपर्युक्त कोड में कॉमा का उपयोग करने पर मान को एक लाइन में स्पेस के साथ प्रदर्शित किया जाता है। sys मॉड्यूल आउटपुट फॉर्मेटिंग के लिए विभिन्न फंक्शन्स प्रदान करता है लेकिन यहां हम फॉर्मेटिंग का बुनियादी ज्ञान का उपयोग आउटपुट को अपनी आवश्यकताओं के अनुसार प्राप्त करने में सीखते हैं। कुछ उदाहरण देखते हैं आउटपुट फॉर्मेटिंग को समझने के लिए।

a = 5
b = 0.63
c = "hello"
print "a is : %d, b is %0.4f,c is %s" % (a,b,c)

आउटपुट को आत्म व्याख्यात्मक बनना चाहिए।

व्यायाम

ऐसा प्रोग्राम लिखें जो उपयोगकर्ता से उनका नाम, आयु और देश का नाम पूछे। फिर प्रोग्राम यह जानकारी वाक्य में शामिल करते हुए प्रदर्शित करेगा। प्रोग्राम में शामिल होना चाहिए:

  1. raw_input() का उपयोग करके नाम के लिए इनपुट लेना।
  2. input() का उपयोग करके आयु के लिए इनपुट लेना, और इसे पूर्णांक में परिवर्तित करना।
  3. raw_input() का उपयोग करके देश का नाम लेना।
  4. आउटपुट को एक वाक्य के रूप में प्रारूपित करना जो नाम, आयु, और देश को शामिल करता है।

यह प्रोग्राम पाइथन में इनपुट हैंडलिंग और स्ट्रिंग फॉर्मेटिंग को प्रदर्शित करना चाहिए।

# Taking the name input using raw_input() name = raw_input("Enter your name: ") # Taking the age input using input() and converting it to integer age = int(input("Enter your age: ")) # Taking the country input using raw_input() country = raw_input("Enter your country: ") # Displaying the formatted sentence with name, age, and country print("Hello, my name is {}, I am {} years old, and I am from {}.".format(name, age, country)) # Taking the name input using raw_input() name = raw_input("Enter your name: ") # Taking the age input using input() and converting it to integer age = int(input("Enter your age: ")) # Taking the country input using raw_input() country = raw_input("Enter your country: ") # Displaying the formatted sentence with name, age, and country print("Hello, my name is {}, I am {} years old, and I am from {}.".format(name, age, country)) name is {}, I am {} years old, and I am from {}.".format(name, age, country)) Enter your name: John Enter your age: 25 Enter your country: USA Hello, my name is John, I am 25 years old, and I am from USA.

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

Previous Tutorial Next Tutorial Take the Test
Copyright © learnpython.org. Read our Terms of Use and Privacy Policy