Python Cheat Sheet

Comprehensive reference for Python 3. Standard Library, Strings, Math, and System commands.

Top Content Ad (Responsive)

String Operations

String Formatting (%)
Code Description
%d / %i Signed integer decimal
%o Signed octal value
%u Obsolete (same as %d)
%x Hexadecimal (lowercase)
%X Hexadecimal (uppercase)
%e Exponential (lowercase)
%E Exponential (uppercase)
%f Floating point decimal
%F Floating point decimal
%g Floating point format
%c Single character
%r String (using repr())
%s String (using str())
%% Literal '%' character
Date Formatting (strftime)
Code Description
%a Abbreviated weekday (Sun)
%A Weekday (Sunday)
%b Abbreviated month (Jan)
%B Month name (January)
%c Date and time
%d Day (01 to 31)
%H 24 hour (00 to 23)
%I 12 hour (01 to 12)
%j Day of year (001-366)
%m Month (01 to 12)
%M Minute (00 to 59)
%p AM or PM
%S Second (00 to 61)
%U Week number
%w Weekday (0=Sun)
%x Date representation
%X Time representation
%y Year (00-99)
%Y Year (2025)
%Z Time zone

File System

Containers (Lists, Sets, Arrays)

Array / List Indexing

Example: a = [0, 1, 2, 3, 4, 5]

Code Result
len(a) 6
a[0] 0
a[5] 5
a[-1] 5 (Last)
a[-2] 4
a[1:] [1, 2, 3, 4, 5]
a[:5] [0, 1, 2, 3, 4]
a[:-2] [0, 1, 2, 3]
a[1:3] [1, 2]
b = a[:] Shallow Copy
Dictionaries (Map)

Date & Time

Math & Random

Sys & OS

Sys Variables
  • argv Command line args
  • path Search path
  • platform Current platform
  • stdin/out I/O Objects
  • version Python version
  • exit() Exit function
OS Variables
  • name Name of OS
  • sep Path separator
  • getcwd() Current Dir
  • listdir() List files
  • mkdir() Make dir
  • remove() Delete file
Bottom Content Ad (Responsive)