end, file)
This function is present in
Python 2 and from Python
2.6 is the same as the
function in Python 3 by using
from __future__ import
print_function. It replaces the
print statement which is in
Python 2 but not in Python 3.
Prints a textual representation
of one or more Python
objects to the screen or file.
x = 'Some data'
print(x)
print(x, file=fileObj)
#Python 3
print(1,2,3)
Result is 1 2 3.
print(1, 2, 3, sep=';',
end='*\n')
Result is 1;2;3*.
property(getFunc,
setFunc, delFunc,
docStr)
Allows attribute-style dot
notation access
(classObj.attrName) for
getter and setter functions in
‘new-style’ class definitions
(which inherit from object).
class DemoObj(object):
def __init__(self):
self._x = None
def getX(self):
return self._x
def setX(self,
value):
This means that the code
looks cleaner and allows
extra functionality, for
example, validation in the
setter function. Optionally
there is a third argument
which is a function for
attribute deletion, and a
fourth argument, which is a
document string.
self._x = value
x = property(getX,
setX)
d = DemoObj()
d.x = 55 # same as
d.setX(55)
print(d.x) # same as
d.getX()
Result is 55.
range(end)
or
range(start, end,
step)
Gives a range of integers
from a start up to (but not
including) an end value with
a regular increment. By
default the range starts at 0
and increments by 1. In
Python 2 range() creates a
list, but in Python 3 becomes
an iterable object, more like
xrange(), and xrange() itself
disappears.
range(7) # [0,1,2,3,4,5,6]
range(3,8) # [3, 4, 5, 6, 7]
range(3,10,2) # [3, 5, 7, 9]
range(5,0,-1) # [5, 4, 3, 2,
1]
In Python 3 the result is not
a list but instead a range
iterable.
raw_input(prompt)
Prompts the user at the
command line for keyboard
input, i.e. so the entered
values can be used in the
program. Not available in
Python 3, use input() instead.
x = raw_input(‘Enter
value:’)
print(“Value is: “, x)
reload(module)
Reloads a Python module,
assuming it was previously
imported. Not available in
Python 3.
import math
math.pi = 3.0
print(math.pi) # 3.0
reload(math)
print(math.pi) #
3.14159265359
repr(obj)
Creates a formal, textual
representation of a Python
object. Similar to str(), but
gives unambiguous (in terms
of identifying the original
object), albeit sometimes less
readable, text.
from numpy import array
a = array([1,2,3])
str(a) # '[1 2 3]'
repr(a) # 'array([1, 2,
3])'
reversed(seq)
Creates an iterator object that
for x in
provides items in the reverse
order, based on an ordered
collection.
reversed(range(7)):
print(x)
round(val, places)
Rounds val to the nearest
whole number or optionally
accepts places, stating how
many decimal places to round
to.
[See Mathematics section
above]
set(vals)
Creates a set from a given
object; an unordered
collection of non-repeating
items.
l = [0,1,2,3,1,2,3,2,1,0,1,3]
x = set(l)
print(x)
Result is set([0, 1, 2, 3]).
setattr(obj, name,
val)
Sets a named attribute of a
specified object with a given
value. This is often an
alternative to the object.attr =
value notation where the
name of the attribute can
vary.
p1 = Molecule()
setattr(p1, ‘name’, ‘c-Myc’)
p1.name = ‘c-Myc’ # Same
sorted(vals,
comparator, key,
reverse)
Creates a new list with items
in sorted order from the items
of a collection or other
iterable object.
list1 = [‘G’,‘C’,‘A’,‘T’]
list2 = sorted(list1)
print(list2)
Result is [‘A’,‘C’,‘G’,‘T’].
str(obj)
Converts a Python object into
an informal textual string
representation. Numeric
values may be rounded for
display.
str(7.500000000001) #
'7.5'
str([5,7,11]) # '[5, 7,
11']
str(type(1)) # "
'int'>"
sum(vals, start)
Adds all the items of a
collection or other iterable
object. For NumPy arrays it
sums along the first axis.
values =
[7,69,31,99,53,16,72]
print(sum(values)) #
347
super(objType,
obj)
When a function is
overridden in a subclass this
allows calling of a superclass
function without mentioning
class C(B):
def f(self, arg):
super(C,
self).f(arg)
the superclass by name. An
optional object (or class) may
be passed in as a second
argument.
# same as doing:
# B.f(self, arg)
tuple(vals)
Creates a Python tuple
collection from a specified
object, which must be
iterable. Can be used on a
tuple to make a copy.
Sometimes used to convert a
list, so that it may be used as
a dictionary key.
l = [‘D’,‘E’,‘R’,‘K’,‘H’]
t = tuple(enumerate(l))
Result is ((0,‘D’), (1,‘E’),
(2,‘R’), (3,‘K’), (4,‘H’)).
type(obj)
Gives an object representing
the type of a specified object.
type(1) #
type(‘a’) #
type(type(1)) #
‘type’>
In Python 3 the result is
etc.
unichr(code)
Gives the Unicode character
string for an integer code
number. Performs the inverse
operation to ord() for
Unicode strings. Not
available in Python 3 since
all strings are Unicode.
alpha = u’\u03b1’
i = ord(alpha)
print(unichr(i+1))
Result is u’\u03b2’; ‘β’.
unicode(obj,
encoding)
Converts a Python object into
a Unicode string
representation in a manner
similar to str(). The optional
second argument allows the
encoding type to be specified,
e.g. when converting plain
text. Not available in Python
3, since all strings are
Unicode.
text1 = unicode(3.141)
x = ‘\xce\xb1-helix’
text2 = unicode(x, ‘utf-8’)
Last result is u’\u03b1-
helix’; ‘α-helix’.
xrange(end)
or
xrange(start, end,
step)
Used for looping through
large ranges of numbers.
Compared to range(),
xrange() doesn’t create a
whole list and thus saves
n = 1000000
y = [x**0.5 for x in
xrange(n)]
memory; instead it creates an
iterable object. Not available
in Python 3 since range()
behaves like xrange().
zip(vals1, vals,
…)
Takes items in sequence from
a number of collections (or
other iterable objects) to
make a list of tuples, where
each tuple contains one item
from each collection. Often
used to group items from lists
which have the same index.
Note the inverse operation is
achieved with zip(*c). In
Python 3 the result is a zip
iterator rather than a list.
a = [1,2,3]
b = [‘x’,‘y’,‘z’]
c = list(zip(a,b))
d = list(zip(*c))
Note: list() conversion not
necessary in Python 2.
Result:
c is [(1,‘x’), (2,‘y’), (3,‘z’)]
d is [(1,2,3), (‘x’,‘y’,‘z’)]
Do'stlaringiz bilan baham: |