A Byte of Python - UC Homepages

[Pages:177]A Byte of Python

Swaroop C H

Dedication

To Kalyan Varma46 and many other seniors at PESIT47 who introduced us to GNU/ Linux and the world of open source. To the memory of Atul Chitnis48, a friend and guide who shall be missed greatly. To the pioneers who made the Internet happen49. 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 ................................................................................................. 11 1.5. Buy The Book ........................................................................................... 11 1.6. Download .................................................................................................. 11 1.7. Read the book in your native language .................................................... 12 Preface .................................................................................................................... xiii 1. Who This Book Is For ................................................................................. xiii 2. Official Website ............................................................................................ xiii 3. Something To Think About .......................................................................... xiii 2. Introduction ......................................................................................................... 15 2.1. Features of Python ................................................................................... 15 2.2. Python 2 versus 3 .................................................................................... 17 2.3. What Programmers Say ........................................................................... 18 3. Installation ........................................................................................................... 19 3.1. Installation on Windows ............................................................................ 19

3.1.1. DOS Prompt ................................................................................... 19 3.1.2. Running Python prompt on Windows ............................................. 20 3.2. Installation on Mac OS X .......................................................................... 20 3.3. Installation on GNU/Linux ......................................................................... 20 3.4. Summary ................................................................................................... 21 4. First Steps ........................................................................................................... 22 4.1. Using The Interpreter Prompt ................................................................... 22 4.2. Choosing An Editor ................................................................................... 23 4.3. PyCharm ................................................................................................... 24 4.4. Vim ............................................................................................................ 33 4.5. Emacs ....................................................................................................... 33 4.6. Using A Source File ................................................................................. 33 4.7. Getting Help .............................................................................................. 36 4.8. Summary ................................................................................................... 36 5. Basics .................................................................................................................. 37 5.1. Comments ................................................................................................. 37 5.2. Literal Constants ....................................................................................... 37

iii

A Byte of Python

5.3. Numbers ................................................................................................... 38 5.4. Strings ....................................................................................................... 38

5.4.1. Single Quote ................................................................................... 38 5.4.2. Double Quotes ................................................................................ 38 5.4.3. Triple Quotes .................................................................................. 38 5.4.4. Strings Are Immutable .................................................................... 39 5.4.5. The format method ......................................................................... 39 5.4.6. Escape Sequences ......................................................................... 41 5.4.7. Raw String ...................................................................................... 42 5.5. Variable ..................................................................................................... 42 5.6. Identifier Naming ....................................................................................... 42 5.7. Data Types ............................................................................................... 43 5.8. Object ........................................................................................................ 43 5.9. How to write Python programs ................................................................. 43 5.10. For PyCharm .......................................................................................... 43 5.11. For other editors ..................................................................................... 44 5.12. Example: Using Variables And Literal Constants .................................... 44 5.13. Logical And Physical Line ....................................................................... 45 5.14. Indentation .............................................................................................. 47 5.15. Summary ................................................................................................. 48 6. Operators and Expressions ................................................................................ 49 6.1. Operators .................................................................................................. 49 6.2. Shortcut for math operation and assignment ............................................ 52 6.3. Evaluation Order ....................................................................................... 52 6.4. Changing the Order Of Evaluation ........................................................... 54 6.5. Associativity .............................................................................................. 54 6.6. Expressions ............................................................................................... 54 6.7. Summary ................................................................................................... 55 7. Control Flow ........................................................................................................ 56 7.1. The if statement ........................................................................................ 56 7.2. The while Statement ................................................................................. 58 7.3. The for loop .............................................................................................. 60 7.4. The break Statement ................................................................................ 61 7.5. The continue Statement ............................................................................ 62 7.6. Summary ................................................................................................... 63 8. Functions ............................................................................................................. 64 8.1. Function Parameters ................................................................................. 65 8.2. Local Variables ......................................................................................... 66

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

A Byte of Python

12.4. The init method............................................................................... 110 12.5. Class And Object Variables .................................................................. 111 12.6. Inheritance ............................................................................................ 114 12.7. Summary ............................................................................................... 117 13. Input and Output ............................................................................................. 118 13.1. Input from user ..................................................................................... 118

13.1.1. Homework exercise .................................................................... 119 13.2. Files ...................................................................................................... 119 13.3. Pickle .................................................................................................... 121 13.4. Unicode ................................................................................................. 122 13.5. Summary ............................................................................................... 123 14. Exceptions ....................................................................................................... 124 14.1. Errors .................................................................................................... 124 14.2. Exceptions ............................................................................................. 124 14.3. Handling Exceptions ............................................................................. 125 14.4. Raising Exceptions ............................................................................... 126 14.5. Try ... Finally ........................................................................................ 127 14.6. The with statement ............................................................................... 128 14.7. Summary ............................................................................................... 129 15. Standard Library ............................................................................................. 130 15.1. sys module ............................................................................................ 130 15.2. logging module ..................................................................................... 131 15.3. Module of the Week Series .................................................................. 132 15.4. Summary ............................................................................................... 133 16. More ................................................................................................................ 134 16.1. Passing tuples around .......................................................................... 134 16.2. Special Methods ................................................................................... 134 16.3. Single Statement Blocks ....................................................................... 135 16.4. Lambda Forms ...................................................................................... 136 16.5. List Comprehension .............................................................................. 136 16.6. Receiving Tuples and Dictionaries in Functions ................................... 137 16.7. The assert statement ............................................................................ 137 16.8. Decorators ............................................................................................. 138 16.9. Differences between Python 2 and Python 3 ........................................ 140 16.10. Summary ............................................................................................. 140 17. What Next ....................................................................................................... 141 17.1. Next Projects ........................................................................................ 141 17.2. Example Code ...................................................................................... 142

vi

A Byte of Python

17.3. Advice ................................................................................................... 142 17.4. Videos ................................................................................................... 142 17.5. Questions and Answers ........................................................................ 142 17.6. Tutorials ................................................................................................ 143 17.7. Discussion ............................................................................................. 143 17.8. News ..................................................................................................... 143 17.9. Installing libraries .................................................................................. 143 17.10. Creating a Website ............................................................................. 144 17.11. Graphical Software ............................................................................. 144 17.12. Summary of GUI Tools ....................................................................... 145 17.13. Various Implementations ..................................................................... 145 17.14. Functional Programming (for advanced readers) ................................ 146 17.15. Summary ............................................................................................. 146 18. Appendix: FLOSS ........................................................................................... 148 19. Appendix: History Lesson ............................................................................... 153 19.1. Status Of The Book .............................................................................. 153 20. Appendix: Revision History ............................................................................. 155 21. Translations ..................................................................................................... 158 21.1. Arabic .................................................................................................... 158 21.2. Brazilian Portuguese ............................................................................. 158 21.3. Catalan .................................................................................................. 158 21.4. Chinese ................................................................................................. 159 21.5. Chinese Traditional ............................................................................... 160 21.6. French ................................................................................................... 160 21.7. German ................................................................................................. 161 21.8. Greek .................................................................................................... 162 21.9. Indonesian ............................................................................................. 162 21.10. Italian .................................................................................................. 163 21.11. Japanese ............................................................................................. 163 21.12. Korean ................................................................................................ 163 21.13. Mongolian ............................................................................................ 164 21.14. Norwegian (bokm?l) ............................................................................ 164 21.15. Polish .................................................................................................. 165 21.16. Portuguese .......................................................................................... 165 21.17. Romanian ............................................................................................ 165 21.18. Russian ............................................................................................... 165 21.19. Ukranian .............................................................................................. 166 21.20. Serbian ................................................................................................ 166

vii

A Byte of Python 21.21. Slovak ................................................................................................. 166 21.22. Spanish ............................................................................................... 166 21.23. Swedish ............................................................................................... 167 21.24. Turkish ................................................................................................ 167 22. Translation Howto ........................................................................................... 168

viii

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

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

Google Online Preview   Download