Você está na página 1de 2

Python Cheat Sheet

by Dave Child (DaveChild) via cheatography.com/1/cs/19/

Python sys Variables

Python os Variables (cont)

Python String Methods

argv

Command line args

name

Name of OS

capitalize() *

lstrip()

builtin_module_names

Linked C modules

pardir

Parent dir string

center(width)

partition(sep)

byteorder

Native byte order

pathsep

Patch separator

count(sub, start, end)

replace(old, new)

check_interval

Signal check

sep

Path separator

decode()

rfind(sub, start ,end)

Registered OS names: "posix", "nt",

encode()

rindex(sub, start, end)

"mac", "os2", "ce", "java", "riscos"

endswith(sub)

rjust(width)

expandtabs()

rpartition(sep)

find(sub, start, end)

rsplit(sep)

index(sub, start, end)

rstrip()

isalnum() *

split(sep)

isalpha() *

splitlines()

isdigit() *

startswith(sub)

islower() *

strip()

isspace() *

swapcase() *

istitle() *

title() *

isupper() *

translate(table)

join()

upper() *

ljust(width)

zfill(width)

frequency
exec_prefix

Root directory

executable

Name of executable

exitfunc

Exit function name

Python Class Special Methods

modules

Loaded modules

__new__(cls)

__lt__(self, other)

path

Search path

__init__(self, args)

__le__(self, other)

platform

Current platform

__del__(self)

__gt__(self, other)

stdin, stdout, stderr

File objects for I/O

__repr__(self)

__ge__(self, other)

version_info

Python version info

__str__(self)

__eq__(self, other)

winver

Version number

__cmp__(self, other)

__ne__(self, other)

__index__(self)

__nonzero__(self)

Python sys.argv

__hash__(self)

sys.argv[0]

foo.py

__getattr__(self, name)

sys.argv[1]

bar

__getattribute__(self, name)

sys.argv[2]

-c

__setattr__(self, name, attr)

sys.argv[3]

qux

__delattr__(self, name)

sys.argv[4]

--h

__call__(self, args, kwargs)

bit strings.

Python List Methods

Python File Methods

lower() *
Methods marked * are locale dependant for 8-

sys.argv for the command:


$ python foo.py bar -c qux --h
Python os Variables
altsep

Alternative sep

curdir

Current dir string

defpath

Default search path

devnull

Path of null device

extsep

Extension separator

linesep

Line separator

append(item)

pop(position)

close()

readlines(size)

count(item)

remove(item)

flush()

seek(offset)

extend(list)

reverse()

fileno()

tell()

index(item)

sort()

isatty()

truncate(size)

insert(position, item)

next()

write(string)

By Dave Child (DaveChild)

Published 19th October, 2011.

Sponsored by Readability-Score.com

cheatography.com/davechild/

Last updated 24th November, 2014.

Measure your website readability!

www.addedbytes.com

Page 1 of 2.

https://readability-score.com

Python Cheat Sheet


by Dave Child (DaveChild) via cheatography.com/1/cs/19/

Python File Methods (cont)

Python Date Formatting

read(size)

%a

Abbreviated weekday (Sun)

%A

Weekday (Sunday)

%b

Abbreviated month name (Jan)

writelines(list)

readline(size)
Python Indexes and Slices

%B

Month name (January)

len(a)

%c

Date and time

a[0]

%d

Day (leading zeros) (01 to 31)

a[5]

%H

24 hour (leading zeros) (00 to 23)

a[-1]

%I

12 hour (leading zeros) (01 to 12)

a[-2]

%j

Day of year (001 to 366)

a[1:]

[1,2,3,4,5]

%m

Month (01 to 12)

a[:5]

[0,1,2,3,4]

%M

Minute (00 to 59)

a[:-2]

[0,1,2,3]

%p

AM or PM

a[1:3]

[1,2]

%S

Second (00 to 61)

a[1:-1]

[1,2,3,4]

%U

Week number (00 to 53)

b=a[:]

Shallow copy of a

%w

Weekday (0 to 6)

%W

Week number (00 to 53)

%x

Date

%X

Time

Indexes and Slices of a=[0,1,2,3,4,5]


Python Datetime Methods
today()

fromordinal(ordinal)

%y

Year without century (00 to 99)

now(timezoneinfo)

combine(date, time)

%Y

Year (2008)

utcnow()

strptime(date, format)

%Z

Time zone (GMT)

%%

A literal "%" character (%)

fromtimestamp(timestamp)
utcfromtimestamp(timestamp)

Sunday as start of week. All days in a new


year preceding the first Sunday are considered

Python Time Methods

to be in week 0.

replace()

utcoffset()

isoformat()

dst()

__str__()

tzname()

strftime(format)

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.

By Dave Child (DaveChild)

Published 19th October, 2011.

Sponsored by Readability-Score.com

cheatography.com/davechild/

Last updated 24th November, 2014.

Measure your website readability!

www.addedbytes.com

Page 2 of 2.

https://readability-score.com

Você também pode gostar