Python Basics Cheatsheet
Quick reference for Python: syntax, variables, data types, operators, control flow, functions, loops, I/O, and basic data structures.
| Feature | Description | Example | Category |
|---|---|---|---|
| Comments | Single and multi-line comments | # This is a comment """This is a multi-line comment""" | Syntax |
| Indentation | Blocks are defined by indentation | if True: print("Hello") | Syntax |
| Variables | Store data in variables | x = 5 y = "hello" | Variables & Types |
| Data types | int, float, str, bool, None | a = 10 b = 3.14 c = "text" d = True e = None | Variables & Types |
| Arithmetic | +, -, *, /, %, **, // | 5 + 2, 5 ** 2, 5 // 2 | Operators |
| Comparison | ==, !=, >, <, >=, <= | 5 == 5, 3 > 2 | Operators |
| Logical | and, or, not | True and False, not True | Operators |
| if-elif-else | Conditional branching | if x > 0: print("Positive") elif x == 0: print("Zero") else: print("Negative") | Control Flow |
| match-case | Pattern matching (Python 3.10+) | match x: case 1: print("One") case _: print("Other") | Control Flow |
| def | Define a function | def add(a, b): return a + b | Functions |
| lambda | Anonymous function | square = lambda x: x**2 | Functions |
| for loop | Iterate over sequences | for i in range(5): print(i) | Loops |
| while loop | Loop until condition | while x < 5: x += 1 | Loops |
| break / continue | Exit or skip iteration | for i in range(5): if i == 3: break print(i) | Loops |
| Output to console | print("Hello") | I/O | |
| input | Read input from user | name = input("Enter name: ") | I/O |
| List | Ordered, mutable collection | lst = [1, 2, 3] | Data Structures |
| Tuple | Ordered, immutable collection | t = (1, 2, 3) | Data Structures |
| Set | Unordered unique collection | s = {1, 2, 3} | Data Structures |
| Dictionary | Key-value mapping | d = {"a": 1, "b": 2} | Data Structures |