Python Functions and Methods Reference
Built-in Functions
print():
Prints the given object to the console.
len():
Returns the number of items in an object.
type():
Returns the type of the object.
range():
Returns a sequence of numbers.
input():
Reads a line from input.
int():
Converts a value to an integer.
str():
Converts a value to a string.
float():
Converts a value to a float.
list():
Creates a list.
dict():
Creates a dictionary.
List Methods
append(x):
Adds an item x to the end of the list.
extend(iterable):
Extends list by appending elements from the iterable.
insert(i, x):
Inserts item x at position i.
remove(x):
Removes the first occurrence of x.
pop([i]):
Removes and returns item at index i (last item if i is not given).
clear():
Removes all items from the list.
index(x):
Returns the index of the first occurrence of x.
count(x):
Returns the count of x in the list.
sort():
Sorts the list in ascending order.
reverse():
Reverses the elements of the list in place.
Tuple Methods
count(x):
Returns the number of times x appears in the tuple.
index(x):
Returns the index of the first occurrence of x.
String Methods
lower():
Returns a lowercase version of the string.
upper():
Returns an uppercase version of the string.
find(x):
Returns the lowest index of x.
replace(old, new):
Replaces occurrences of old with new.
split(sep):
Splits the string at sep and returns a list.
strip():
Removes whitespace from both ends.
startswith(x):
Returns True if the string starts with x.
endswith(x):
Returns True if the string ends with x.
join(iterable):
Joins elements of iterable into a string.
Set Methods
add(x):
Adds element x to the set.
update(iterable):
Updates the set with elements from iterable.
remove(x):
Removes x from the set (raises error if not present).
discard(x):
Removes x if present.
pop():
Removes and returns an arbitrary element.
clear():
Removes all elements from the set.
union(set):
Returns a new set with elements from both sets.
intersection(set):
Returns a new set with elements common to both sets.
difference(set):
Returns elements in the set but not in the other.
Dictionary Methods
get(key):
Returns value for key if present.
keys():
Returns a view of dictionary's keys.
values():
Returns a view of dictionary's values.
items():
Returns a view of dictionary's key-value pairs.
update(other):
Updates the dictionary with key-value pairs from other.
pop(key):
Removes the specified key and returns the corresponding value.
clear():
Removes all items from the dictionary.