Cheat Sheets

Python Cheat Sheet

Quick reference for Python syntax and built-ins — variables, strings, lists, dicts, loops, functions, classes, file I/O, and more.

Read. Transform. Automate.

Getting Started
print()Variablesinput()type()CommentMultiline comment
Data Types
strintfloatboollisttupledictsetint()float()str()bool()
Arithmetic & Operators
AdditionSubtractionMultiplicationFloat divisionInteger divisionModuloExponentiationCompound assignmentandornotis
Strings
Index accessSlicingSlice from startSlice to endReverse stringlen()ConcatenationRepeatin operatorLoop charactersjoin()split()strip()replace()lower() / upper()endswith()
String Formatting
f-string (basic)f-string (expression)f-string (decimals)f-string (zero pad)f-string (thousands)f-string (percentage)f-string (binary)f-string (hex)format() method% formatting
Lists
Create listFrom rangeappend()extend()pop()delIndex accessSlicingsort()reverse()count()len()Repeat listList comprehensionfilter()
Tuples & Sets
TupleTuple unpackSetset.add()set.remove()UnionIntersectionDifference
Dictionaries
Create dictAccess valueget()Add / updateupdate()del keykeys()values()items()Loop dictDict comprehensionin operator
Control Flow
if / elif / elseTernary operatorpass
Loops
for loopwhile looprange()enumerate()zip()breakcontinuefor / else
Functions
Define functionDefault argument*args**kwargsMultiple returnLambdamap()
Classes & OOP
Define classInstantiateMethodClass variableInheritancesuper()__repr__()Custom exception
File Handling
Read fileRead line by lineWrite fileWrite JSONRead JSONDelete fileCheck file exists
Modules
importfrom importimport asdir()
Error Handling
try / exceptMultiple exceptionselse clausefinally clauseraise
Generators & Advanced
Generator functionGenerator expressionheapq (min-heap)dequeenumerate (start)