Introduction to Python Programming Course Notes

Introduction to Python Programming

Course Notes

Phil Spector

Department of Statistics, University of California Berkeley

March 16, 2005

2

Contents

1 Introduction

1.1 What is Python? . . . . . . . . . . . . .

1.2 The very Basics of Python . . . . . . . .

1.3 Invoking Python . . . . . . . . . . . . .

1.4 Basic Principles of Python . . . . . . . .

1.4.1 Basic Core Language . . . . . . .

1.4.2 Modules . . . . . . . . . . . . . .

1.4.3 Object Oriented Programming . .

1.4.4 Namespaces and Variable Scoping

1.4.5 Exception Handling . . . . . . . .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2 String Data

2.1 String Constants . . . . . . . . . . . . . . .

2.2 Special Characters and Raw Strings . . . . .

2.3 Unicode Strings . . . . . . . . . . . . . . . .

2.4 String Operations . . . . . . . . . . . . . . .

2.4.1 Concatenation . . . . . . . . . . . . .

2.4.2 Repetition . . . . . . . . . . . . . . .

2.4.3 Indexing and Slicing . . . . . . . . .

2.4.4 Functions and Methods for Character

3 Numeric Data

3.1 Types of Numeric Data . . . . . . . . . .

3.1.1 Hexadecimal and Octal Constants

3.1.2 Numeric Operators . . . . . . . .

3.1.3 Functions for Numeric Data . . .

3.2 Conversion of Scalar Types . . . . . . . .

3

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

Strings

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

7

7

8

8

11

11

12

12

13

15

.

.

.

.

.

.

.

.

17

17

18

19

19

19

21

21

23

.

.

.

.

.

29

29

31

31

32

33

4

CONTENTS

4 Lists, Tuples and Dictionaries

4.1 List Data . . . . . . . . . . . . . . . .

4.2 List Indexing and Slicing . . . . . . . .

4.3 List Operators . . . . . . . . . . . . . .

4.3.1 Concatenation . . . . . . . . . .

4.3.2 Repetition . . . . . . . . . . . .

4.3.3 The in operator . . . . . . . . .

4.4 Functions and Methods for Lists . . . .

4.5 Tuple Objects . . . . . . . . . . . . . .

4.6 Operators and Indexing for Tuples . .

4.7 Functions and Methods for Tuples . . .

4.8 Dictionaries . . . . . . . . . . . . . . .

4.9 Functions and Methods for Dictionaries

5 Input and Output

5.1 The print command . . . . . . . . .

5.2 Formatting Strings . . . . . . . . . .

5.3 Using Names in Format Strings . . .

5.4 File Objects . . . . . . . . . . . . . .

5.4.1 Methods for Reading . . . . .

5.4.2 Methods for Writing . . . . .

5.4.3 Printing to a File . . . . . .

5.4.4 Other Methods . . . . . . . .

5.4.5 File Object Variables . . . . .

5.5 Standard Input and Output Streams

5.6 Pipes . . . . . . . . . . . . . . . . . .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

6 Programming

6.1 Assignments . . . . . . . . . . . . . . . .

6.2 Indentation . . . . . . . . . . . . . . . .

6.3 Truth, Falsehood and Logical Operators

6.4 if statement . . . . . . . . . . . . . . .

6.5 for loops . . . . . . . . . . . . . . . . .

6.6 for loops and the range function . . . .

6.7 while loops . . . . . . . . . . . . . . . .

6.8 Control in Loops: break and continue .

6.9 List Comprehensions . . . . . . . . . . .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

37

37

39

42

42

43

43

44

48

49

49

51

52

.

.

.

.

.

.

.

.

.

.

.

55

55

55

57

57

59

60

60

61

61

62

63

.

.

.

.

.

.

.

.

.

67

67

72

72

74

76

78

80

82

84

CONTENTS

7 Functions

7.1 Introduction . . . . . . . . . . . . . . . . . . . . . .

7.2 Scoping: How Python finds your variables . . . . .

7.3 Function Basics . . . . . . . . . . . . . . . . . . . .

7.4 Named Arguments and Default Values . . . . . . .

7.5 Variable Number of Arguments . . . . . . . . . . .

7.6 Functional Programming, and anonymous functions

5

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

87

87

88

92

93

96

98

8 Using Modules

103

8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

8.2 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

8.3 Functions for working with modules . . . . . . . . . . . . . . . 106

8.4 The string module . . . . . . . . . . . . . . . . . . . . . . . . 106

8.4.1 String Constants . . . . . . . . . . . . . . . . . . . . . 106

8.4.2 Functions in the string module . . . . . . . . . . . . . 107

8.5 The re module: Regular Expressions . . . . . . . . . . . . . . 109

8.5.1 Introduction to Regular Expressions . . . . . . . . . . 109

8.5.2 Constructing Regular Expressions . . . . . . . . . . . . 109

8.5.3 Compiling Regular Expressions . . . . . . . . . . . . . 110

8.5.4 Finding Regular Expression Matches . . . . . . . . . . 111

8.5.5 Tagging in Regular Expressions . . . . . . . . . . . . . 113

8.5.6 Using Named Groups for Tagging . . . . . . . . . . . . 115

8.5.7 Greediness of Regular Expressions . . . . . . . . . . . . 116

8.5.8 Multiple Matches . . . . . . . . . . . . . . . . . . . . . 117

8.5.9 Substitutions . . . . . . . . . . . . . . . . . . . . . . . 119

8.6 Operating System Services: os and shutil modules . . . . . . 121

8.7 Expansion of Filename wildcards - the glob module . . . . . . 125

8.8 Information about your Python session - the sys module . . . 126

8.9 Copying: the copy module . . . . . . . . . . . . . . . . . . . . 127

8.10 Object Persistence: the pickle/cPickle and shelve modules 128

8.10.1 Pickling . . . . . . . . . . . . . . . . . . . . . . . . . . 128

8.10.2 The shelve module . . . . . . . . . . . . . . . . . . . . 130

8.11 CGI (Common Gateway Interface): the cgi module . . . . . . 131

8.11.1 Introduction to CGI . . . . . . . . . . . . . . . . . . . 131

8.11.2 Security Concerns . . . . . . . . . . . . . . . . . . . . . 134

8.11.3 CGI Environmental Variables . . . . . . . . . . . . . . 135

8.12 Accessing Documents on the Web: the urllib module . . . . 135

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download