Logic Control

The main part of programming is learning how to make your code do something, primarily through a variety of logic controllers. These controllers handle if-then conditions, reiterative processing through loops, and dealing with errors. While there are other ways of working with code, these are the most important ones for new programmers to learn.

When dealing with logic control, a developer needs to be aware of how data is being transferred, particularly when working with user input, network connections, or filesystem access. Python has three data streams for input/output (I/O). sys.stdout is the standard output stream; it handles the output of print() and Python expressions. sys.stdin is the standard input stream; it is used for all interactive input. sys.stderr is the standard error stream; it only takes errors from the program, but also handles the interpreter's own prompts.

One thing to recognize is that, depending on the OS environment, information that you would consider going to stdout is actually sent to stderr, because stderr normally goes to the same location as stdout, by default you'll usually see it on the screen as well. However, you won't know that a response is actually going to stderr without testing. If the particular environment routes stderr to another location, such as a log file, you won't know until you need to troubleshoot. This is important to note because, sometimes, you may not be seeing the information you expect because it's not a normal stdout message.

These data streams are considered regular text files and can be accessed and interacted with just like normal files. File operations are looked at in Chapter 5Files and Databases, in the File I/O section.

This chapter will cover the following: