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/welcomearrow-up-right

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)arrow-up-right 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

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 tuplearrow-up-right is a collection which is ordered, unchangeable and can contain duplicate values

  • Operations Time Complexities

    Similar to list

Strings

Python String isnumeric()arrow-up-right

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 stringarrow-up-right 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 Usage

    The assert keyword is used when debugging code.

    The assert keyword 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

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

The Modulo Operation (%) With Negative Numbers in Pythonarrow-up-right

Last updated