Python Basics Cheatsheet

Quick reference for Python: syntax, variables, data types, operators, control flow, functions, loops, I/O, and basic data structures.

FeatureDescriptionExampleCategory
CommentsSingle and multi-line comments# This is a comment """This is a multi-line comment"""Syntax
IndentationBlocks are defined by indentationif True: print("Hello")Syntax
VariablesStore data in variablesx = 5 y = "hello"Variables & Types
Data typesint, float, str, bool, Nonea = 10 b = 3.14 c = "text" d = True e = NoneVariables & Types
Arithmetic+, -, *, /, %, **, //5 + 2, 5 ** 2, 5 // 2Operators
Comparison==, !=, >, <, >=, <=5 == 5, 3 > 2Operators
Logicaland, or, notTrue and False, not TrueOperators
if-elif-elseConditional branchingif x > 0: print("Positive") elif x == 0: print("Zero") else: print("Negative")Control Flow
match-casePattern matching (Python 3.10+)match x: case 1: print("One") case _: print("Other")Control Flow
defDefine a functiondef add(a, b): return a + bFunctions
lambdaAnonymous functionsquare = lambda x: x**2Functions
for loopIterate over sequencesfor i in range(5): print(i)Loops
while loopLoop until conditionwhile x < 5: x += 1Loops
break / continueExit or skip iterationfor i in range(5): if i == 3: break print(i)Loops
printOutput to consoleprint("Hello")I/O
inputRead input from username = input("Enter name: ")I/O
ListOrdered, mutable collectionlst = [1, 2, 3]Data Structures
TupleOrdered, immutable collectiont = (1, 2, 3)Data Structures
SetUnordered unique collections = {1, 2, 3}Data Structures
DictionaryKey-value mappingd = {"a": 1, "b": 2}Data Structures