680 likes | 888 Views
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
E N D
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
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).
35.1 Introduction • Python • Interpreted • Cross-platform • Object-oriented • Large-scale Internet search engines • Small administration scripts • GUI applications • CGI scripts • Freely distributed
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
35.1.1 First Python Program Fig. 35.2 Python in interactive mode.
35.1.2 Python Keywords • Python is case-sensitive • Keywords can be obtained from keyword module
35.1.2 Python Keywords Fig. 35.4 Printing Python keywords in interactive mode.
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
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
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
35.5 Exception Handling • Enables programs and programmers to identify an error when it occurs and take appropriate action • try…except blocks
35.5 Exception Handling Fig. 35.14 Interactive session illustrating a ZeroDivisionError exception.
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
35.7 Form Processing and Business Logic • XHTML forms • Allow users to enter data to be sent to a Web server for processing
35.8 Cookies • Small text files • State information • Username • Password • Specific information helpful when user returns • Etc.