A Byte of Python - UC Homepages

A Byte of Python

Swaroop C H

Dedication

46

To Kalyan Varma and many other seniors at PESIT

Linux and the world of open source.

To the memory of Atul Chitnis

48

47

who introduced us to GNU/

, a friend and guide who shall be missed greatly.

49

To the pioneers who made the Internet happen . This book was first written in 2003.

It still remains popular, thanks to the nature of sharing knowledge on the Internet as

envisioned by the pioneers.

46



47



48



49



ii

Table of Contents

.................................................................................................................................. ix

1. Welcome ............................................................................................................... 1

1.1. Who reads A Byte of Python? .................................................................... 1

1.2. Academic Courses .................................................................................... 10

1.3. License ...................................................................................................... 10

1.4. Read Now .................................................................................................

1.5. Buy The Book ...........................................................................................

1.6. Download ..................................................................................................

1.7. Read the book in your native language ....................................................

Preface ....................................................................................................................

1. Who This Book Is For .................................................................................

2. Official Website ............................................................................................

3. Something To Think About ..........................................................................

2. Introduction .........................................................................................................

2.1. Features of Python ...................................................................................

2.2. Python 2 versus 3 ....................................................................................

2.3. What Programmers Say ...........................................................................

3. Installation ...........................................................................................................

3.1. Installation on Windows ............................................................................

3.1.1. DOS Prompt ...................................................................................

3.1.2. Running Python prompt on Windows .............................................

3.2. Installation on Mac OS X ..........................................................................

3.3. Installation on GNU/Linux .........................................................................

3.4. Summary ...................................................................................................

4. First Steps ...........................................................................................................

4.1. Using The Interpreter Prompt ...................................................................

4.2. Choosing An Editor ...................................................................................

4.3. PyCharm ...................................................................................................

4.4. Vim ............................................................................................................

4.5. Emacs .......................................................................................................

4.6. Using A Source File .................................................................................

4.7. Getting Help ..............................................................................................

4.8. Summary ...................................................................................................

5. Basics ..................................................................................................................

5.1. Comments .................................................................................................

5.2. Literal Constants .......................................................................................

iii

11

11

11

12

xiii

xiii

xiii

xiii

15

15

17

18

19

19

19

20

20

20

21

22

22

23

24

33

33

33

36

36

37

37

37

A Byte of Python

5.3. Numbers ................................................................................................... 38

5.4. Strings ....................................................................................................... 38

5.4.1. Single Quote ...................................................................................

5.4.2. Double Quotes ................................................................................

5.4.3. Triple Quotes ..................................................................................

5.4.4. Strings Are Immutable ....................................................................

5.4.5. The format method .........................................................................

5.4.6. Escape Sequences .........................................................................

5.4.7. Raw String ......................................................................................

Variable .....................................................................................................

Identifier Naming .......................................................................................

Data Types ...............................................................................................

Object ........................................................................................................

How to write Python programs .................................................................

38

38

38

39

39

41

42

42

42

43

43

43

5.10. For PyCharm ..........................................................................................

5.11. For other editors .....................................................................................

5.12. Example: Using Variables And Literal Constants ....................................

5.13. Logical And Physical Line .......................................................................

5.14. Indentation ..............................................................................................

5.15. Summary .................................................................................................

6. Operators and Expressions ................................................................................

6.1. Operators ..................................................................................................

6.2. Shortcut for math operation and assignment ............................................

6.3. Evaluation Order .......................................................................................

6.4. Changing the Order Of Evaluation ...........................................................

6.5. Associativity ..............................................................................................

6.6. Expressions ...............................................................................................

6.7. Summary ...................................................................................................

7. Control Flow ........................................................................................................

7.1. The if statement ........................................................................................

7.2. The while Statement .................................................................................

7.3. The for loop ..............................................................................................

7.4. The break Statement ................................................................................

7.5. The continue Statement ............................................................................

7.6. Summary ...................................................................................................

8. Functions .............................................................................................................

8.1. Function Parameters .................................................................................

8.2. Local Variables .........................................................................................

43

44

44

45

47

48

49

49

52

52

54

54

54

55

56

56

58

60

61

62

63

64

65

66

5.5.

5.6.

5.7.

5.8.

5.9.

iv

A Byte of Python

8.3. The global statement ................................................................................ 67

8.4. Default Argument Values .......................................................................... 68

8.5. Keyword Arguments .................................................................................. 69

8.6. VarArgs parameters .................................................................................. 70

8.7. The return statement ................................................................................ 70

8.8. DocStrings ................................................................................................ 71

8.9. Summary ................................................................................................... 73

9. Modules ............................................................................................................... 74

9.1. Byte-compiled .pyc files ............................................................................ 76

9.2. The from ¡­ import statement ................................................................... 76

9.3. A module¡¯s name .................................................................................... 76

9.4. Making Your Own Modules ...................................................................... 77

9.5. The dir function ......................................................................................... 79

9.6. Packages .................................................................................................. 80

9.7. Summary ................................................................................................... 81

10. Data Structures ................................................................................................. 82

10.1. List .......................................................................................................... 82

10.2. Quick Introduction To Objects And Classes ........................................... 82

10.3. Tuple ....................................................................................................... 84

10.4. Dictionary ................................................................................................ 86

10.5. Sequence ................................................................................................ 88

10.6. Set ........................................................................................................... 91

10.7. References .............................................................................................. 91

10.8. More About Strings ................................................................................. 93

10.9. Summary ................................................................................................. 94

11. Problem Solving ................................................................................................ 95

11.1. The Problem ........................................................................................... 95

11.2. The Solution ............................................................................................ 96

11.3. Second Version ...................................................................................... 99

11.4. Third Version ........................................................................................ 101

11.5. Fourth Version ...................................................................................... 103

11.6. More Refinements ................................................................................. 105

11.7. The Software Development Process .................................................... 105

11.8. Summary ............................................................................................... 106

12. Object Oriented Programming ........................................................................ 107

12.1. The self ................................................................................................. 108

12.2. Classes ................................................................................................. 108

12.3. Methods ................................................................................................ 109

v

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

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

Google Online Preview   Download