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
This section introduces basic data types, control statements and functions, using a simple program (Fig. 35.5). In this program, we define two functions that use control statements to perform the operations of these functions.
Line 5 contains the function definition header for function greatestCommonDivisor. This function computes the greatest common divisor of two numbers-the largest integer that divides evenly into both numbers. The keyword def marks the beginning of the function definition. The function takes two parameters: x and y. The list of parameters is placed inside parentheses (()), and the parameter list is followed by a colon (:).
Fig. 35.5 Data types, control statements and functions.
1   # Fig. 35.5:
2   # Program to illustrate basic data types, control statements and
3   # functions.
5   def greatestCommonDivisor( x, y ):
6      gcd = min( x, y )
8      while gcd >= 1:
10         if ( x % gcd ) == ( y % gcd ) == 0:
11            return gcd
12         else:
13            gcd -= 1
15   def determineColor( color ):
17      if color == "green":
18         print "You entered green!"
19      elif color == "purple":
20         print "You entered purple!"
21      else:
22         print "You did not enter green or purple."
24   number1 = int( raw_input( "Enter a positive integer: " ) )
25   number2 = int( raw_input( "Enter a positive integer: " ) )
27   print "The greatest common divisor is", \
28      greatestCommonDivisor( number1, number2 )
30   for entry in range( 5 ):
31      colorChoice = raw_input( "\nEnter your favorite color: " )
32      determineColor( colorChoice )

Enter a positive integer: 2
Enter a positive integer: 30
The greatest common divisor is 2

Enter your favorite color: yellow
You did not enter green or purple.

Enter your favorite color: green
You entered green!

Enter your favorite color: black
You did not enter green or purple.

Enter your favorite color: purple
You entered purple!

Enter your favorite color: red
You did not enter green or purple.

Common Programming Error 35.1
Forgetting to place a colon after a function definition header or after a control statement is a syntax error.
Page 1 | 2 | 3
Read additional Python Tutorials
Return to Tutorial Index