Here’s a solid reference list of common Python 3+ commands, functions, and their uses. I’ve grouped them into categories so you can see where they fit in everyday coding.
Cheat sheet of Python 3+ commands you’ll use constantly.
That’s a core cheat sheet of Python 3+ commands you’ll use constantly.
Console & Environment
- python3 --version → Check installed Python version.
- python3 file.py → Run a Python script.
- pip install package_name → Install a package from PyPI.
- help(object) → Show help/documentation for an object.
- dir(object) → List available attributes and methods of an object.
Input & Output
- print("Hello") → Output text to console.
- input("Enter: ") → Get user input (as string).
- open("file.txt", "r") → Open a file (modes: "r", "w", "a", "rb", etc.).
- with open("file.txt") as f: → Context manager for safe file handling.
Data Types & Variables
- x = 5 → Assign integer.
- type(x) → Get data type.
- str(), int(), float(), bool() → Type casting.
- isinstance(x, int) → Check if variable is a type.
Strings
- "hello".upper() → "HELLO".
- "HELLO".lower() → "hello".
- "word".replace("w", "s") → "sord".
- "name: {}".format("Steve") → String formatting.
- f"Name: {name}" → f-string formatting.
- "a,b,c".split(",") → ["a","b","c"].
- "".join(["a","b","c"]) → "abc".
Lists
- lst = [1, 2, 3] → Create list.
- lst.append(4) → Add item to end.
- lst.insert(1, 99) → Insert at position.
- lst.remove(2) → Remove first matching value.
- lst.pop() → Remove last item (or by index).
- lst.sort() → Sort list in place.
- sorted(lst) → Return sorted list (new).
- len(lst) → Length of list.
- for x in lst: → Iterate over list.
Dictionaries
- d = {"a": 1, "b": 2} → Create dictionary.
- d["a"] → Access value.
- d.get("a", 0) → Access with default.
- d.keys() → Dictionary keys.
- d.values() → Dictionary values.
- d.items() → Pairs (key, value).
- d.update({"c": 3}) → Add/update keys.
Sets & Tuples
- s = {1,2,3} → Set (unique elements).
- s.add(4) → Add element.
- s.union({3,4,5}) → Merge sets.
- s.intersection({2,4}) → Common elements.
- t = (1,2,3) → Tuple (immutable list).
Control Flow
- if x > 10: → Conditional.
- elif x == 5: → Else-if condition.
- else: → Default block.
- for i in range(5): → Loop from 0–4.
- while x < 10: → While loop.
- break → Exit loop.
- continue → Skip iteration.
Functions
- def add(a, b): return a+b → Define function.
- lambda x: x*2 → Anonymous function.
- map(func, iterable) → Apply function to iterable.
- filter(func, iterable) → Filter iterable.
- sum([1,2,3]) → Built-in sum.
- min(), max(), abs() → Common math helpers.
Classes & OOP
- class Dog: → Define class.
- def __init__(self, name): → Constructor.
- def bark(self): → Instance method.
- d = Dog("Rex") → Create object.
- isinstance(d, Dog) → Check type.
Exceptions
- try: → Start block.
- except Exception as e: → Catch error.
- finally: → Always run.
- raise ValueError("bad") → Throw error.
Modules & Imports
- import math → Import module.
- from math import sqrt → Import function.
- import os → File system commands.
- import sys → System-specific calls.
Virtual Environments
- python3 -m venv env → Create environment.
- source env/bin/activate → Activate (Linux/macOS).
- .\env\Scripts\activate → Activate (Windows).
- deactivate → Exit environment.