Chapter 35 Python Outline 35 1 Introduction 35

  • Slides: 63
Download presentation
Chapter 35 – Python Outline 35. 1 Introduction 35. 1. 1 First Python Program

Chapter 35 – Python Outline 35. 1 Introduction 35. 1. 1 First Python Program 35. 1. 2 Python Keywords 35. 2 Basic Data Types, Control Statements and Functions 35. 3 Tuples, Lists and Dictionaries 35. 4 String Processing and Regular Expressions 35. 5 Exception Handling 35. 6 Introduction to CGI Programming 35. 7 Form Processing and Business Logic 35. 8 Cookies 35. 9 Database Application Programming Interface (DB-API) 35. 9. 1 Setup 35. 9. 2 Simple DB-API Program 35. 10 Operator Precedence Chart 35. 11 Web Resources 2004 Prentice Hall, Inc. All rights reserved.

Objectives • In this lesson, you will learn: – To understand basic Python data

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 My. SQL 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

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

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.

Outline fig 35_01. py (1 of 1) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_01. py (1 of 1) 2004 Prentice Hall, Inc. All rights reserved.

35. 1. 1 First Python Program Fig. 35. 2 Python in interactive mode. 2004

35. 1. 1 First Python Program 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

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 Keywords Fig. 35. 4 Printing Python keywords in interactive mode.

35. 1. 2 Python Keywords 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,

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…else statement – Line-continuation character ( ) – Escape sequences 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_05. py (1 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_05. py (1 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_05. py (2 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_05. py (2 of 2) 2004 Prentice Hall, Inc. All rights reserved.

35. 2 Basic Data Types, Control Statements and Functions 2004 Prentice Hall, Inc. All

35. 2 Basic Data Types, Control Statements and Functions 2004 Prentice Hall, Inc. All rights reserved.

35. 3 Tuples, Lists and Dictionaries • list – A sequence of related data

35. 3 Tuples, Lists and Dictionaries • list – A sequence of related data • Tuple – A list whose elements may not be modified – Immutable – Singleton • One-element tuple • Dictionary – A list of values that are accessed through their associated keys 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_07. py (1 of 3) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_07. py (1 of 3) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_07. py (2 of 3) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_07. py (2 of 3) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_07. py (3 of 3) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_07. py (3 of 3) 2004 Prentice Hall, Inc. All rights reserved.

 2004 Prentice Hall, Inc. All rights reserved.

2004 Prentice Hall, Inc. All rights reserved.

35. 3 Tuples, Lists and Dictionaries 2004 Prentice Hall, Inc. All rights reserved.

35. 3 Tuples, Lists and Dictionaries 2004 Prentice Hall, Inc. All rights reserved.

35. 3 Tuples, Lists and Dictionaries 2004 Prentice Hall, Inc. All rights reserved.

35. 3 Tuples, Lists and Dictionaries 2004 Prentice Hall, Inc. All rights reserved.

35. 4 String Processing and Regular Expressions • Regular expression – Used to search

35. 4 String Processing and Regular Expressions • Regular expression – Used to search through strings, text files, databases, and so on • Regular-expression string – Defines a pattern with which text data can be compared 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_10. py (1 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_10. py (1 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_10. py (2 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_10. py (2 of 2) 2004 Prentice Hall, Inc. All rights reserved.

35. 4 String Processing and Regular Expressions 2004 Prentice Hall, Inc. All rights reserved.

35. 4 String Processing and Regular Expressions 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_12. py (1 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_12. py (1 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_12. py (2 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_12. py (2 of 2) 2004 Prentice Hall, Inc. All rights reserved.

35. 4 String Processing and Regular Expressions 2004 Prentice Hall, Inc. All rights reserved.

35. 4 String Processing and Regular Expressions 2004 Prentice Hall, Inc. All rights reserved.

35. 5 Exception Handling • Enables programs and programmers to identify an error when

35. 5 Exception Handling • Enables programs and programmers to identify an error when it occurs and take appropriate action • try…except blocks 2004 Prentice Hall, Inc. All rights reserved.

35. 5 Exception Handling Fig. 35. 14 Interactive session illustrating a Zero. Division. Error

35. 5 Exception Handling Fig. 35. 14 Interactive session illustrating a Zero. Division. Error exception. 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_15. py (1 of 1) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_15. py (1 of 1) 2004 Prentice Hall, Inc. All rights reserved.

35. 6 Introduction to CGI Programming • Use Python on the Web – cgi

35. 6 Introduction to CGI Programming • Use Python on the Web – cgi • For access to XHTML forms • Directive (pound-bang or Shebang) – Provides server with location of Python executable • cgi module – Provides functionalities for writing CGI scripts • environ data member – Holds all environment variables – Cookie • To read and write cookies – smtplib • To manipulate SMTP messages – urllib • To manipulate Web data – ftplib • To perform client-side FTP tasks 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_16. py (1 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_16. py (1 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_16. py (2 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_16. py (2 of 2) 2004 Prentice Hall, Inc. All rights reserved.

35. 7 Form Processing and Business Logic • XHTML forms – Allow users to

35. 7 Form Processing and Business Logic • XHTML forms – Allow users to enter data to be sent to a Web server for processing 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_17. html (1 of 3) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_17. html (1 of 3) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_17. html (2 of 3) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_17. html (2 of 3) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_17. html (3 of 3) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_17. html (3 of 3) 2004 Prentice Hall, Inc. All rights reserved.

 2004 Prentice Hall, Inc. All rights reserved.

2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_18. py (1 of 4) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_18. py (1 of 4) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_18. py (2 of 4) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_18. py (2 of 4) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_18. py (3 of 4) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_18. py (3 of 4) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_18. py (4 of 4) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_18. py (4 of 4) 2004 Prentice Hall, Inc. All rights reserved.

35. 8 Cookies • Small text files – State information • • Username Password

35. 8 Cookies • Small text files – State information • • Username Password Specific information helpful when user returns Etc. 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_19. html (1 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_19. html (1 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_19. html (2 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_19. html (2 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_20. py (1 of 3) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_20. py (1 of 3) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_20. py (2 of 3) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_20. py (2 of 3) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_20. py (3 of 3) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_20. py (3 of 3) 2004 Prentice Hall, Inc. All rights reserved.

 2004 Prentice Hall, Inc. All rights reserved.

2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_21. py (1 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_21. py (1 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_21. py (2 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_21. py (2 of 2) 2004 Prentice Hall, Inc. All rights reserved.

35. 9 Database Application Programming Interface (DB-API) • Python Database Special Interest Group (SIG)

35. 9 Database Application Programming Interface (DB-API) • Python Database Special Interest Group (SIG) – Formed to develop a specification for Python database application programming interface (DB-API) 2004 Prentice Hall, Inc. All rights reserved.

35. 9. 1 Setup • My. SQL and the My. SQLdb module must be

35. 9. 1 Setup • My. SQL and the My. SQLdb module must be installed 2004 Prentice Hall, Inc. All rights reserved.

35. 9. 2 Simple DB-API Program • Lets user choose an author from XHTML

35. 9. 2 Simple DB-API Program • Lets user choose an author from XHTML dropdown list • User clicks button to query database • Database query returns a list of all books by author 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_22. py (1 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_22. py (1 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_22. py (2 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_22. py (2 of 2) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_23. py 1 of 3 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_23. py 1 of 3 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_23. py (2 of 3) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_23. py (2 of 3) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_23. py (3 of 3) 2004 Prentice Hall, Inc. All rights reserved.

Outline fig 35_23. py (3 of 3) 2004 Prentice Hall, Inc. All rights reserved.

 2004 Prentice Hall, Inc. All rights reserved.

2004 Prentice Hall, Inc. All rights reserved.

35. 10 Operator Precedence Chart 2004 Prentice Hall, Inc. All rights reserved.

35. 10 Operator Precedence Chart 2004 Prentice Hall, Inc. All rights reserved.

35. 10 Operator Precedence Chart 2004 Prentice Hall, Inc. All rights reserved.

35. 10 Operator Precedence Chart 2004 Prentice Hall, Inc. All rights reserved.

35. 10 Operator Precedence Chart 2004 Prentice Hall, Inc. All rights reserved.

35. 10 Operator Precedence Chart 2004 Prentice Hall, Inc. All rights reserved.

35. 11 Web Resources • • • • • www. python. org www. zope.

35. 11 Web Resources • • • • • www. python. org www. zope. com www. zope. org starship. python. net www. python. org/download_mac. html www. vex. net/parnassus www. pythonware. com starship. python. net/crew/davem/cgifaq/faqw. cgi www. devshed. com/Server_Side/Python/CGI starship. python. net/crew/aaron_watters/pws. html www. python. org/doc/howto/regex. html www. python. org/windows/win 32 com gadfly. sourceforge. net aspn. activestate. com/ASPN/Python/Cookbook www. python. org/windows/win 32/odbc. html starship. python. net/crew/bwilk/access. html www. python. org/doc/Comparisons. html www. vic. auug. org. au/auugvic/av_paper_python. html www. networkcomputing. com/unixworld/tutorial/005. html 2004 Prentice Hall, Inc. All rights reserved.