To Documents
Builtin Objects and Methods
- A method is a subroutine (also called a function or procedure) that can be called to perform a computation
or other action.
- Standalone Functions
- abs(n) Return the absolute value of the number
n.
- bool(v) Convert the values
v to boolean (bool object): 0,
0.0, "", and None to
False; everything else to True.
- chr(n) Return the
character that has the ASCII code n.
- float(v) Convert a value
v to a floating point number (float
object).
- input(s) Return a line typed in from the
keyboard. s is the prompt telling the user what to enter.
- int(v) Convert a value v,
which is a string or numeric value, to an integer (int object).
- len(obj) Return the length of a string or list.
- list(obj) Convert obj into a list. For example
list("abc") returns ['a', 'b', 'c'].
Also, list(range(1, 4)) returns [1, 2,
3].
- max(v1, v2, v2) Return the maximum value of
v1, v2, v3.
The number of arguments is arbitrary.
- max(lst) Return the maximum value of the list lst.
- min(v1, v2, v3) Return the minimum
value of
v1, v2, v3.
The number of arguments is arbitrary.
- min(lst) Return the minimum value of the
list lst.
- open(filename, mode) Return a file object that
accesses the file with specified filename and
mode.
Mode can be one of the strings "r": read, "w": write,
"rb": read binary, "wb": write
binary, "a": append.
- ord(c) Return the ASCII
code of the c, which is a string (str
object) of length 1.
- range(end) Return a range
object with values [0, 1, ... , end - 2, end - 1]
- range(start, end) Return a
range object with values [start, start
+ 1, ... , end - 2, end - 1]
- range(start, end, step) Return a
range object with values [start, start + step, start + 2 *
step, ... ]. The largest value of the returned range is less than
end.
- repr(obj) Return a technical string
representation of the object obj, for use by developers.
- round(x, n) Return the floating point
value x rounded to n digits after the decimal point. If
n is omitted, it defaults to zero
- str(obj) Return a user friendly
string (str object) representation of the object obj,
in a form suitable for end users.
- type(obj) Return the datatype or class
of the object obj.
- String Methods s is an
str variable whose value is a character string.
[ ] means optional parameter.
- s.capitalize( ) Return the string
s
with the first character capitalized if it is a letter.
- s.center(width[, fillchar='\n']) Center
s in a string of width. Use
fillchar to fill in the regions before and after s.
- s.count(sub) Return the number of
non-overlapping occurrences of the substring sub in
s.
- s.endswith(suffix) Return True
if s ends with suffix,
otherwise return False.
- s.find(sub) Return
the index of the first occurrence of
sub within s. If sub is
not found in s, return -1.
- s.format(args) Return the data in
args formatted according to the format string
s.
- s.join(lst) Join the list of strings
lst using the seperator string s
to form a single string
- s.lower( ) Return
s with all letters, if any,
converted to lower case all non-letters are unchanged.
- s.ljust(width[, fillchar='\n']) Return
s left justified in a string of length
width. Fill with
fillchar characters to the right.
- s.lstrip(char="\s\n\t") Return a
copy of the string with leading characters removed. chars
is a string specifying the set of characters to be removed.
- s.rjust(width[, fillchar='\n']) Return
s right justified in a string of length
width. Fill with
fillchar characters to the left.
- s.rfind(sub) Return
the index of the last occurrence of
sub within s. If sub is
not found in s, return -1.
- s.rstrip(char="\s\n\t") Return a
copy of the string with trailing characters removed. chars
is a string specifying the set of characters to be removed.
- s.replace(old, new[, count]) Return
a copy of s with
all occurrences of substring old replaced by
new. If the optional argument count
is given, only the first count occurrences are replaced.
- s.strip(chars="\s\n\t") Return a
copy of the string with leading and trailing characters removed. chars
is a string specifying the set of characters to be removed.
- s.split(sep) Return a
list of the fields in a delimited string, where sep indicates the delimiter. A
common delimiter is a comma, for example fields = s.split(',').
- s.startswith(suffix) Return True
if s begins with prefix,
otherwise return False.
- s.swapcase( ) Return a copy of the string with uppercase
characters converted to lowercase and vice versa.
- s.upper( ) Return
s with all letters, if any,
converted to upper case.
- List Methods a is a
list variable whose value is a list, also called an
array. [ ] means optional parameter.
- a.append( ) Append an item to the end of the list.
- a.clear( ) Remove all items from the list.
The resulting list looks like this: [ ].
- a.count(x) Return the number of times
x appears in the list. If x
is not found in the list, an error is raised.
- a.insert(i, x) Insert an item
x at position i. The first argument
i is the index of the element before which to insert, so
a.insert(0, x) inserts at the front of the list, and
a.insert(len(a), x) is equivalent to
a.append(x).
- len(a) Return the number of items in the list.
- a.pop([x]) Remove the item at the given position in the list, and return it. If no index is specified,
a.pop( ) removes and returns the last item in the list.
- a.remove(x) Remove the first item from the list whose value is
x. An error is raised if there is no such item.
- a.reverse( ) Reverse the items of the list in place.
- Dictionary Methods
d is a
dict variable whose value is a dictionary object. [ ] means optional parameter.
- d[key] Return the value in
d with key key. If key is not in
d, raise an error.
- d[key] = value Set d[key] to value.
- key in d Return True if
d has a key key, else return
False.
- d.clear( ) Remove all items from the dictionary.
- d.get(key) Return the value for
key if key is in
d. If key is not in
d, raise an error. This is the same as
d[key].
- a.items( ) Return a new view of the items ((key,value) pairs) in
d. This view is dynamic because it changes if the
dictionary changes. Use the list method to change the view into a list, which is
not dynamically tied to d.
- a.keys( ) Return a new view of the keys in
d. This view is dynamic because it changes if the
dictionary changes. Use the list method to change the view into a list, which is
not dynamically tied to d.
- d.pop(key) If key is in the dictionary, remove it and return its value. If
key is not in d, an error is
raised.
- a.values( ) Return a new view of the values in
d. This view is dynamic because it changes if the
dictionary changes. Use the list method to change the view into a list, which is
not dynamically tied to d.