"read numbers till eof and show squaresdef interact()print('hello stream world'print sends to sys stdout while truetryreply input('enter number>'input reads sys stdin except eoferrorbreak raises an except on eof elseinput given as string num int(replyprint("% squared is % (numnum * )print('bye'if __name__ ='__main__'interact(when runnot imported as usualthe interact function here is automatically executed when this file is runnot when it is imported by defaultrunning this file from system command line makes that standard stream appear where you typed the python command the script simply reads numbers until it reaches end-of-file in the standard input stream (on windowsend-of-file is usually the two-key combination ctrl-zon unixtype ctrl- instead+) :\pp \system\streamspython teststreams py hello stream world enter number> squared is enter number> squared is enter number>^ bye but on both windows and unix-like platformswe can redirect the standard input stream to come from file with the filename shell syntax here is command session in dos console box on windows that forces the script to read its input from text fileinput txt it' the same on linuxbut replace the dos type command with unix cat commandc:\pp \system\streamstype input txt :\pp \system\streamspython teststreams py input txt hello stream world notice that input raises an exception to signal end-of-filebut file read methods simply return an empty string for this condition because input also strips the end-of-line character at the end of linesan empty string result means an empty lineso an exception is necessary to specify the end-of-file condition file read methods retain the end-of-line character and denote an empty line as "\ninstead of "this is one way in which reading sys stdin directly differs from input the latter also accepts prompt string that is automatically printed before input is accepted standard streams