input output python

i am quite new to python …
i don’t understand how to handle the input , output … should it be in the form of a file [so we use something like open() to open the file ] or should it be entered at runtime [so that everytime i run it i should enter the input] ??

i tried both on the "enourmous input test " que. and both showed error

1 Like

I’m not “python guy”, but generally it depends on problem you are solving. I assume you are interested in CodeChef, here the input is from so called standard input (aka stdin).

On the other hand you do not need to enter the input by typing on keyboard. Both OS (linux, Windows) support stream redirections (I’m not sure about this term), so you can use

text.ex < test.in

and your program behavior is exactly the same as if you entered the input from keyboard.

How to implement this look at some AC solutions here on codechef - CodeChef: Practical coding for everyone with some language reference you will understand what’s going on :wink:

Hello csxxx,

Like betlista pointed out, you should always check the FAQ section in order to see how the I/O should be done…

However, online judge at Codechef is the same as SPOJ and it turns out that the judge will automatically redirect the input you entered to file format.

With that being said, to read and write data in Python you should use basic I/O functions like:

x = int(raw_input())

to read an integer from input, or

var1, var2 = [int(x) for x in raw_input().split()]

to read multiple integers on the same line…

To print you can use the basic print function!!

Hope it helps,

Bruno

2 Likes

use stdin in angle brackets for input and print for output!

or generally speaking, there are many ways to input or output data in Python. :slight_smile: