Python Functions List

compile

The compile(str,filename,kind) function compiles a string into bytecode.

c = compile(s,'x.txt','exec')
exec(c)

dir

dir function lists the methods available on an object

enumerate

creates an iterator that simply returns a sequence of tuples (0, s[0]), (1, s[1]), (2, s[2]), and so on. see loops.

eval

Executes an expression string and returns the result: a = eval('4 + 5 * (3+x) + 7')

exec

executes a string containing arbitrary Python code.The code supplied to exec() is executed as if the code actually appeared in place of the exec operation:
a = [3, 5, 10, 13]
exec("for i in a: print(i)")

File

  • f.seek(0,2): go to EOF

help

give information about the modules/functions. similar to using _ _ doc _ _ .

id

returns the identity of an object as an integer.This integer usually corresponds to the object’s location in memory

isinstance

to check type of an object and is aware of inheritance

class A(object): pass
class B(A): pass
class C(object): pass
 
a = A() 
b = B() 
c = C() 
 
type(a) # Returns the class object A
isinstance(a,A) # Returns True
isinstance(b,A) # Returns True
isinstance(b,C) # Returns False
issubclass(B,A) # Returns True
issubclass(C,A) # Returns False

join

str.join(iterable)

Return a string which is the concatenation of the strings in the iterable with a separator between elements which is the string providing this method. For example ', '.joing(["a","b"]) is a, b.

Concats a list using a

partial

Evaluates some of the arguments to a function and returns an object that you can call to supply the remaining arguments at a later point. Partial evaluation of function arguments is closely related to a process known as currying.

def foo(x,y,z):
    return x + y + z
 
from functools import partial
f = partial(foo,1,2) # Supply values to x and y arguments of foo
f(3) # Calls foo(1,2,3), result is 6

range

range(i,j [,stride]) function creates an object that represents a range of integers with values i to j-1. range(5), range(8,1,-1). This was xrange previously.

sys.getrefcount

returns the reference count of an object

string

  • ord(char): returns the ascii value of char and chr() does the other way round
from string import maketrans 
s = maketrans("abcdefghijklmnopqrstuvwxyz","cdefghijklmnopqrstuvwxyzab")
 
str = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj.";
 
print str.translate(s)

type

returns type of an object (class, function, int, etc )

Time

  • time.sleep(x) : suspend execution

zip(s,t)

combines sequences s and t into a sequence of tuples (s[0],t[0]), (s[1],t[1]), … stopping with the shortest of the sequences s and t should they be of unequal length. see loops.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License