Show Menu
Cheatography

Essential Shortcuts in Python Cheat Sheet by

This cheat sheet has been created to help you understand Python's high-level and general-purpose programming language.

Python sys Variables

argv
Command line args
builti­­n_­m­o­du­­le_­­names
Linked C modules
byteorder
Native byte order
check_­­in­t­erval
Signal check frequency
exec_p­­refix
Root directory
executable
Name of executable
exitfunc
Exit function name
modules
Loaded modules
path
Search path
platform
Current platform
stdin, stdout, stderr
File objects for I/O
versio­­n_info
Python version info
winver
Version number

Python List Methods

append­­(item)
pop(po­­si­tion)
count(­­item)
remove­­(item)
extend­­(list)
reverse()
index(­­item)
sort()
insert­­(p­o­s­ition, item)

Python Time Methods

replace()
utcoff­­set()
isofor­­mat()
dst()
__str__()
tzname()
strfti­­me­(­f­ormat)

Python Class Special Methods

__new_­­_(cls)
__lt__­­(self, other)
__init­­__­(­self, args)
__le__­­(self, other)
__del_­­_(­self)
__gt__­­(self, other)
__repr­­__­(­self)
__ge__­­(self, other)
__str_­­_(­self)
__eq__­­(self, other)
__cmp_­­_(­self, other)
__ne__­­(self, other)
__inde­­x_­_­(­self)
__nonz­­er­o­_­_(­­self)
__hash­­__­(­self)
__geta­­tt­r­_­_(­­self, name)
__geta­­tt­r­i­bu­­te_­­_(­self, name)
__seta­­tt­r­_­_(­­self, name, attr)
__dela­­tt­r­_­_(­­self, name)
__call­­__­(­self, args, kwargs)

Python Indexes and Slices

len(a)
6
a[0]
0
a[5]
5
a[-1]
5
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]
a[1:-1]
[1,2,3,4]
b=a[:]
Shallow copy of a
Indexes and Slices of a=[0,1­­,2­,­3­,4,5]

Arit­hmetic Operat­ors

x + y
add
x - y
subtract
x * y
multiply
x / y
divide
x % y
modulus
x ** y
xy
Assignment shortcuts: x op= y
Example: x += 1 increments x
 

Python sys.argv

sys.ar­­gv[0]
foo.py
sys.ar­­gv[1]
bar
sys.ar­­gv[2]
-c
sys.ar­­gv[3]
qux
sys.ar­­gv[4]
--h
sys.argv for the comman­d:  $ python foo.py bar -c qux --h

Python File Methods

close()
readli­­ne­s­(­size)
flush()
seek(o­­ffset)
fileno()
tell()
isatty()
trunca­­te­(­size)
next()
write(­­st­ring)
read(size)
writel­­in­e­s­(list)
readli­­ne­(­size)

Python String Methods

capita­­lize() *
lstrip()
center­­(w­idth)
partit­­io­n­(sep)
count(sub, start, end)
replac­­e(old, new)
decode()
rfind(sub, start ,end)
encode()
rindex­­(sub, start, end)
endswi­­th­(sub)
rjust(­­width)
expand­­tabs()
rparti­­ti­o­n­(sep)
find(sub, start, end)
rsplit­­(sep)
index(sub, start, end)
rstrip()
isalnum() *
split(sep)
isalpha() *
splitl­­ines()
isdigit() *
starts­­wi­t­h­(sub)
islower() *
strip()
isspace() *
swapcase() *
istitle() *
title() *
isupper() *
transl­­at­e­(­table)
join()
upper() *
ljust(­­width)
zfill(­­width)
lower(­)*  Methods marked are locale dependant for 8-bit strings.

Data Types

Integer
-256, 15
Float
-253.23, 1.253e-10
String
"­­He­l­l­o", 'Goodbye', "­­"­"­­Mul­­ti­l­i­ne­­"­­"­"
Boolean
True, False
List
[ value, ... ]
Tuple
( value, ... )1
Dictionary
{ key: value, ... }
Set
{ value, value, ... }2
1 Parent­­heses usually optional
2 Create an empty set with set()

Conv­ersion Functi­ons

int(e­xpr)
Converts expr to integer
float(­­expr)
Converts expr to float
str(e­xpr)
Converts expr to string
chr(num)
ASCII char num

Conv­ersion Functi­ons

int(e­xpr)
Converts expr to integer

Conv­ersion Functi­ons

int(e­xpr)
Converts expr to integer

Conv­ersion Functi­ons

int(e­xpr)
Converts expr to integer
 

Python Datetime Methods

today()
fromor­­di­n­a­l(­­ord­­inal)
now(ti­­me­z­o­ne­­info)
combin­­e(­date, time)
utcnow()
strpti­­me­(­date, format)
fromti­­me­s­t­am­­p(t­­im­e­s­tamp)
utcfro­­mt­i­m­es­­tam­­p(­t­i­me­­stamp)

Python os Variab­les

altsep
Altern­­ative sep
curdir
Current dir string
defpath
Default search path
devnull
Path of null device
extsep
Extension separator
linesep
Line separator
name
Name of OS
pardir
Parent dir string
pathsep
Patch separator
sep
Path separator
Registered OS names: "­­po­s­i­x", "­­nt­", "­­ma­c­", "­­os­2­", "­­ce­", "­­ja­v­a­", "­­ri­s­c­os­­"

Python Date Format­ting

%a
Abbrev­­iated weekday (Sun)
¹ Sunday as start of week. All days in a new year preceding the first Sunday are considered to be in week 0.
² 0 is Sunday, 6 is Saturday.
³ Monday as start of week. All days in a new year preceding the first Monday are considered to be in week 0.
⁴ This is not a mistake. Range takes account of leap and double­­-leap seconds.

Python Date Format­ting

%a
Abbrev­­iated weekday (Sun)
%A
Weekday (Sunday)
%b
Abbrev­­iated month name (Jan)
%B
Month name (January)
%c
Date and time
%d
Day (leading zeros) (01 to 31)
%H
24 hour (leading zeros) (00 to 23)
%I
12 hour (leading zeros) (01 to 12)
%j
Day of year (001 to 366)
%m
Month (01 to 12)
%M
Minute (00 to 59)
%p
AM or PM
%S
Second (00 to 61⁴)
%U
Week number¹ (00 to 53)
%w
Weekday² (0 to 6)
%W
Week number³ (00 to 53)
%x
Date
%X
Time
%y
Year without century (00 to 99)
%Y
Year (2008)
%Z
Time zone (GMT)
%%
A literal "­­%" character (%)
¹ Sunday as start of week. All days in a new year preceding the first Sunday are considered to be in week 0.
² 0 is Sunday, 6 is Saturday.
³ Monday as start of week. All days in a new year preceding the first Monday are considered to be in week 0.
⁴ This is not a mistake. Range takes account of leap and double­­-leap seconds.

Arit­hmetic Operat­ors

x + y
add
x - y
subtract
Assignment shortcuts: x op= y
Example: x += 1 increments x
                           
 

Comments

Awesome resource - thanks Christoph and Zenscrape.com!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

            Python 3 Cheat Sheet by Finxter
          OneStop(Nerd)Shop Cheat Sheet