0% found this document useful (0 votes)
148 views1 page

Python Cheat Sheet (2009) PDF

This document summarizes key Python variables, methods, and formatting options related to strings, dates, times, files, and classes. It outlines built-in variables like sys.argv and os.environ, common string methods such as capitalize() and count(), datetime methods like today() and strptime(), file methods like read() and write(), and special class methods like __init__() and __repr__(). It also provides details on indexing and slicing lists, available date/time format codes, and notes on week and year calculations.

Uploaded by

Truong Tuan Kiet
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
148 views1 page

Python Cheat Sheet (2009) PDF

This document summarizes key Python variables, methods, and formatting options related to strings, dates, times, files, and classes. It outlines built-in variables like sys.argv and os.environ, common string methods such as capitalize() and count(), datetime methods like today() and strptime(), file methods like read() and write(), and special class methods like __init__() and __repr__(). It also provides details on indexing and slicing lists, available date/time format codes, and notes on week and year calculations.

Uploaded by

Truong Tuan Kiet
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 1

sys Variables String Methods Datetime Methods

argv Command line args capitalize() * lstrip() today() fromordinal(ordinal)


builtin_module_names Linked C modules center(width) partition(sep) now(timezoneinfo) combine(date, time)
byteorder Native byte order count(sub, start, end) replace(old, new) utcnow() strptime(date, format)
check_interval Signal check frequency decode() rfind(sub, start ,end) fromtimestamp(timestamp)
exec_prefix Root directory encode() rindex(sub, start, end) utcfromtimestamp(timestamp)
executable Name of executable endswith(sub) rjust(width)
exitfunc Exit function name expandtabs() rpartition(sep)
Time Methods
modules Loaded modules find(sub, start, end) rsplit(sep)
path Search path index(sub, start, end) rstrip() replace() utcoffset()
platform Current platform isalnum() * split(sep) isoformat() dst()
stdin, stdout, stderr File objects for I/O isalpha() * splitlines() __str__() tzname()
version_info Python version info isdigit() * startswith(sub) strftime(format)
winver Version number islower() * strip()
isspace() * swapcase() *
Date Formatting (strftime and strptime)
istitle() * title() *
sys.argv for $ python foo.py bar -c qux --h
isupper() * translate(table) %a Abbreviated weekday (Sun)
sys.argv[0] foo.py join() upper() * %A Weekday (Sunday)
sys.argv[1] bar ljust(width) zfill(width) %b Abbreviated month name (Jan)
sys.argv[2] -c lower() * %B Month name (January)
sys.argv[3] qux %c Date and time
sys.argv[4] --h Methods marked * are locale %d Day (leading zeros) (01 to 31)
Note dependant for 8-bit strings. %H 24 hour (leading zeros) (00 to 23)
%I 12 hour (leading zeros) (01 to 12)
os Variables
%j Day of year (001 to 366)
List Methods
altsep Alternative sep %m Month (01 to 12)
curdir Current dir string append(item) pop(position) %M Minute (00 to 59)
defpath Default search path count(item) remove(item) %p AM or PM
devnull Path of null device extend(list) reverse() %S Second (00 to 61 4)
extsep Extension separator index(item) sort() %U Week number 1
(00 to 53)
linesep Line separator insert(position, item) %w Weekday 2
(0 to 6)
name Name of OS %W Week number 3
(00 to 53)
pardir Parent dir string %x Date
File Methods
pathsep Patch separator %X Time
sep Path separator close() readlines(size) %y Year without century (00 to 99)
flush() seek(offset) %Y Year (2008)
Registered OS names: “posix”, “nt”, fileno() tell() %Z Time zone (GMT)
Note “mac”, “os2”, “ce”, “java”, “riscos” isatty() truncate(size) %% A literal "%" character (%)
next() write(string)
read(size) writelines(list) 1. Sunday as start of week. All days in a
Class Special Methods
readline(size) new year preceding the first Sunday
__new__(cls) __lt__(self, other) are considered to be in week 0.
__init__(self, args) __le__(self, other) Indexes and Slices (of a=[0,1,2,3,4,5])
__del__(self) __gt__(self, other) 2. 0 is Sunday, 6 is Saturday.
__repr__(self) __ge__(self, other) len(a) 6
__str__(self) __eq__(self, other) a[0] 0 3. Monday as start of week. All days in a
__cmp__(self, other) __ne__(self, other) a[5] 5 new year preceding the first Monday
__index__(self) __nonzero__(self) a[-1] 5 are considered to be in week 0.
__hash__(self) a[-2] 4
__getattr__(self, name) a[1:] [1,2,3,4,5] 4. This is not a mistake. Range takes
__getattribute__(self, name) a[:5] [0,1,2,3,4] account of leap and double-leap
__setattr__(self, name, attr) a[:-2] [0,1,2,3] seconds.
__delattr__(self, name) a[1:3] [1,2]
__call__(self, args, kwargs) a[1:-1] [1,2,3,4]
b=a[:] Shallow copy of a Available free from AddedBytes.com

You might also like