Basic cheatsheet for Python.
Based on the book writted by Al Sweigart, Automate the Boring Stuff with Python under the Creative Commons license.
- Python Basics
- Math Operators
- Data Types
- String Concatenation and Replication
- Variables
- Comments
- The print() Function
- The input() Function
- The len() Function
- The str(), int(), and float() Functions
- Flow Control
- Comparison Operators
- Boolean Operators
- Mixing Boolean and Comparison Operators
- if Statements
- else Statements
- elif Statements
- while Loop Statements
- break Statements
- continue Statements
- for Loops and the range() Function
- Importing Modules
- Ending a Program Early with sys.exit()
- Functions
- Return Values and return Statements
- The None Value
- Keyword Arguments and print()
- Local and Global Scope
- The global Statement
- Exception Handling
- Lists
- Getting Individual Values in a List with Indexes
- Negative Indexes
- Getting Sublists with Slices
- Getting a List’s Length with len()
- Changing Values in a List with Indexes
- List Concatenation and List Replication
- Removing Values from Lists with del Statements
- Using for Loops with Lists
- The in and not in Operators
- The Multiple Assignment Trick
- Augmented Assignment Operators
- Finding a Value in a List with the index() Method
- Adding Values to Lists with the append() and insert() Methods
- Removing Values from Lists with remove()
- Sorting the Values in a List with the sort() Method
- Tuple Data Type
- Converting Types with the list() and tuple() Functions
- Dictionaries and Structuring Data
- The keys(), values(), and items() Methods
- Checking Whether a Key or Value Exists in a Dictionary
- The get() Method
- The setdefault() Method
- Pretty Printing
- Manipulating Strings
- Escape Characters
- Raw Strings
- Multiline Strings with Triple Quotes
- Indexing and Slicing Strings
- The in and not in Operators with Strings
- The upper(), lower(), isupper(), and islower() String Methods
- The isX String Methods
- The startswith() and endswith() String Methods
- The join() and split() String Methods
- Justifying Text with rjust(), ljust(), and center()
- Removing Whitespace with strip(), rstrip(), and lstrip()
- Copying and Pasting Strings with the pyperclip Module
- Regular Expressions
- Matching Regex Objects
- Grouping with Parentheses
- Matching Multiple Groups with the Pipe
- Optional Matching with the Question Mark
- Matching Zero or More with the Star
- Matching One or More with the Plus
- Matching Specific Repetitions with Curly Brackets
- Greedy and Nongreedy Matching
- The findall() Method
- Making Your Own Character Classes
- The Caret and Dollar Sign Characters
- The Wildcard Character
- Matching Everything with Dot-Star
- Matching Newlines with the Dot Character
- Review of Regex Symbols
- Case-Insensitive Matching
- Substituting Strings with the sub() Method
- Managing Complex Regexes
- Reading and Writing Files
- Backslash on Windows and Forward Slash on OS X and Linux
- The Current Working Directory
- Absolute vs. Relative Paths
- Creating New Folders with os.makedirs()
- Handling Absolute and Relative Paths
- Finding File Sizes and Folder Contents
- Checking Path Validity
- The File Reading/Writing Process
- Opening Files with the open() Function
- Reading the Contents of Files
- Writing to Files
- Saving Variables with the shelve Module
- Saving Variables with the pprint.pformat() Function
- Copying Files and Folders
- Moving and Renaming Files and Folders
- Permanently Deleting Files and Folders
- Safe Deletes with the send2trash Module
- Walking a Directory Tree
- Reading ZIP Files
- Extracting from ZIP Files
- Creating and Adding to ZIP Files