Python Programming - PythonAnywhere

Transcription

PYTHON PROGRAMMINGNotes by Michael Brothershttp://titaniumventures.net/library/Table of ContentsINTRODUCTION . 5What is Python? . 5Ways to Code Python . 5Using Anaconda . 5Using Jupyter Notebooks . 5Python 2 vs Python 3 . 5PEP 8 -- Style Guide for Python Code. 5Using this guide: . 5PYTHON PROGRAMMING . 6Variables . 6Multiple Declaration . 6Multiple Assignment . 6Data Types . 6Operators . 6Comparison Operators: . 6Chained Comparison Operators: . 6Strings: . 7Lists: . 7Tuples:. 7Dictionaries:. 7Sets:. 7Comments:. 7WORKING WITH STRINGS . 8Built-in String Functions: . 8Built-in String Methods: . 8Splitting Strings: . 8Joining Strings:. 8Turning Objects Into Strings. 8String formatting with formatted string literals (f-strings). 9String Formatting with .format() . 10String Formatting with Placeholders: . 11Escape Characters: . 11WORKING WITH LISTS: . 12Built-in List Functions: . 12Built-in List Methods: . 12Adding & Removing List Items . 12List Index Method. 12Sorting Lists. 13Making a List of Lists: . 13LIST COMPREHENSIONS . 14WORKING WITH TUPLES . 15WORKING WITH DICTIONARIES . 15Dictionary Comprehensions:. 15WORKING WITH SETS: . 16Set Operators. 161REV 1020

Built-in Set Methods . 16Frozensets . 16RANGE . 16CONDITIONAL STATEMENTS & LOOPS . 17If / Elif / Else statements: . 17For Loops . 17While Loops . 17Nested Loops . 17Loop Control Statements (Break, Continue, Pass & Else) . 18Try and Except . 18INPUT . 19UNPACKING . 19Tuple Unpacking . 19Dictionary Unpacking . 19FUNCTIONS . 20Default Parameter Values . 20Positional Arguments *args and **kwargs . 20PRE-DEFINED FUNCTIONS . 21LAMBDA EXPRESSIONS . 21MORE USEFUL FUNCTIONS . 22MAP . 22FILTER . 22REDUCE . 22ZIP . 22ENUMERATE . 23ALL & ANY . 23COMPLEX . 23PYTHON THEORY & DEFINITIONS . 24SCOPE . 24LEGB Rule: . 24In place. 24Sequenced . 24Iterable. 24Statement . 24Stack. 24FUNCTIONS AS OBJECTS & ASSIGNING VARIABLES . 25FUNCTIONS AS ARGUMENTS . 25DECORATORS:. 26GENERATORS & ITERATORS . 27NEXT & ITER built-in functions:. 27GENERATOR COMPREHENSIONS . 27WORKING WITH FILES . 28READING AND WRITING TO FILES . 28RENAMING & COPYING FILES with the OS MODULE . 28CREATING BINARY FILES with the SHELVE MODULE . 28ORGANIZING FILES with the SHUTIL MODULE . 29DELETING FOLDERS AND FILES . 292REV 1020

Using the OS module: . 29Using the SHUTIL module: . 29Using the Send2Trash module: . 29WALKING A DIRECTORY TREE . 29MANAGING EXCEPTIONS . 30Raising Exceptions . 30Raising Your Own Exception . 30Tracebacks . 30Assertions . 30Logging . 31Logging to a text file . 31Create your own Exception class: . 31OBJECT ORIENTED PROGRAMMING – Classes, Attributes & Methods . 32Classes:. 32Built-in Types: . 32Instances: . 32Attributes:. 32Methods: . 32Inheritance: . 32Special Methods (aka Magic Methods): . 32For Further Reading: . 32Example 1: . 33Example 2 – Methods: . 33Example 3 – Inheritance: . 34Example 4 – Special Methods: . 35MODULES & PACKAGES . 36COLLECTIONS Module: . 37Counter . 37defaultdict . 37OrderedDict . 38namedtuple . 38RANDOM Module . 38DATETIME Module . 39TIMEIT Module . 39PYTHON DEBUGGER – the pdb Module. 40IDLE'S BUILT-IN DEBUGGER. 40REGULAR EXPRESSIONS – the re Module . 41Searching for Patterns in Text . 41Finding all matches . 41Split on multiple delimeters with regular expressions . 41Using metacharacters . 42STYLE AND READABILITY (PEP 8) . 43APPENDIX I: GOING DEEPER: . 45Reserved words . 45The ' ' variable . 46Print on the same line: . 46Print multiple objects using the * unpacking operator . 46Some more (& obscure) built-in string methods:. 473REV 1020

Some more (& obscure) built-in set methods: . 47Common Errors & Exceptions: . 47Using dir() to identify available names and functions . 48Adding a username & password . 48Picking a random rock/paper/scissors . 48Referential Arrays . 48Deep and shallow copies. 48Dynamic Arrays . 49More ways to break out of loops (in "Goto" fashion) . 49Bitwise Operators:. 49How to take an input as a list , tuple or dictionary . 50Unpacking a nested list using sum() . 50Adding an incremental counter as a class attribute . 50APPENDIX II: PYTHON "GOTCHAS" . 511. Default Arguments . 512. Rounding issues in Python . 513. Some methods permanently affect the objects they act on AND RETURN A NONE VALUE . 514. print() returns a None value . 51APPENDIX III: CODE SAMPLES . 52Try and Except . 52Capture Tracebacks to a File . 52Assertions: The Stoplight Example . 52Logging with a Buggy Factorial program . 53Find every target item in a nested list using recursion . 53APPENDIX IV: DIFFERENCES BETWEEN PYTHON 2 and PYTHON 3 . 54APPENDIX V: OTHER RESOURCES . 56Docs, Tutorials and Popular Forums . 56Sharing Code and Version Control . 56For More Practice . 564REV 1020

The following courses and resources aided in the creation of this document:Introduction To Python Programming Instructed by Avinash Jain https://www.udemy.com/pythonforbeginnersintro/The Python Tutorial http://docs.python.org/3/tutorial/Introducing Python: Modern Computing in Simple Packages, 1st ed by Bill Lubanovic (paperback)Complete Python Bootcamp Instructed by Jose Portilla omate the Boring Stuff with Python Instructed by Al Sweigart https://www.udemy.com/automate/INTRODUCT

Python can be run from a command prompt using IDLE (runs an editor & a python shell side-by-side), though it is far more common to write Python programs using a text editor (Notepad, Notepad , Atom, Sublime Text)