gto76 /python-cheatsheet

Comprehensive Python Cheatsheet

★ 38,913⑂ 6,683👁 953⑃ 0● 27▤ 1◈ 4
PythonCreated 2018-01-25gto76.github.io/python-cheatsheet/
38,913
stars
+6+44+220
6,683
forks
953
watchers
0
prs
27
issues
1
releases
4
discussions

README

Comprehensive Python Cheatsheet
===============================
Download text file, Fork me on GitHub or Check out FAQ.


!Monty Python


Contents
--------
    1. Collections:         List__,__ Dictionary__,__ Set__,__ Tuple__,__ Range__,__ Enumerate__,__ Iterator__,__ Generator__.__  
    2. Data Types:          Type__,__ String__,__ Regular_Exp__,__ Format__,__ Numbers__,__ Combinatorics__,__ Datetime__.__  
    3. Syntax Rules:       Function__,__ Inline__,__ Import__,__ Decorator__,__ Class__,__ Duck_Type__,__ Enum__,__ Except__.__  
    4. System Calls:       Exit__,__ Print__,__ Input__,__ Command_Line_Arguments__,__ Open__,__ Path__,__ OS_Commands__.__  
    5. Data Formats:      JSON__,__ Pickle__,__ CSV__,__ SQLite__,__ Bytes__,__ Struct__,__ Array__,__ Memory_View__,__ Deque__.__  
    6. Misc Topics:         Operator__,__ Match_Statement__,__ Logging__,__ Introspection__,__ Threads__,__ Asyncio__.__  
    7. Pip Packages:      Progress_Bar__,__ Plot__,__ Table__,__ Console_App__,__ GUI__,__ Scraping__,__ Web__,__ Profile__.__  
    8. Multimedia:          NumPy__,__ Image__,__ Animation__,__ Audio__,__ Synthesizer__,__ Pygame__,__ Pandas__,__ Plotly__.__


Main
----
python
if __name__ == '__main__':       Skips indented lines of code if file was imported.
    main()                       Executes user-defined def main(): ... function.



List
----
python
 = [, , ...]       Creates new list object. E.g. list_a = [1, 2, 3].


python
   = [index]           First index is 0, last -1. Also [i] = .
 = []         Also [from_inclusive : to_exclusive : ±step].


python
.append()              Appends element to the end. Or  += [].
.extend()            Appends collection's items. Or  += .


python
.sort()                    Sorts in ascending order. Accepts reverse=True.
.reverse()                 Reverses the order of elements. Takes linear time.
 = sorted()          Returns a new sorted list. Accepts reverse=True.
 = reversed()        Returns reversed iterator. Also list().


python
  = max()              Returns the largest element. Also min(, ).
 = sum()              Returns a sum of elements. Also math.prod().


python
elementwise_sum  = [sum(pair) for pair in zip(list_a, list_b)]
sorted_by_second = sorted(, key=lambda pair: pair[1])
sorted_by_both   = sorted(, key=lambda p: (p[1], p[0]))
flatter_list     = list(itertools.chain.from_iterable())

 For details about sort(), sorted(), max() and min() see Sortable.
 Module operator has function itemgetter() that can replace listed lambdas.
 This text uses the term collection instead of iterable. For rationale see duck types.

python
 = len()   Returns number of items. Doesn't 

Discussions

Latest release

July 2026 jul-2026 2026-07-29
Contains a PDF generated on July 27, 2026

Snapshot: 2026-11-10T09:35:25Z · Data license Apache-2.0 · JSON