Python Cheat Sheet
Entirely made by @AbdulMalikDev: https://github.com/AbdulMalikDev https://github.com/AbdulMalikDev/PythonCheatSheet
If Table of Contents doesn't show, reduce zoom to 90%.
https://app.gitbook.com/welcome
A Cheat Sheet 📜 to revise Python syntax in less time. Particularly useful for solving Data Structure and Algorithmic problems or a quick overview before an interview.
Click here for similar Java Resource (not made by me) Get a PDF of this sheet at the end. Leave a ⭐ if you like the cheat sheet (contributions welcome!)
Basics
Data Types

Untitled Operators and it’s precedence

Untitled
Data Structures
Important data structures for LeetCode
Lists
Lists are used to store multiple items in a single variable
Operations Time Complexities

Untitled
List or String slicing in Python
Resource
Dictionary
Dictionaries are used to store data values in key:value pairs. Info about collections.Counter() available below.
Operations Time Complexities

Untitled
Counter
Python Counter is a container that will hold the count of each of the elements present in the container. The counter is a sub-class available inside the dictionary class. Specifically used for element frequencies
Pretty similar to dictionary, in fact I use defaultdict(int) most of the time
Deque
A double-ended queue, or deque, has the feature of adding and removing elements from either end.
Operations Time Complexities

Untitled
Heapq
As we know the Heap Data Structure is used to implement the Priority Queue ADT. In python we can directly access a Priority Queue implemented using a Heap by using the Heapq library/module.
Operations Time Complexities

Untitled
Sets
A set is a collection which is unordered, immutable, unindexed, No Duplicates.
Tuples
A tuple is a collection which is ordered, unchangeable and can contain duplicate values
Operations Time Complexities
Similar to list
Strings
Built-in or Library functions
Functions to iterate over list / other iterable (tuple, dictionaries)
Getting ASCII value of a character
Clean Code Tips
Doc Strings - Documentation for your functions in the interview to look slick 😎
A docstring is short for documentation string.
Python docstrings (documentation strings) are the string literals that appear right after the definition of a function, method, class, or module.
Triple quotes are used while writing docstrings. For example:
Docstrings appear right after the definition of a function, class, or a module. This separates docstrings from multiline comments using triple quotes.
The docstrings are associated with the object as their
__doc__attribute.So, we can access the docstrings of the above function with the following lines of code:
Output
Use Assert keyword in python for testing edge cases. Looks more professional.
Definition and UsageThe
assertkeyword is used when debugging code.The
assertkeyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError.You can write a message to be written if the code returns False, check the example below.
ALWAYS be aware of any code snippet that is being REPEATED in your solution. MODULARITY #1 Priority. Refactoring is also an important part of interview.
This is usually asked as a follow up after coding the solution. Are there any changes you want to make to this solution?
Miscellaneous
How to take multiple line input in python?
Taking multiple inputs from user in Python - GeeksforGeeks
Using split() method
Using List comprehension
Syntax :
ExampleImportant Python Math Functions
Python Math Module - GeeksforGeeks
Log Function
Log functions in Python - GeeksforGeeks
Finding the ceiling and the floor value
Ceil value means the smallest integral value greater than the number and the floor value means the greatest integral value smaller than the number. This can be easily calculated using the ceil() and floor() method respectively.
Other Important functions
Python cmp_to_key function to sort list with custom compare function
Sort a list of lists with a custom compare function
How the custom comparator worksWhen providing a custom comparator, it should generally return an integer/float value that follows the following pattern (as with most other programming languages and frameworks):
return a negative value (
< 0) when the left item should be sorted before the right itemreturn a positive value (
> 0) when the left item should be sorted after the right itemreturn
0when both the left and the right item have the same weight and should be ordered "equally" without precedence
Python integer division doesn’t work properly with -ve numbers ex: -3//2 will give -2 answer instead of -1 so always use int(-3/2) for integer division in problems
Resources
PDF with all Python Data Structures in-depth
Last updated