A Byte of Python - UC Homepages

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

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

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

Google Online Preview   Download