COP 3223H meeting -*- Outline -*- * Input and Output (I/O) in Python Input and output are handled by statements, since they change the state of the program's environment ** input/output devices/streams In this course we will focus on terminal-style interaction based on character streams (keyboards and monitors)... (it's simplest) ------------------------------------------ STANDARD INPUT AND OUTPUT DEVICES In most operating systems (POSIX, Unix,...): stdin stdout ------> [ Program ] ------> \ stderr \-------> these are conventional names for I/O streams of characters stdin: standard input (often from a user) could be the terminal/console could be a file (prog file) stderr: standard error (usually to a user) usually a terminal/console/shell could be a file In Unix (POSIX) system shells: prog < in > out 2>errs runs the program with input from file "in" output going into file "out" and errors going into file "errs" prog1 out3 sets up a "pipeline" where prog1, prog2, and prog3 run in parallel, prog1's stdin is file in1 prog2's stdin is prog1's stdout prog3's stdin is prog2's stdout prog3's stdout is out3 ------------------------------------------ Unix also has more advanced ways of plumbing programs together, but this is a common pattern (pipes and filters) ** the problem, abstractly ------------------------------------------ THE PROBLEM OF INPUT AND OUTPUT Input Output ------------------------------------------ ...conveys some information from the external world (outside the computer) to the program - it must be encoded (or parsed) into some representation that the program can use ...conveys some information from the program to the extranal world, - it must be decoded (or unparsed) from the program's internal representation into a representation that conveys the intended information Q: Have you ever interacted with a program that was hard to talk to in a way that was understandable? A classic example is the error messages in modern GUIs, which come up with a button "OK" that to the program means "you acknowledge you have read this message" and to you probably means the opposite of "OK" (more like "Darn it!") ** output *** printing printing uses character strings as an external representation for communication ------------------------------------------ OUTPUT IN PYTHON print() function handles simple output (by default to sys.stdout) this is like printf in C Examples: >>> print("hi") >>> t = 7 >>> print("The value of t is:", t) >>> t = t + 1 >>> print("Now t", t, sep='==') >>> t = t+1 >>> print("And now t is", t) ------------------------------------------ Q: How did that space get in the output before 7? print separates its arguments with a space by default (but printf in C doesn't do that) Q: What does the sep keyword parameter do? specifies a string that separates the actuals in the output Note: keyword parameters are optional and have a default value (shown in IDLE's tool tips). They can be temporarily given a different value, in a call by adding sep='...' to the end of the call. C doesn't have any keyword parameters ------------------------------------------ OPTIONAL KEYWORD ARGUMENTS TO PRINT NAME= Explanation ========================================= sep=' ' separator printed between the arguments end='\n' terminator printed after all the arguments file=sys.stdout output device flush=False forcibly flush the output device? Examples: print("Error!", file=sys.stderr, flush=True) ------------------------------------------ in C different functions handle these options (like fflush()) *** other ways to print ------------------------------------------ THERE ARE OTHER WAYS TO PRINT We may see later: - write method on file objects - format strings - str.format() - printf style % operator formatting ------------------------------------------ ** character-based input in Python There are other important kinds of input, mouse clicks, for example, but character input is sufficient ------------------------------------------ READING A LINE OF INPUT WITH THE INPUT FUNCTION Reading a line from sys.stdin: >>> text = input("what number? ") >>> text >>> text + 3 # error! ------------------------------------------ Note that the newline in the input is stripped off, and that the result is a string! So the last bit is an error! similarly in C, but C doesn't have a function like "input" built-in *** reading and parsing ------------------------------------------ PARSING READ INPUT To convert a string into a number or some other type, input is parsed. Parsing an int: myint = int(input("number? ")) Parsing a float: myint = float(input("number? ")) ------------------------------------------ Note I think it looks nice to put a space after the question mark Other types are similar, but we haven't gotten to them yet. In C one uses fscanf or sscanf to do these tasks. ------------------------------------------ FOR YOU TO DO Write a Python program that reads 3 ints from standard input and prints their sum. ------------------------------------------ *** input from files ------------------------------------------ READING FROM A FILE f = open(filename) # prepares to read from filename f_contents = f.read() # reads entire contents of f See modules file and fileinput... ------------------------------------------ C uses library functions for these tasks