Python | Input and Output Introduction By. Shivansh Sir
PYTHON PROGRAMMING
PYTHON | INPUT & OUTPUT
Python में input/output के लिए कुछ functions का इस्तेमाल किया जाता है | User के keyboard से input लेने के लिए 'input()' इस function का इस्तेमाल किया जाता है और User के screen पर कुछ print करने के लिए 'print()' इस function का इस्तेमाल किया जाता है |
input() Function for taking User Input
Python में जब User से कुछ letters या numbers लेने की बारी आती है तब 'input()' function का इस्तेमाल किया जाता है |
Syntax
input("SomeText")
SomeText :- User को क्या input देना है उसके बारे में यहाँ पर कुछ text दिया जाता है |
Example
Source Code
a = input("Enter your name : ")
print("Your name is", a)
Output
Enter your name : SUNRISE COMPUTER CLASSES
Your name is SUNRISE COMPUTER CLASSES
जब User Numeric, Letters या Special Symbols; Input लेते है तो उसका Data Type 'String' होता है |
Source Code
a = input("Enter Number : ")
print("Entered input's type is", type(a))
Output
Enter Number : 4
Entered input's type is
PYTHON | INPUT | TYPE CONVERSION
जब उस String data type को Number(int, float, complex number) में convert करना हो तो Type Conversion functions का इस्तेमाल करना पड़ता है |
Source Code
a = input("Enter Number : ")
print("Entered input's type is", a)
b = int(a)
print("Entered input's type is", b)
print("Entered input's type is", type(b))
c = float(a)
print("Entered input's type is", c)
print("Entered input's type is", type(c))
d = complex(a)
print("Entered input's type is", d)
print("Entered input's type is", type(d))
Output
Enter Number : 4
Entered input's type is 4
Entered input's type is 4
Entered input's type is
Entered input's type is 4.0
Entered input's type is
Entered input's type is (4+0j)
Entered input's type is
PYTHON | INPUT | PRINT FUNCTION
print() Function :- अभी तक User के screen पर output दिखाने के लिए 'print()' function का इस्तेमाल किया गया था, लेकिन print() function के लिए एक से ज्यादा parameters होते है | जिनका इस्तेमाल एक से ज्यादा newline देना या file handling के लिए किया जाता है |
Example
Source Code
a = 5
print("Value of a is", a)
Output
Value of a is 5
Syntax
print(value1,value2,...,valueN, sep="", end="\n", file=sys.stdout, flush=False)
value1,value2,...,valueN, :- यहाँ पर एक या एक से ज्यादा values दी जाती है | हर value को comma(,) से separate किया जाता है |
sep=" " :- जब एक से ज्यादा values दी जाती है तो default 'sep' parameter द्वारा space( ) दिया जाता है | User चाहे तो 'sep' parameter पर कोई भी value दे सकता है |
end="\n" :- 'end' parameter पर '\n'(newline) default दिया जाता है | User चाहे तो 'end' parameter पर कोई भी value दे सकता है |
file=sys.stdout :- Optional. 'file' parameter पर 'sys.stdout'(print entered text on screen) default दिया जाता है | User चाहे तो 'file' parameter पर किसी भी file को open कर सकता है |
flush=False :- 'flush' parameter पर 'False' ये value default होती है | जब False दिया जाता है तब stream को flush नहीं किया जाता है या जब 'True' दिया जाता है तब stream को जबरन flush किया जाता है |
print() function null/None return करता है |
Example for print() Function with 'sep' Parameter
sep= '!!!!!' इस string से separate किया गया है |
Source Code
print("Hello World", "Hello Friends", "Hello Ramesh", sep="!!!!!")
Output
Hello World!!!!!Hello Friends!!!!!Hello Ramesh
Example for print() Function with 'end' Parameter
Example में end पर '\n\n\n\n\n' इस string दिया गया है |
Source Code
print("Welcome to Sunrise Computer", end="\n\n\n\n\n")
print("Visit Again")
Output
Welcome to Sunrise Computer
Visit Again
Example for print() Function with 'file' Parameter
Example पर print() function के 'file' parameter द्वारा 'textfile.txt' इस file पर 'Hello World' ये string write किया गया है |
Source Code
f = open("textfile.txt", "w")
print("Welcome to Sunrise Computer", file = f)
f.close()
sunrise.txt
Output
Hello World
Note : print() function में binary files पर operation नहीं किया जाता है | इसके बदले में write() function का इस्तेमाल किया जाता है |
Comments
Post a Comment