DOC PREVIEW
Chapter 35 – Python

This preview shows page 1-2-3-4-29-30-31-32-33-60-61-62-63 out of 63 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 63 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 63 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 63 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 63 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 63 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 63 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 63 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 63 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 63 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 63 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 63 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 63 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 63 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 63 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 35 – PythonObjectives35.1 Introduction35.1.1 First Python Programfig35_01.py (1 of 1)35.1.1 First Python Program35.1.2 Python Keywords35.1.2 Python Keywords35.2 Basic Data Types, Control Statements and Functionsfig35_05.py (1 of 2)fig35_05.py (2 of 2)35.2 Basic Data Types, Control Statements and Functions35.3 Tuples, Lists and Dictionariesfig35_07.py (1 of 3)fig35_07.py (2 of 3)fig35_07.py (3 of 3)PowerPoint Presentation35.3 Tuples, Lists and DictionariesSlide 1935.4 String Processing and Regular Expressionsfig35_10.py (1 of 2)fig35_10.py (2 of 2)35.4 String Processing and Regular Expressionsfig35_12.py (1 of 2)fig35_12.py (2 of 2)Slide 2635.5 Exception Handling35.5 Exception Handlingfig35_15.py (1 of 1)35.6 Introduction to CGI Programmingfig35_16.py (1 of 2)fig35_16.py (2 of 2)35.7 Form Processing and Business Logicfig35_17.html (1 of 3)fig35_17.html (2 of 3)fig35_17.html (3 of 3)Slide 37fig35_18.py (1 of 4)fig35_18.py (2 of 4)fig35_18.py (3 of 4)fig35_18.py (4 of 4)35.8 Cookiesfig35_19.html (1 of 2)fig35_19.html (2 of 2)fig35_20.py (1 of 3)fig35_20.py (2 of 3)fig35_20.py (3 of 3)Slide 48fig35_21.py (1 of 2)fig35_21.py (2 of 2)35.9 Database Application Programming Interface (DB-API)35.9.1 Setup35.9.2 Simple DB-API Programfig35_22.py (1 of 2)fig35_22.py (2 of 2)fig35_23.py 1 of 3fig35_23.py (2 of 3)fig35_23.py (3 of 3)Slide 5935.10 Operator Precedence ChartSlide 61Slide 6235.11 Web Resources 2004 Prentice Hall, Inc. All rights reserved.Chapter 35 – PythonOutline35.1 Introduction35.1.1 First Python Program35.1.2 Python Keywords35.2 Basic Data Types, Control Statements and Functions35.3 Tuples, Lists and Dictionaries35.4 String Processing and Regular Expressions35.5 Exception Handling35.6 Introduction to CGI Programming35.7 Form Processing and Business Logic35.8 Cookies35.9 Database Application Programming Interface (DB-API)35.9.1 Setup35.9.2 Simple DB-API Program35.10 Operator Precedence Chart35.11 Web Resources 2004 Prentice Hall, Inc. All rights reserved.Objectives•In this lesson, you will learn:–To understand basic Python data types.–To understand string processing and regular expressions in Python.–To use exception handling.–To perform basic CGI tasks in Python.–To construct programs that interact with MySQL databases using the Python Database Application Programming Interface (DB-API). 2004 Prentice Hall, Inc. All rights reserved.35.1 Introduction •Python–Interpreted–Cross-platform–Object-oriented–Large-scale Internet search engines–Small administration scripts–GUI applications–CGI scripts–Freely distributed 2004 Prentice Hall, Inc. All rights reserved.35.1.1 First Python Program •Python–Can be executed on a program stored in a file–Can run in interactive mode•Users enter lines of code one at a time•Enables programmers to test small blocks of code quickly 2004 Prentice Hall, Inc.All rights reserved.OutlineOutlinefig35_01.py(1 of 1)1 # Fig. 35.1: fig35_01.py 2 # A first program in Python 3 4 print "Welcome to Python!" Welcome to Python! 2004 Prentice Hall, Inc. All rights reserved.35.1.1 First Python Program Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32 Type "copyright", "credits" or "license" for more information. >>> print "Welcome to Python!" Welcome to Python! >>> ^Z Fig. 35.2 Python in interactive mode. 2004 Prentice Hall, Inc. All rights reserved.35.1.2 Python Keywords •Python is case-sensitive•Keywords can be obtained from keyword module 2004 Prentice Hall, Inc. All rights reserved.35.1.2 Python KeywordsPython keywords and continue else for import not raise assert def except from in or return break del exec global is pass try class elif finally if lambda print while Fig. 35.3 Python keywords. Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32 Type "copyright", "credits" or "license" for more information. >>> import keyword >>> print keyword.kwlist ['and', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while'] >>> Fig. 35.4 Printing Python keywords in interactive mode. 2004 Prentice Hall, Inc. All rights reserved.35.2 Basic Data Types, Control Statements and Functions •Introduces basic data types, control statements, and functions–while loop–if statement–Modulo operator ( % )–return keyword–if…elif…else statement–Line-continuation character ( \ )–Escape sequences 2004 Prentice Hall, Inc.All rights reserved.OutlineOutlinefig35_05.py(1 of 2)1 # Fig. 35.5: fig35_05.py 2 # Program to illustrate basic data types, control structures and 3 # functions. 4 5 def greatestCommonDivisor( x, y ): 6 gcd = min( x, y ) 7 8 while gcd >= 1: 9 10 if ( x % gcd ) == ( y % gcd ) == 0: 11 return gcd 12 else: 13 gcd -= 1 14 15 def determineColor( color ): 16 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." 23 24 number1 = int( raw_input( "Enter a positive integer: " ) ) 25 number2 = int( raw_input( "Enter a positive integer: " ) ) 2004 Prentice Hall, Inc.All rights reserved.OutlineOutlinefig35_05.py(2 of 2)26 27 print "The greatest common divisor is", \ 28 greatestCommonDivisor( number1, number2 ) 29 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. 2004 Prentice Hall, Inc. All rights reserved.35.2 Basic Data Types, Control Statements and FunctionsEscape sequence Meaning \n Newline (line feed). \r Carriage return. \t Tab. \' Single quote. \" Double quote. \b Backspace. \\ Backslash. Fig. 35.6 Escape sequences. 2004


Chapter 35 – Python

Download Chapter 35 – Python
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 35 – Python and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 35 – Python 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?