A Byte of Python
Total Page:16
File Type:pdf, Size:1020Kb
A Byte of Python Swaroop C H Dedication To Kalyan Varma43 and many other seniors at PESIT44 who introduced us to GNU/ Linux and the world of open source. To the memory of Atul Chitnis45, a friend and guide who shall be missed greatly. To the pioneers who made the Internet happen46. 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. 43 http://www.kalyanvarma.net/ 44 http://www.pes.edu/ 45 http://www.nextbigwhat.com/atul-chitnis-obituary-297/ 46 http://www.ibiblio.org/pioneers/index.html ii Table of Contents ................................................................................................................................... x 1. Welcome ................................................................................................................ 1 1.1. Who reads A Byte of Python? .................................................................... 1 1.2. Academic Courses ...................................................................................... 9 1.3. License ...................................................................................................... 10 1.4. Read Now ................................................................................................. 10 1.5. Buy The Book ........................................................................................... 10 1.6. Download .................................................................................................. 11 1.7. Read the book in your native language .................................................... 11 Preface ..................................................................................................................... xii 1. Who This Book Is For .................................................................................. xii 2. History Lesson .............................................................................................. xii 3. Status Of The Book ..................................................................................... xiii 4. Official Website ............................................................................................ xiv 5. Something To Think About .......................................................................... xiv 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 ......................................................................... 21 3.4. Summary ................................................................................................... 21 4. First Steps ........................................................................................................... 22 4.1. Using The Interpreter Prompt ................................................................... 22 4.2. Choosing An Editor ................................................................................... 23 4.3. Light Table ................................................................................................ 24 4.4. Vim ............................................................................................................ 25 4.5. Emacs ....................................................................................................... 26 4.6. Using A Source File .................................................................................. 26 4.7. Getting Help .............................................................................................. 29 4.8. Summary ................................................................................................... 29 5. Basics .................................................................................................................. 30 iii A Byte of Python 5.1. Comments ................................................................................................. 30 5.2. Literal Constants ....................................................................................... 30 5.3. Numbers .................................................................................................... 31 5.4. Strings ....................................................................................................... 31 5.4.1. Single Quote ................................................................................... 31 5.4.2. Double Quotes ................................................................................ 31 5.4.3. Triple Quotes .................................................................................. 31 5.4.4. Strings Are Immutable .................................................................... 32 5.4.5. The format method ......................................................................... 32 5.4.6. Escape Sequences ......................................................................... 34 5.4.7. Raw String ...................................................................................... 35 5.5. Variable ..................................................................................................... 35 5.6. Identifier Naming ....................................................................................... 35 5.7. Data Types ................................................................................................ 36 5.8. Object ........................................................................................................ 36 5.9. How to write Python programs .................................................................. 36 5.10. Example: Using Variables And Literal Constants .................................... 36 5.11. Logical And Physical Line ....................................................................... 37 5.12. Indentation ............................................................................................... 39 5.13. Summary ................................................................................................. 40 6. Operators and Expressions ................................................................................. 41 6.1. Operators .................................................................................................. 41 6.2. Shortcut for math operation and assignment ............................................ 44 6.3. Evaluation Order ....................................................................................... 44 6.4. Changing the Order Of Evaluation ............................................................ 46 6.5. Associativity ............................................................................................... 46 6.6. Expressions ............................................................................................... 46 6.7. Summary ................................................................................................... 47 7. Control Flow ........................................................................................................ 48 7.1. The if statement .................................................................................... 48 7.2. The while Statement ................................................................................. 50 7.3. The for loop ........................................................................................... 52 7.4. The break Statement ................................................................................ 53 7.5. The continue Statement ...................................................................... 54 7.6. Summary ................................................................................................... 55 8. Functions ............................................................................................................. 56 8.1. Function Parameters ................................................................................. 57 8.2. Local Variables .......................................................................................... 58 iv A Byte of Python 8.3. The global statement ........................................................................... 59 8.4. Default Argument Values .......................................................................... 60 8.5. Keyword Arguments .................................................................................. 61 8.6. VarArgs parameters .................................................................................. 62 8.7. The return statement ........................................................................... 62 8.8. DocStrings ................................................................................................