Internet & World Wide Web How to Program, 3/e
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
[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)
Line 6 calls Python function min on parameters x and y. This function returns the smaller of the two values. We assign the value returned by min to local variable gcd.
Note that line 6 is indented. Unlike many other languages, Python determines the beginning and end of a statement based on whitespace. Each new line begins a new statement. The indentation in line 6 marks the beginning of the code block that belongs to function greatestCommonDivisor. Groups of statements that belong to the same block of code are indented by the same amount. The language does not specify how many spaces to indent, only that the indentation must be consistent.
Common Programming Error 35.2
Inconsistent indentation in a Python program causes syntax errors.
Line 8 describes the beginning of a Python while loop. The code in the while block executes as long as gcd is greater than or equal to 1.
Line 10 is a Python if statement. If the specified condition is true (i.e., the condition evaluates to any nonzero value), the code in the if block (i.e., the indented code that follows the if statement) executes. The statement in line 10 uses the modulo operator (%) to determine whether parameters x and y can be divided evenly by variable gcd. The statement illustrates the fact that Python comparison expressions can be "chained." This code is identical to
if ( x % gcd ) == 0 == ( y % gcd ):
if x % gcd == 0 and y % gcd == 0:
Chaining occurs left to right; therefore, the preceding expression is more efficient than the expression presented in the code, because the preceding expression may save a division operation.
If the expression in line 10 is true, we have found the greatest common divisor. The return keyword (line 11) exits the function and returns the specified value.
If the expression in line 10 is false (i.e., the condition evaluates to zero), the code in the else block (lines 12-13) executes. This code decrements variable gcd by 1, using the -= statement and has the same effect as the statement
gcd = gcd - 1
Python defines several such operators, including +=, -=, *=, /=, %= (modulo division) and **= (exponentiation). [Note: These operators are new as of Python 2.0; using these operators in Python 1.5.2 or earlier causes a syntax error.]