Deitel & Associates, Inc. Logo

Back to
digg.png delicious.png blinkit.png furl.png
Internet & World Wide Web How to Program, 3/e
Internet & World Wide Web How to Program, 3/e

© 2004
pages: 1420

Amazon logo

This tutorial continues our introduction to the Python programming language. We introduce Python's data types and control statements, and show how to build functions. (See our preceding tutorial Introduction to Python.)
Download the Code Examples
[Note: This tutorial is an excerpt (Section 35.2) of Chapter 35, Python, from our textbook Internet & World Wide Web How to Program, 3/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., INTERNET & WORLD WIDE WEB HOW TO PROGRAM, 3/E, 2004, pp.1242-1246. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
35.2   Basic Data Types, Control Statements and Functions (Continued)
Function determineColor (lines 15-22) takes parameter color, which contains a string. Lines 17-22 use the if...elif...else control statement to evaluate expressions based on the value of the parameter. If the value of parameter color is equal to the string "green" (line 17), the function prints "You entered green!" If the value of color is equal to the string "purple" (line 19), the function prints "You entered purple!" If the value of name does not match either of these strings (line 21), the function prints "You did not enter green or purple. Function determineColor illustrates simple Python string comparisons. We discuss string comparison and other string manipulations in Section 35.4.
Line 24 calls Python function raw_input to get input from the user. This function takes an optional string argument that is displayed as a prompt to the user. The raw_input function returns a string. The Python function int takes as an argument a noninteger type and returns an integer representation of the argument. We store the integer returned from function int in local variable number1. Line 25 retrieves a value for number2 in a similar fashion.
Common Programming Error 35.3
A numerical value obtained via the raw_input function must be converted from a string to the proper numerical type. Manipulating a string representation of a numerical value may result in a logical or syntactical error.
Lines 27-28 print the greatest common divisor of the two numbers to the screen. The backslash character (\) at the end of line 27 is a line-continuation character that allows us to continue a statement on the next line. The comma (,) that follows the string informs Python that we want to print additional items after the string. In this case, the additional item is the integer value returned by the call to function greatestCommonDivisor. Note from the output that Python automatically inserts a space between the last character in the string and the integer value.
Common Programming Error 35.4
Forgetting to include a line-continuation character (\) at the end of a statement that continues onto the next line is a syntax error.
Line 30 begins a Python for loop. The call to Python function range with an argument of 5 returns the values 0, 1, 2, 3 and 4. [Note: The function actually returns a list that contains these values. We discuss lists in Section 35.3.] The for loop iterates through these values and, on each iteration, assigns a value to variable entry and then executes the statements in the for block (lines 31-32). Thus, the statements in the for block execute five times. These statements retrieve a string from the user and pass it to function determineColor. Note the "\n" escape sequence at the beginning of the string in line 31. This is a special Python character that prints a newline to the screen. A newline causes the cursor (i.e., the current screen-position indicator) to move to the beginning of the next line on the screen. The program exits after calling function determineColor on five user-defined strings. Figure 35.6 lists some common Python escape sequences.
Fig. 35.6 Escape sequences.
Escape sequence Meaning
Newline (line feed).
Carriage return.
Single quote.
Double quote.
Page 1 | 2 | 3
Read additional Python Tutorials
Return to Tutorial Index