- 2.1 Program Output, the print Statement, and Hello World!
- 2.2 Program Input and the raw_input()Built-in Function
- 2.3 Comments
- 2.4 Operators
- 2.5 Variables and Assignment
- 2.6 Numbers
- 2.7 Strings
- 2.8 Lists and Tuples
- 2.9 Dictionaries
- 2.10 Code Blocks Use Indentation
- 2.11 if Statement
- 2.12 while Loop
- 2.13 for Loop and the range() Built-in Function
- 2.14 List Comprehensions
- 2.15 Files and the open() and file() Built-in Functions
- 2.16 Errors and Exceptions
- 2.17 Functions
- 2.18 Classes
- 2.19 Modules
- 2.20 Useful Functions
- 2.21 Exercises
- Variables and Assignment
- Python Types
- Loops and Conditionals
This “quick start” section is intended to “flash” Python to you so that any constructs recognized from previous programming experience can be used for your immediate needs. The details will be spelled out in succeeding chapters, but a high-level tour is one fast and easy way to get you into Python and show you what it has to offer. The best way to follow along is to bring up the Python interpreter in front of you and try some of these examples, and at the same time you can experiment on your own.
We introduced how to start up the Python interpreter in Chapter 1 as well as in the exercises (Problems 1-4). In all interactive examples, you will see the Python primary ( >>> ) and secondary ( ... ) prompts. The primary prompt is a way for the interpreter to let you know that it is expecting the next Python statement, while the secondary prompt indicates that the interpreter is waiting for additional input to complete the current statement.
You will notice two primary ways that Python “does things” for you: statements and expressions (functions, equations, etc.). Most of you already know the difference between the two, but in case you need to review, a statement is a body of control which involves using keywords. It is similar to issuing a command to the interpreter. You ask Python to do something for you, and it will do it. Statements may or may not lead to a result or output. Let us use the print statement for the programmer’s perennial first example, Hello World:
>>> print 'Hello World!' Hello World!
Expressions, on the other hand, do not use keywords. They can be simple equations that you use with mathematical operators, or can be functions which are called with parentheses. They may or may not take input, and they may or may not return a (meaningful) value. (Functions that do not explicitly return a value by the programmer automatically return None, Python’s equivalent to NULL.) An example of a function that takes input and has a return value is the abs() function, which takes a number and returns its absolute value is:
>>> abs(4) 4 >>> abs(-4) 4
We will introduce both statements and expressions in this chapter. Let us continue with more about the print statement.
2.1 Program Output, the print Statement, and “Hello World!”
In some languages, such as C, displaying to the screen is accomplished with a function, e.g., printf(), while with Python and most interpreted and scripting languages, it is a statement. Many shell script languages use an echo command for program output.
Python’s print statement, paired with the string format operator ( % ), supports string substitution, much like the printf() function in C:
>>> print "%s is number %d!" % ("Python", 1) Python is number 1!
%s means to substitute a string while %d indicates an integer should be substituted. Another popular one is %f for floating point numbers. We will see more examples throughout this chapter. Python is fairly flexible, though, so you could pass in a number to %s without suffering any consequences with more rigid languages. See Section 6.4.1 for more information on the string format operator.
The print statement also allows its output directed to a file. This feature was added way back in Python 2.0. The >> symbols are used to redirect the output, as in this example with standard error:
import sys print >> sys.stderr, 'Fatal error: invalid input!'
Here is the same example with a logfile:
logfile = open('/tmp/mylog.txt', 'a') print >> logfile, 'Fatal error: invalid input!' logfile.close()