Python Cheat Sheet

GlowTouch 4,361 views 2 slides Aug 13, 2015
Slide 1
Slide 1 of 2
Slide 1
1
Slide 2
2

About This Presentation

This Python cheat sheet summarizes the most commonly-used language features. It is ideal for those new to learning this language.


Slide Content

Enabling Your Growth Through
www.GlowTouch.com Application Development & Support Services
Python Cheat Sheet
Python sys Variables

Python List Methods
argv Command line args

append(item) pop(position)
builtin_module_names Linekd C modules

count(item) remove(item)
byteorder Native byte order

extend(list) reverse()
check_interval Signal check frequency

index(item) sort()
exec_prefix Root directory

insert(position, item)
executable Name of executable
exitfunc Exit function name

Python String Methods
modules Loaded modules

capitalize() * lstrip()
path Search path

center(width) partition(sep)
platform Current platform

count(sub, start, end) replace(old, new)
stdin, stdout, stderr File objects for I/O

decode() rfind(sub, start ,end)
version_info Python version info

encode() rindex(sub, start, end)
winver Version number

endswith(sub) rjust(width)

expandtabs() rpartition(sep)
Python sys.argv

find(sub, start, end) rsplit(sep)
sys.argv[0] foo.py

index(sub, start, end) rstrip()
sys.argv[1] bar

isalnum() * split(sep)
sys.argv[2] -c

isalpha() * splitlines()
sys.argv[3] qux

isdigit() * startswith(sub)
sys.argv[4] --h

islower() * strip()
sys.argv for the command:

isspace() * swapcase() *
$ python foo.py bar -c qux --h

istitle() * title() *

isupper() * translate(table)
Python os Variables

join() upper() *
altsep Alternative sep

ljust(width) zfill(width)
curdir Current dir string

lower() *
defpath Default search path

Methods marked * are locale dependant for
devnull Path of null device

8-bit strings.
extsep Extension separator
linesep Line separator

Python File Methods
name Name of OS

close() readlines(size)
pardir Parent dir string

flush() seek(offset)
pathsep Patch separator

fileno() tell()
sep Path separator

isatty() truncate(size)
Registered OS names: "posix", "nt",

next() write(string)
"mac", "os2", "ce", "java", "riscos"

read(size) writelines(list)

readline(size)

Enabling Your Growth Through
www.GlowTouch.com Application Development & Support Services
Python Cheat Sheet
Python Class Special Methods

Python Date Formatting
__new__(cls) __lt__(self, other)

%a Abbreviated weekday (Sun)
__init__(self, args) __le__(self, other)

%A Weekday (Sunday)
__del__(self) __gt__(self, other)

%b Abbreviated month name (Jan)
__repr__(self) __ge__(self, other)

%B Month name (January)
__str__(self) __eq__(self, other)

%c Date and time
__cmp__(self, other) __ne__(self, other)

%d Day (leading zeros) (01 to 31)
__index__(self) __nonzero__(self)

%H 24 hour (leading zeros) (00 to 23)
__hash__(self)

%I 12 hour (leading zeros) (01 to 12)
__getattr__(self, name)

%j Day of year (001 to 366)
__getattribute__(self, name)

%m Month (01 to 12)
__setattr__(self, name, attr)

%M Minute (00 to 59)
__delattr__(self, name)

%p AM or PM
__call__(self, args, kwargs)

%S Second (00 to 61⁴)

%U Week number¹ (00 to 53)
Python Indexes and Slices

%w Weekday² (0 to 6)
len(a) 6

%W Week number³ (00 to 53)
a[0] 0

%x Date
a[5] 5

%X Time
a[-1] 5

%y Year without century (00 to 99)
a[-2] 4

%Y Year (2008)
a[1:] [1,2,3,4,5]

%Z Time zone (GMT)
a[:5] [0,1,2,3,4]

%% A literal "%" character (%)
a[:-2] [0,1,2,3]

¹ Sunday as start of week. All days in a new
a[1:3] [1,2]

year preceding the first Sunday are
a[1:-1] [1,2,3,4]

considered to be in week 0.
b=a[:] Shallow copy of a

² 0 is Sunday, 6 is Saturday.
Indexes and Slices of a=[0,1,2,3,4,5]

³ Monday as start of week. All days in a new

year preceding the first Monday are
Python Datetime Methods

considered to be in week 0.
today() fromordinal(ordinal)

⁴ This is not a mistake. Range takes account
now(timezoneinfo) combine(date, time)

of leap and double-leap seconds.
utcnow() strptime(date, format)
fromtimestamp(timestamp)
utcfromtimestamp(timestamp)

Python Time Methods
replace() utcoffset()
isoformat() dst()
__str__() tzname()
strftime(format)