Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Chapter 4. Simple Numeric Expressions and Output

print Statement and Numeric Operations

Basic expressions are the most central and useful feature of modern programming languages. To see the results of expressions, we'll use a simple output statement. This chapter starts out with the section called “The print Statement”, which covers the print statement. the section called “Numeric Types and Operators” covers the basic numeric data types and operators that are integral to writing expressions Python. the section called “Numeric Conversion Functions” covers conversions between the various numeric types. the section called “Built-In Functions” covers some of the built-in functions that Python provides.

The print Statement

Before delving into numbers, we'll look briefly at the print statement. We'll cover just the essential syntax of the print statement in this section. While the print statement is very handy, it has some odd syntax quirks that we have to defer until later.

Note

Future versions of Python will replace this irregular statement with a built-in function that is perfectly regular, making it simpler to explain and use. Consequently, we have to remember that the print statement is handy, but must be examined critically.

print Syntax Overview

The print statement takes a list of values and, well, prints them. Speaking strictly, it does two things: it converts the objects to strings and puts the characters of those strings on standard output. Generally, standard output is the console window where Python was started, although there are ways to change this that are beyond the scope of this book.

Here's a quick summary of the more important features of print statement syntax. In short, the keyword, print, is followed by a comma-separated list of expressions.

print expression ,...〉

Note

This syntax summary isn't completely correct because it implies that the list of expressions is terminated with a comma. Rather than fuss around with complex syntax diagrams (that's what the Python reference manual is for) we've show an approximation that is close enough.

The , in a print statement is used to separate the various expressions.

A , can also be used at the end of the print statement to change the formatting; this is an odd-but-true feature that is unique to print statement syntax.

It's hard to capture all of this sublety in a single syntax diagram.

One of the simplest kind of expressions is a quoted string. You can use apostrophes (') or quotes (") to surround strings. This gives you some flexibility in your strings. You can put an apostrophe into a quoted string, and you can put quotes into an apostrophe'd string without special escapes that some other languages require. The full set of quoting rules and alternatives, however, will have to wait for Chapter 12, Strings .

For example, the following trivial program prints three strings and two numbers.

print "Hi, Mom", "Isn't it lovely?", 'I said, "Hi".', 42, 91056

Multi-Line Output. Ordinarily, each print statement produces one line of output. You can end the print statement with a trailing , to combine the results of multiple print statements into a single line. Here are two examples.

print "335/113=",
print 335.0/113.0

print "Hi, Mom", "Isn't it lovely?", 
print 'I said, "Hi".', 42, 91056

Since the first print statement ends with a , it does not produce a complete line of output. The second print statement finishes the line of output.

Redirecting Output. The print statement's output goes to the operating system's standard output file. How do we send output to the system's standard error file? This involves some more advanced concepts, so we'll introduce it with a two-part recipe that we need to look at in more depth. We'll revisit these topics in Part IV, “Components, Modules and Packages”.

First, you'll need access to the standard error object; you get this via the following statement.

import sys

Second, there is an unusual piece of syntax called a "chevron print" which can be used to redirect output to standard error.


print
 >>
file
, [ 
expression
, ]

The file can be either sys.stdout or sys.stderr. Here is an example of a small script which produces messages on both stderr and stdout.

Example 4.1. mixedout.py

#!/usr/bin/env python
"""Mixed output in stdout and stderr."""
import sys
print >>sys.stderr, "This is an error message"
print "This is stdout"
print >>sys.stdout, "This is also stdout"

When you run this inside IDLE, you'll notice that the stderr is colored red, where the stdout is colored black. You'll also notice that the order of the output in IDLE doesn't match the order in our program. Most POSIX operating systems buffer stdout, but does not buffer stderr. Consequently, stdout messages don't appear until the buffer is full, or the program exits.

print Notes and Hints

A program produces a number of kinds of output. The print statement is a handy jumping-off point. Generally, we'll replace this with more advanced techiques.

  • Final Reports. Our desktop applications may produce text-based report files. These are often done with print statements.

  • PDF or other format output files. A desktop application which produces PDF or other format files will need to use additional libraries to produce PDF files. For example, ReportLab offers PDF-production libraries. These applications won't make extensive use of print statements.

  • Error messages and processing logs. Logs and errors are often directed to the standard error file. You won't often use the print statement for this, but use the logging library.

  • Debugging messages. Debugging messages are often handled by the logging library.

The print statement is a very basic tool for debugging a complex Python program. Feel free to use print statements heavily to create a clear picture of what a program is actually doing. Ultimately, you are likely to replace print statements with other, more sophisticated methods.

Python Enhancement Proposal (PEP) 3105 suggests writing a print function which replaces the irregular print statement syntax with a regular function call. This function can then be altered in simple ways to produce more sophisticated output. We'll provide an example of this approach in the section called “Advanced Parameter Handling For Functions”.


 
 
  Published under the terms of the Open Publication License Design by Interspire