Python Input, Output and Import.
हे ट्यूटोरियल पायथनमध्ये I / O कार्य करण्यासाठी दोन अंगभूत फंक्शन्स प्रिंट () आणि इनपुट () वर लक्ष केंद्रित करते. तसेच, आपण मॉड्यूल आयात करणे आणि त्या आपल्या प्रोग्राममध्ये वापरण्यास शिकाल.
पायथन असंख्य अंगभूत फंक्शन्स पुरवते जी पायथन प्रॉम्प्टवर सहज उपलब्ध असतात. अनुक्रमे इनपुट () आणि प्रिंट () सारख्या काही फंक्शन्सचा मोठ्या प्रमाणात वापर केला जातो. प्रथम आउटपुट सेक्शन पाहू.
• Python Output Using print() function.
आम्ही मानक आउटपुट डिव्हाइस (स्क्रीन) वर डेटा आउटपुट करण्यासाठी प्रिंट () फंक्शन वापरतो. आम्ही ('This sentence is output to the screen') फाईलमध्ये डेटा आउटपुट देखील करू शकतो, परंतु नंतर यावर चर्चा होईल. त्याच्या वापराचे उदाहरण खाली दिले आहे.
print('This sentence is output to the screen')
Output
This sentence is output to the screen
आणखी एक उदाहरण खाली दिले आहे:
a = 5
print('The value of a is', a)
Output
The value of a is 5
दुसर्या प्रिंट () स्टेटमेंटमध्ये आपल्या लक्षात येईल की स्ट्रिंग आणि व्हेरिएबल a च्या व्हॅल्यू दरम्यान स्पेस जोडली गेली आहे. हे डीफॉल्टनुसार आहे, परंतु आम्ही ते बदलू शकतो.
प्रिंट () कार्याचे वास्तविक वाक्यरचना असेः
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
येथे ऑब्जेक्ट्स प्रिंट करण्याचे मूल्य आहे.
व्हॅल्यूज दरम्यान सेप सेपरेटर वापरला जातो. हे एखाद्या स्पेस कॅरेक्टरमध्ये डिफॉल्ट होते.
सर्व व्हॅल्यूज प्रिंट झाल्यानंतर एंड प्रिंट होईल. ते एका नवीन ओळीत डीफॉल्ट होते.
फाईल एक ऑब्जेक्ट आहे जिथे मूल्ये मुद्रित केली जातात आणि त्याचे डीफॉल्ट मूल्य sys.stdout (स्क्रीन) असते. हे स्पष्ट करण्यासाठी येथे एक उदाहरण आहे.
Output
Output formatting
>>> x = 5; y = 10
>>> print('The value of x is {} and y is {}'.format(x,y))
The value of x is 5 and y is 10
print('I love {0} and {1}'.format('bread','butter'))
print('I love {1} and {0}'.format('bread','butter'))
I love bread and butter I love butter and bread
>>> print('Hello {name}, {greeting}'.format(greeting = 'Goodmorning', name = 'John'))
Hello John, Goodmorning
>>> x = 12.3456789
>>> print('The value of x is %3.2f' %x)
The value of x is 12.35
>>> print('The value of x is %3.4f' %x)
The value of x is 12.3457
Python Input
input([prompt])
>>> num = input('Enter a number: ')
Enter a number: 10
>>> num
'10'
>>> int('10')
10
>>> float('10')
10.0
>>> int('2+3')
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '2+3'
>>> eval('2+3')
5
Python Import
import math
import math
print(math.pi)
3.141592653589793
>>> from math import pi
>>> pi
3.141592653589793
>>> import sys
>>> sys.path
['',
'C:\\Python33\\Lib\\idlelib',
'C:\\Windows\\system32\\python33.zip',
'C:\\Python33\\DLLs',
'C:\\Python33\\lib',
'C:\\Python33',
'C:\\Python33\\lib\\site-packages']
0 टिप्पण्या