0% found this document useful (0 votes)
9 views19 pages

Let Us Python - 4th Edition

The document is a comprehensive guide to Python programming, covering its basics, advanced concepts, and various programming paradigms. It includes detailed sections on data structures, control flow, functions, object-oriented programming, and libraries like Numpy. The guide also emphasizes Python's popularity due to its ease of use, portability, and extensive community support.

Uploaded by

bickyyadav27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
9 views19 pages

Let Us Python - 4th Edition

The document is a comprehensive guide to Python programming, covering its basics, advanced concepts, and various programming paradigms. It includes detailed sections on data structures, control flow, functions, object-oriented programming, and libraries like Numpy. The guide also emphasizes Python's popularity due to its ease of use, portability, and extensive community support.

Uploaded by

bickyyadav27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 19
| CS tT YASHAVANT KANETKAR | Pe CN eae Python Is Future, Embrace It Fast eT a UC TCL 7 A Programmer-Friendly Guide aS esbychon | as 2 Par 5 he: re ge a ers) \ y eign. oe fe 7 i y i MD) Gish a: ae eee ee Ee leas f 4 Ve \ iy Javan, (Poy worn nue wne 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Brief Contents Introduction to Python Getting Started. Python Basics Strings and Regular Expressions Decision Control Instruction Repetition Control Instruction Console Input/Output Lists $$ Tuples cecmmnrnsincmseh Oeics Sets Dictionaries Comprehensions Functions Recursion _.cO Functional Programming Modules and Packages Namespaces —$———————————————— Classes and Objects Intricacies of Classes and Objects Containership and Inheritance Iterators and Generators Exception Handling = File Input/Output Miscellany Concurrency and Parallelism, Synchronization vii 19 41 63 77 89 99 117 129 139 153 165 183 199 213 225 235 253 267 285 307 325 341 363 27> Numpy Library Appendix A - Precedence Table, Appendix B - Debugging In Python. Appendix C - Chasing the Bugs. Periodic Tests Index vill 40; 414 42) 42) 433 1 2 2 Contents Introduction to Python What is Python? Reasons for Popularity What sets Python apart? Where is Python used? Who uses Python today? Programming Paradigms Functional Programming Model Procedural Programming Model Object-oriented Programming Model Event-driven Programming Model Exercises Getting Started Python Specification Python Installation under Windows Python Installation under Linux Python Resources Third-party Packages More Sophisticated Tools Compilation Approach in C, C++ Compilation Approach in Python Pros and Cons of Traditional Approach Pros and Cons of Modern Approach Working with Python Python Programming Modes Determining Python Version Exercises Python Basics Identifiers and Keywords Python Types Basic Types Integer and Float Ranges Variable Type and Assignment Arithmetic Operators Operation Nuances Precedence and Associativity Conversions Built-in Functions 4 5 6 Built-in Modules Container Types Python Type Jargon Comments and Indentation Mutti-tining Classes and Objects Multiple Objects Programs Exercises Strings and Regular Expressions. What are Strings? Accessing String Elements. String Properties Built-in Functions String Methods String Conversions String Comparisons’ Byte Sequences Regular Expressions Regex Functionality Regex Metacharacters Regex Repetition Qualifiers Regex Anchors Regex Grouping Programs Exercises Decision Control Instruction, Decision Control Instruction Nuances of Conditions Logical Operators Conditional Expressions all() and any() Receiving Input pass Statement Programs Exercises Repetition Control Instruction Repetition Control Instruction Usage of while Loop Usage of for Loop break and continue 6 28 28 29 29 30 31 32 39 a1 42 42 43 44 45 46 46 47 48 48 50 51 51 59 63 65 65 66 67 67 68 68 73 71 78 78 79 81 Else Block of a Loop 81 Programs. 82 Exercises 85 7 Console Input/Output 89 Console Input 90 Console Output 91 Formatted Printing 91 Programs 93 Exercises 96 8 Lists 99 What are Lists? 100 Accessing List Elements 100 Looping in Lists 101 Basic List Operations 101 Using Built-in Functions on Lists 104 List Methods 105 Sorting and Reversing 105 List Varieties 106 Stack Data Structure 107 Queue Data Structure 107 Programs 107 Exercises 113 9 Tuples 117 What are Tuples? 118 Accessing Tuple Elements 118 Looping in Tuples 119 Basic Tuple Operations 119 Using Built-in Functions on Tuples 120 Tuple Methods 121 Tuple Varieties 121 Programs 122 Exercises 127 10 Sets 129 What are Sets? 130 Accessing Set Elements 131 Looping in Sets 131 Basic Set Operations 131 Using Built-in Functions on Sets 132 Set Methods 132 Mathematical Set Operations 133 Updating Set Operations 134 134 134 Set Varieties. Programs Exercises 137 12 Dictionaries 8 What are Dictionaries? 149, Accessing Dictionary Elements 149 Looping in Dictionaries 14] Basic Dictionary Operations 141 Using Built-in Functions on Dictionaries 142 Dictionary Methods 143 Dictionary Varieties 143 Programs 144 Exercises 149 12 Comprehensions 153 What are Comprehensions? 154 List Comprehension 154 Set Comprehension 155 Dictionary Comprehension 156 Programs 156 Exercises 162 13 Functions 165 What are Functions? 166 Communication with Functions 167 Types of Arguments 168 Unpacking Arguments a1 Programs 172 Exercises 181 14 Recursion 183 Repetitions 184 Recursive Function 184 When to use Recursion 185 Problem as Similar Sub-problems 185 Recursive Factorial Function 186 Problem with Unknown Loops 187 Types of Recursion : 189 Recursion Limit 190 Iteration to Recursion 190 Programs 190 Exercises 196 15 Functional Programming 199 Functional Programming 200 xil Functions as First-class Values. 200 Lambda Functions 201 Higher Order Functions 202 Map, Filter, Reduce 203 map( ) Function 203 filter( ) Function 204 reduce( ) Function 204 Using Lambda with map( ), filter( ), reduce() 205 Where are they Useful? 206 Programs 207 Exercises 211 16 Modules and Packages 213 The Main Module 214 Multiple Modules 214 Importing a Module 215 Variations of import 216 Search Sequence 216 Same Code, Different Interpretation 217 Packages 217 Third-party Packages 218 Programs 219 Exercises 223 17 Namespaces 225 Symbol Table 226 Namespace 226 globals( ) and locals( ) 227 Where to use them? 228 Inner Functions 229 Scope and LEGB Rule 230 Programs 231 Exercises 233 18 Classes and Objects 235 Programming Paradigms 236 What are Classes and Objects? 236 Classes and Objects in Programming 237 User-defined Classes 237 Access Convention 239 Object Initialization 240 Class Variables and Methods 242 vars( ) and dir( ) Functions 242 More vars{) and dir( ) 247 xili Programs 2M, Exercises 2ny 19 _ Intricacies of Classes and Objects 25) Identifier Naming Convention 254 Calling Functions and Methods 255, Operator Overloading 256, Which Operators to Overload 257 Everything is an Object 288 Imitating a Structure 260 Type Conversion 260 Programs 261 Exercises 264 20 = ©Containership and Inheritance___ 2G? Reuse Mechanisms 268 Which to use When? 268 Containership 268 Inheritance 269 What is Accessible where? 271 isinstance{ ) and issubclass( ) 272 The object Class . 273 Features of Inheritance 273 Types of Inheritance 273 Diamond Problem 275 Abstract Classes 276 Runtime Polymorphism 277 Programs 277 Exercises 282 21 __ Iterators and Generators 285 Iterables and Iterators 286 zip( ) Function 286 Iterators 287 User-defined Iterators 289 Generators 290 Which to use When? 291 Generator Expressions 291 Programs 292 Exercises 305 22 ~~ Exception Handling 807 What may go Wrong? 308 Syntax Errors 308 Exceptions 309 xiv 23 24 ~~ Miscellany How to deal with Exceptions? How to use try - except? Nuances of try and except User-defined Exceptions else Block finally Block Exception Handling Tips Programs Exercises File Input/Output 1/0 System File /O Read / Write Operations File Opening Modes with Keyword Moving within a File Serialization and Deserialization Serialization of User-defined Types File and Directory Operations Programs Exercises Documentation Strings Command-line Arguments Parsing of Command-line Bitwise Operators Assertion Decorators Decorating Functions with Arguments. Unicode bytes Datatype Create Executable File Programs Exercises 25 Concurrency and Parallelism Concurrency and Parallelism What are Threads? Concurrency and Parallelism in Programming CPU-bound and I/O-bound Programs. Which to use When? Concurrency for Improving Performance xv 310 311 312 313 316 317 317 317 322 325 326 326 327 328 329 329 330 333 334 335 338 341 342 343 344 346 347 348 350 353 353 355 356 361 363 364 365 366 366 362 Types of Concurrencies 36p Thread Properties 36% Launching Threads 369 Passing Parameters to a Thread 379 Programs. 379 Exercises 378 26 Synchronization 38] Synchronization 382 Examples of Sharing Resources 382 Example of Communication between Threads 383 Mechanisms for Sharing Resources 383 Lock 383 RLock 384 Semaphore 388 Mechanisms for Inter-thread Communication 388 Event 386 Condition 386 Programs 387 Exercises 401 27° Numpy Library 403 Creation of Array 404 Creation of Filler Arrays 405 Array Attributes 406 Array Operations 407 Arithmetic Operations 407 Statistical Operations 408 Linear Algebra Operations 408 Bitwise Operations 409 Copying and Sorting 409 Comparison 410 Indexing and Slicing 411 Array Manipulation 412 Programs 413 Exercises 417 Appendix A - Precedence Table 419 Appendix B - Debugging in Python 421 Appendix C - Chasing the Bugs 427 Appendix D - Periodic Tests 433 Index 447 xvi Introduction to Cieza What is Python? Functional Programming Model Reasons for Popularity Procedural Programming Model What sets Python apart? © Object-oriented Programming Model Where is Python Used? e Event-driven Programming Model Who uses Python today? © Exercises Programming Paradigms 2 Let Us Python What is Python? Python is a high-level programming language created by Guido Van_ Rossum - fondly known as Benevolent Dictator For Life. * Python was first released in 1991. Today Python interpreters are available for many Operating Systems including Windows and Linux. « Python programmers are often called Pythonists or Pythonistas. Reasons for Popularity «There are severa’ reasons for Python’s popularity. These include: (a) Free: - Python is free to use and distribute and is supported by community. - Python interpreter is available for every major platform. (b) Software quality: - Better than traditional and scripting languages. - Readable code, hence reusable and maintainable. - Support for advance reuse mechanisms. Developer productivity: - Much better than statically typed languages. - Much smaller code. - Less to type, debug and maintain. - No lengthy compile and link steps. (c (d) Program portability: - Python programs run unchanged on most platforms. - Python runs on every major platform currently in use. - Porting program to a new platform usually need only cut and paste. This is true even for GUI, DB access, Web programming, OS interfacing, Directory access, etc. (e) Support libraries: - Strong library support from Text pattern matching to networking. - Vast collection of third-party libraries. - Libraries for Web site construction, Numeric programming, Game development, Machine Learning etc. Chapter 1: Introduction to Python __ 3 (f) Component integration: = Can invoke C, C++ libraries and Java components. = Can communicate with frameworks such as COM, .NET. - Can interact over networks with interfaces like SOAP, XML-RPC, CORBA. - With appropriate glue code, Python can subclass C++, Java, CH. classes, thereby extending the reach of the program. - Popularly used for product customization and extension. (g) Enjoyment: - Ease of use. - Built-in toolset. - Programming becomes pleasure than work. What sets Python apart? (a) Powerful: - Dynamic typing. - No variable declaration. - Automatic allocation and Garbage Collection. - Supports classes, modules and exceptions. = Permits componentization and reuse. - Powerful containers - Lists, Dictionaries, Tuples, etc. (b) Ready-made stuff: - Support for operations like joining, slicing, sorting, mapping, etc. = Powerful library. - Large collection of third-party utilities. (c) Ease of use: - Type and run. - No compile and link steps. - Interactive programming experience. - Rapid turnaround. - Programs are simpler, smaller and more flexible. Where is Python used? * Python is used for multiple purposes. These include: (a) System programming (b) Building GUI applications (c) Internet scripting 4 Let Us Python (d) Component integration (e) Database programming (f) Rapid prototyping (g) Numeric and Scientific programming (h) Game programming (i) Robotics programming Who uses Python today? © Many organizations use Python for varied purposes. These include: (a) Google - In web search system (b) YouTube - Video Sharing service (c) Bit-torrent - Peer to Peer file sharing system (d) Intel, HP, Seagate, IBM, Qualcomm - Hardware testing (e) Pixar, Industrial Light and Magic - Movie animation (f) JP Morgan, Chase, UBS - Financial market forecasting (g) NASA, Fermilab - Scientific programming (h) iRobot - Commercial robot vacuum cleaners (i) NSA- Cryptographic and Intelligence analysis (j) IronPort - Email Servers Programming Paradigms Paradigm means organization principle. It is also known as model. «Programming paradigm/model is a style of building the structure and elements of computer programs. There exist many programming models like Functional, Procedural, Object-oriented, Event-driven, etc. ¢ Many languages facilitate programming in one or more paradigms. For example, Python supports Functional, Procedural, Object- oriented and Event-driven programming models. Chapter 1: Introduction to Python 5 * There are situations when Functional programming is the obvious choice, and other situations where Procedural programming is the better choice. * Paradigms are not meant to be mutually exclusive. A single program may use multiple paradigms. 7 " Exompse. Functional Programming Model * Functional programming decomposes a problem into a set of functions. These functions provide the main source of logic in the program. * Functions take input parameters and produce outputs. Python provides functional programming techniques like lambda, map, reduce and filter. These are discussed in Chapter 15. eee * In this model computation is treated as evaluation of mathematical functions. For example, to get factorial value of a number, or n‘* Fibonacci number we can use the following functions: © The output value of a function depends only on its arguments, so calling a function with the same value for an argument always produces the same result. As a result, it is a good fit for parallel execution. * No function can have side effects on other variables (state remains unaltered). * Functional_programming model is often called a Declarative’ programming paradigm as programming is done with expressions or declarations instead of statements. Procedural Programming Model Procedural programming solves the problem by implementing one _ statement (a_procedure)-at-a_time. Thus, it contains explicit steps that are executed in a specific order. 6 Let Us Python * It also uses functions, but these are not mathematical functions like the ones used in functional programming. Functional programming focuses on expressions, whereas Procedural programming focuses ‘on statements. ~~ * The statements don't have values and instead modify the state of some conceptual machine. * Same language expression can result in different values at different times depending on the global state of the executing program. Also, the functions may change a program's state. on’ necessery * Procedural programming model is often called ‘Imperative’ programming as it changes state with an explicit sequence of statements. Object-oriented Programming Model * This model mimics the real world by creating inside the computer a mini-world of objects. Ina University system objects can be VC, Professors, Non-teaching staff, students, courses, semesters, examinations, etc. * Each object has a state (values) and behavior (interface/methods). Objects get state and behavior based on the class from which it created. © Objects interact with one another by sending messages to each other, i.e., by calling each other’s interface methods. Event-driven Programming Model This model is popularly used for programming GUI applications containing elements like windows, check boxes, buttons, combo- boxes, scroll bars, menus, etc. © When we interact with these elements (like clicking a button, or moving the scrollbar or selecting a menu item) events occur and these elements emit messages. There are listener methods which are registered with these GUI elements which react to these events. * Since there is no guaranteed sequence in which events may occur (based on how we interact with GUI elements), the listeners should be able to handle them in asynchronous manner. Chapter 1; Introduction to Python é & ; EXqum TA] Answer the following: (a) Mention 5 fields in which Python is popularly used. (b) Where is event-driven programming popularly used? (c) Why Python is called portable language? (d) What is the single most important feature of different programming models discussed in this chapter? (e) Which of the following is not a feature of Python? - Static typing = Dynamic typing = Run-time error handling through error numbers - Library support for containers like Lists, Dictionaries, Tuples (f) Give an example application of each of the following programming models: - Functional model - Procedural model - Object-oriented model - Event-driven model [B] State whether the following statements are True or False: (a) Python is free to use and distribute. (b) Same Python program can work on different OS - microprocessor combinations. (c) Itis possible to use C++ or Java libraries in a Python program. (d) In Python type of the variable is decided based on its usage. (e) Python cannot be used for building GUI applications. (f) Python supports functional, procedural, object-oriented and event- driven programming models. (g) GUI applications are based on event-driven programming model. 8 SO —8 bet Us Python (h) Functional programming model consists of interaction of multiple objects. [C]_ Match the following pairs: a. Functional programming 1. GUI element based interaction b, Event-driven programming 2. Interaction of objects c. Procedural programming 3. Statements d. OOP 4, Maths-like functions [0] Fill in the blanks: (a) Functional programming paradigm is also known as programming model. (b) Procedural programming paradigm is also known as programming model. (c) Python was created by (d) Python programmers are often called

You might also like