
Advance Python Tutorials Meher Krishna Patel Created on : Octorber, 2017 Last updated : July, 2019 More documents are freely available at PythonDSP Table of contents Table of contents i 1 Python review 1 1.1 Introduction.................................................1 1.2 Download and Installation.........................................1 1.3 Basics....................................................1 1.3.1 Run code from Python shell (>>>)...............................1 1.3.2 Running Python Files.......................................2 1.3.3 Variables..............................................2 1.3.4 Built-in object types........................................3 1.3.5 Numbers..............................................3 1.3.6 String................................................4 1.3.7 List.................................................4 1.3.8 Tuple................................................5 1.3.9 Dictionary..............................................5 1.4 Number conversion.............................................6 1.4.1 Direct conversion..........................................6 1.4.2 zfill..................................................6 1.5 Control structure..............................................7 1.5.1 if-else................................................7 1.5.2 While loop............................................. 11 1.5.3 For loop............................................... 12 1.6 Function................................................... 13 1.7 Numpy, Scipy and Matplotlib....................................... 14 1.7.1 Arrays................................................ 15 1.8 Good practices............................................... 18 1.8.1 Avoid range command....................................... 18 1.8.2 Enumerate............................................. 18 1.8.3 Loop in sorted order........................................ 19 1.8.4 Loop over keys........................................... 19 1.8.5 Loop over keys and values..................................... 19 1.8.6 Create dictionaries from lists................................... 19 1.8.7 Looping and modifying the list simultaneously......................... 19 1.8.8 Check items in the list....................................... 20 1.8.9 Unpacking.............................................. 20 1.8.10 Update variables.......................................... 21 1.9 Object oriented programming....................................... 21 1.9.1 Class and object.......................................... 21 1.9.2 Create class and object...................................... 21 1.9.3 Inheritance............................................. 24 1.9.4 Polymorphism........................................... 25 1.9.5 Abstract class and method.................................... 26 1.9.6 Public and private attribute.................................... 27 i 1.9.7 Class Attribute........................................... 29 1.9.8 Special methods.......................................... 30 1.10 Conclusion................................................. 34 2 Virtual environment, Package and Distribution 35 2.1 Anadconda virtual environment...................................... 35 2.1.1 Installation............................................. 35 2.1.2 Creating virtual environment................................... 35 2.1.3 Other commands.......................................... 37 2.2 Virtual environment using virtualenv and virtualenvw......................... 38 2.2.1 Install virtualenv and virtualenvwrapper............................. 38 2.2.2 Modify .bashrc........................................... 39 2.2.3 Create virtualenv.......................................... 39 2.3 Packages................................................... 39 2.3.1 Location of installed packages................................... 39 2.3.2 Create files............................................. 40 2.3.3 Packages............................................... 41 2.3.4 Globally available package..................................... 42 2.3.5 __init__ file............................................ 42 2.3.6 __all__ in __init__ file.................................... 43 2.4 Distribute the package........................................... 44 2.5 Conclusion................................................. 45 3 Debugging 46 3.1 Introdution................................................. 46 3.2 First code.................................................. 46 3.3 Reading input from command line.................................... 46 3.4 Debugging.................................................. 47 3.4.1 Run script and go to Python shell................................ 47 3.4.2 Python debugger (pdb)...................................... 48 3.5 Underscore operator (_).......................................... 48 3.6 Conclusion................................................. 49 4 Print statement 50 4.1 Introduction................................................. 50 4.2 Expansion calculation........................................... 50 4.3 Print the expansion............................................. 51 4.4 Formatted output.............................................. 52 4.5 Saving results in file............................................ 53 4.6 Alter the printing sequence........................................ 55 4.7 Conclusion................................................. 55 5 CSV module 56 5.1 Introduction................................................. 56 5.2 Basic file operations............................................ 56 5.2.1 Open and close the files...................................... 56 5.2.2 with - open statement....................................... 57 5.3 Strings operations............................................. 58 5.4 Perform calculations............................................ 59 5.5 Problem with current method....................................... 60 5.6 CSV module................................................ 61 5.6.1 csv.reader.............................................. 61 5.6.2 csv.DictReader........................................... 62 5.7 Conclusion................................................. 63 6 Functions 64 6.1 docstring.................................................. 64 6.2 types of docstring.............................................. 64 6.3 Convert previous code into function................................... 64 6.3.1 Conversion............................................. 64 6.3.2 Debugging.............................................. 65 6.4 glob module................................................. 65 6.4.1 Price calculation on files using ‘glob’............................... 66 7 Data types and objects 67 7.1 Introduction................................................. 67 7.2 Identity and type.............................................. 67 7.2.1 ‘is’ operator............................................. 68 7.2.2 Comparing objects......................................... 68 7.2.3 isinstance.............................................. 69 7.3 Reference count and garbage collection.................................. 69 7.3.1 getrefcount............................................. 69 7.3.2 Garbage collection......................................... 70 7.3.3 Shallow and deep copy....................................... 70 7.4 First class object.............................................. 71 7.5 Builtin types for representing data.................................... 71 7.5.1 None................................................. 72 7.5.2 Numbers.............................................. 72 7.5.3 Sequences.............................................. 73 7.5.4 List................................................. 74 7.5.5 Strings................................................ 76 7.5.6 Mapping types........................................... 79 7.5.7 Set types.............................................. 81 7.6 Builtin types for representing program structure............................ 83 7.6.1 Callable types............................................ 83 7.6.2 Class, types and instances..................................... 86 7.6.3 Modules............................................... 87 7.7 Special methods.............................................. 87 7.7.1 Object creation and destruction.................................. 87 7.7.2 String representation........................................ 87 7.7.3 Type checking........................................... 88 7.7.4 Attribute access.......................................... 88 7.7.5 Descriptors............................................. 88 7.7.6 Sequence and mapping methods................................. 88 7.7.7 Iteration............................................... 89 7.7.8 Callable interface.......................................... 89 7.7.9 Context management protocol.................................. 90 7.7.10 Object inspection and dir().................................... 90 8 Exception handling 91 8.1 Introduction................................................. 91 8.2 try-except block............................................... 91 8.3 Report error................................................. 92 8.3.1 Type of error............................................ 92 8.3.2 Location of error.......................................... 93 8.4 Catch all error (bad practice)....................................... 94 8.5 Silencing error............................................... 96 8.6 List of
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages206 Page
-
File Size-