In online contests, when the length of input is not specified and reading the input file directly through the program is not possible, one can use this code in C++:
while (cin >> var)
{
//do something with var
}
What's the equivalent for python?
open() write() ...
So, cout stands for “character output”. Much like the Python print statement, cout is used to print to the standard output device, which is typically your screen.
cin is the standard input stream. Usually the stuff someone types in with a keyboard. We can extract values of this stream, using the >> operator. So cin >> n; reads an integer. However, the result of (cin >> variable) is a reference to cin .
The basic way to do output is the print statement. To end the printed line with a newline, add a print statement without any objects. This will print to any object that implements write(), which includes file objects.
There's no direct equivalent in Python. But you can simulate it with two nested loops:
for line in sys.stdin:
for var in line.split():
If you need something other than a string you'll need to convert it in a separate step:
var = int(var)
This could be helpfull.
import sys
for line in sys.stdin:
#Do stuff
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With