Code & computing / QUICK REFERENCE
Python essentials
Built-in types, comprehensions, iteration, functions and common standard-library patterns.
Values and strings
- Floating-point division
7 / 2Returns 3.5. [1]
- Floor division
7 // 2Returns 3; negative results round downward. [1]
- Remainder
7 % 2Returns 1. [1]
- Formatted string
name = 'Atlas'; message = f'Hello, {name}'Substitutes the variable into a new string. [1]
- Strip surrounding whitespace
' atlas '.strip()Does not change the original immutable string. [1]
- Split words
'one two'.split()Splits on runs of whitespace. [1]
- Join strings
', '.join(['one', 'two'])Every joined element must be a string. [1]
- Slice a string
'abcdef'[1:4]Returns bcd; the stop index is excluded. [1]
Lists, dictionaries and sets
- Create a list
items = [1, 2, 3]Lists preserve order and allow duplicates. [2]
- Last element
items[-1]Raises IndexError for an empty list. [2]
- Append one element
items.append(4)Mutates the list and returns None. [2]
- Create a dictionary
record = {'name': 'Atlas', 'count': 3}Keys map to values. [2]
- Lookup with fallback
record.get('missing', 0)Uses the fallback only when the key is absent. [2]
- Dictionary entries
list(record.items())Returns key-value pairs in insertion order. [2]
- Unique values
set([1, 1, 2])A set removes duplicates; do not rely on display order. [2]
- Membership
'name' in recordTests dictionary keys, not values. [2]
Iteration and functions
- Index while iterating
for i, value in enumerate(['a', 'b']): print(i, value)- Small function
def square(x): return x * xIndentation defines the function body. [3]
- Conditional expression
label = 'even' if 4 % 2 == 0 else 'odd'Selects one expression's value. [3]
- Identity check for None
value is NonePrefer identity rather than == for this singleton. [3]
Useful standard-library patterns
- Open text explicitly
with open('notes.txt', encoding='utf-8') as f: text = f.read()The context manager closes the file. [4]
- Parse JSON
import json; record = json.loads('{"count": 3}')Parses data; do not use eval for JSON. [7]
- Serialize JSON
json.dumps({'count': 3})Import json first; custom objects need handling. [7]
- Build a filesystem path
from pathlib import Path; path = Path('data') / 'notes.txt'Avoids manual separator concatenation. [8]
- Count values
from collections import Counter; counts = Counter('banana')Produces frequencies by distinct element. [9]
- Current UTC time
from datetime import datetime, timezone; now = datetime.now(timezone.utc)Creates an aware datetime rather than a naive one. [10]
Reserved placement, separate from reference controls. No ad network is active.
No matching entry. Try a shorter word or clear the search to see the complete sheet.
Sources
cheatsheetatlas.com/sheets/python/ · Updated 2026-10-19 · Python 3 · portable core syntax