0% found this document useful (0 votes)
92 views

Python Cheat Sheet: by Via

This document provides a cheat sheet summarizing key aspects of the Python programming language, including commonly used Python variables, methods for lists, strings, files, dates, and more. It also includes formatting codes for working with dates in Python.

Uploaded by

David Bumphis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Python Cheat Sheet: by Via

This document provides a cheat sheet summarizing key aspects of the Python programming language, including commonly used Python variables, methods for lists, strings, files, dates, and more. It also includes formatting codes for working with dates in Python.

Uploaded by

David Bumphis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python Cheat Sheet

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

Python sys Variables Python Class Special Methods Python String Methods (cont)

argv Command line args __new__(cls) __lt__(self, other) istitle() * title() *

builtin_module_names Linked C modules __init__(self, args) __le__(self, other) isupper() * translate(table)

byteorder Native byte order __del__(self) __gt__(self, other) join() upper() *

check_interval Signal check __repr__(self) __ge__(self, other) ljust(width) zfill(width)


frequency
__str__(self) __eq__(self, other) lower() *
exec_prefix Root directory
__cmp__(self, other) __ne__(self, other) Methods marked * are locale dependant for 8-
executable Name of executable __index__(self) __nonzero__(self) bit strings.
exitfunc Exit function name
__hash__(self)
Python File Methods
modules Loaded modules
__getattr__(self, name)
path Search path close() readlines(size)
__getattribute__(self, name)
platform Current platform flush() seek(offset)
__setattr__(self, name, attr)
stdin, stdout, stderr File objects for I/O fileno() tell()
__delattr__(self, name)
version_info Python version info isatty() truncate(size)
__call__(self, args, kwargs)
winver Version number next() write(string)
Python List Methods read(size) writelines(list)
Python sys.argv
append(item) pop(position) readline(size)
sys.argv[0] foo.py
count(item) remove(item)
Python Indexes and Slices
sys.argv[1] bar extend(list) reverse()
sys.argv[2] -c len(a) 6
index(item) sort()
sys.argv[3] qux a[0] 0
insert(position, item)
sys.argv[4] --h a[5] 5

sys.argv for the command: Python String Methods a[-1] 5

$ python foo.py bar -c qux --h capitalize() * lstrip() a[-2] 4

center(width) partition(sep) a[1:] [1,2,3,4,5]


Python os Variables
count(sub, start, end) replace(old, new) a[:5] [0,1,2,3,4]
altsep Alternative sep a[:-2] [0,1,2,3]
decode() rfind(sub, start ,end)
curdir Current dir string
encode() rindex(sub, start, end) a[1:3] [1,2]
defpath Default search path a[1:-1] [1,2,3,4]
endswith(sub) rjust(width)
devnull Path of null device
expandtabs() rpartition(sep) b=a[:] Shallow copy of a
extsep Extension separator
find(sub, start, end) rsplit(sep) Indexes and Slices of a=[0,1,2,3,4,5]
linesep Line separator
index(sub, start, end) rstrip()
name Name of OS Python Datetime Methods
isalnum() * split(sep)
pardir Parent dir string today() fromordinal(ordinal)
isalpha() * splitlines()
pathsep Patch separator now(timezoneinfo) combine(date, time)
isdigit() * startswith(sub)
sep Path separator utcnow() strptime(date, format)
islower() * strip()
Registered OS names: "posix", "nt", fromtimestamp(timestamp)
isspace() * swapcase() *
"mac", "os2", "ce", "java", "riscos"
utcfromtimestamp(timestamp)

By Dave Child (DaveChild) Published 19th October, 2011. Sponsored by ApolloPad.com


cheatography.com/davechild/ Last updated 12th May, 2016. Everyone has a novel in them. Finish Yours!
www.getpostcookie.com Page 1 of 2. https://apollopad.com
Python Cheat Sheet
by Dave Child (DaveChild) via cheatography.com/1/cs/19/

Python Time Methods

replace() utcoffset()

isoformat() dst()

__str__() tzname()

strftime(format)

Python Date Formatting

%a Abbreviated weekday (Sun)

%A Weekday (Sunday)

%b Abbreviated 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.

By Dave Child (DaveChild) Published 19th October, 2011. Sponsored by ApolloPad.com


cheatography.com/davechild/ Last updated 12th May, 2016. Everyone has a novel in them. Finish Yours!
www.getpostcookie.com Page 2 of 2. https://apollopad.com

You might also like