Show Menu
Cheatography

python methods Cheat Sheet (DRAFT) by

Python methods for basic data structures

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Lists

append()
Adds an element at the end of the list
clear()
Removes all the elements from the list
copy()
Returns a copy of the list
count()
Returns the number of elements with the specified value
extend()
Add the elements of a list (or any iterable), to the end of the current list
index()
Returns the index of the first element with the specified value
insert()
Adds an element at the specified position
pop()
Removes the element at the specified position
remove()
Removes the first item with the specified value
reverse()
Reverses the order of the list
sort()
Sorts the list

String

capita­lize()
Converts the first character to upper case
casefold()
Converts string into lower case
center()
Returns a centered string
count()
Returns the number of times a specified value occurs in a string
encode()
Returns an encoded version of the string
endswith()
Returns true if the string ends with the specified value
expand­tabs()
Sets the tab size of the string
find()
Searches the string for a specified value and returns the position of where it was found
format()
Formats specified values in a string
format­_map()
Formats specified values in a string
index()
Searches the string for a specified value and returns the position of where it was found
isalnum()
Returns True if all characters in the string are alphan­umeric
isalpha()
Returns True if all characters in the string are in the alphabet
isascii()
Returns True if all characters in the string are ascii characters
isdeci­mal()
Returns True if all characters in the string are decimals
isdigit()
Returns True if all characters in the string are digits
isiden­tif­ier()
Returns True if the string is an identifier
islower()
Returns True if all characters in the string are lower case
isnume­ric()
Returns True if all characters in the string are numeric
 

Dictio­naries

clear()
Removes all the elements from the dictionary
copy()
Returns a copy of the dictionary
fromkeys()
Returns a dictionary with the specified keys and value
get()
Returns the value of the specified key
items()
Returns a list containing a tuple for each key value pair
keys()
Returns a list containing the dictio­nary's keys
pop()
Removes the element with the specified key
popitem()
Removes the last inserted key-value pair
setdef­ault()
Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
update()
Updates the dictionary with the specified key-value pairs
values()
Returns a list of all the values in the dictionary

String

isprin­table()
Returns True if all characters in the string are printable
isspace()
Returns True if all characters in the string are whites­paces
istitle()
Returns True if the string follows the rules of a title
isupper()
Returns True if all characters in the string are upper case
join()
Converts the elements of an iterable into a string
ljust()
Returns a left justified version of the string
lower()
Converts a string into lower case
lstrip()
Returns a left trim version of the string
maketr­ans()
Returns a transl­ation table to be used in transl­ations
partit­ion()
Returns a tuple where the string is parted into three parts
replace()
Returns a string where a specified value is replaced with a specified value
rfind()
Searches the string for a specified value and returns the last position of where it was found
rindex()
Searches the string for a specified value and returns the last position of where it was found
rjust()
Returns a right justified version of the string
rsplit()
Splits the string at the specified separator, and returns a list
rstrip()
Returns a right trim version of the string
split()
Splits the string at the specified separator, and returns a list
splitl­ines()
Splits the string at line breaks and returns a list
starts­with()
Returns true if the string starts with the specified value
strip()
Returns a trimmed version of the string
swapcase()
Swaps cases, lower case becomes upper case and vice versa
title()
Converts the first character of each word to upper case
transl­ate()
Returns a translated string
upper()
Converts a string into upper case
zfill()
Fills the string with a specified number of 0 values at the beginning
 

Tuple

count()
Returns the number of times a specified value occurs in a tuple
index()
Searches the tuple for a specified value and returns the position of where it was found

Set

add()
Adds an element to the set
clear()
Removes all the elements from the set
copy()
Returns a copy of the set
differ­ence()
Returns a set containing the difference between two or more sets
differ­enc­e_u­pdate()
Removes the items in this set that are also included in another, specified set
discard()
Remove the specified item
inters­ect­ion()
Returns a set, that is the inters­ection of two or more sets
inters­ect­ion­_up­date()
Removes the items in this set that are not present in other, specified set(s)
isdisj­oint()
Returns whether two sets have a inters­ection or not
issubset()
Returns whether another set contains this set or not
issupe­rset()
Returns whether this set contains another set or not
pop()
Removes an element from the set
remove()
Removes the specified element
symmet­ric­_di­ffe­rence()
Returns a set with the symmetric differ­ences of two sets
symmet­ric­_di­ffe­ren­ce_­upd­ate()
inserts the symmetric differ­ences from this set and another
union()
Return a set containing the union of sets
update()
Update the set with another set, or any other iterable

File

close()
Closes the file
detach()
Returns the separated raw stream from the buffer
fileno()
Returns a number that represents the stream, from the operating system's perspe­ctive
flush()
Flushes the internal buffer
isatty()
Returns whether the file stream is intera­ctive or not
read()
Returns the file content
readable()
Returns whether the file stream can be read or not
readline()
Returns one line from the file
readli­nes()
Returns a list of lines from the file
seek()
Change the file position
seekable()
Returns whether the file allows us to change the file position
tell()
Returns the current file position
truncate()
Resizes the file to a specified size
writable()
Returns whether the file can be written to or not
write()
Writes the specified string to the file
writel­ines()
Writes a list of strings to the file