Sonntag, 11. Oktober 2009

Python stuff

functions numbers

int(), long8), float(), round(), hex(), oct(), pow(), cmp()

s1+s2 merge
s1*n s1+s1+s1+s1+...

functions strings:
org(), chr(), str(), repr(), unicode(), len()

s.count(), s.encode(), s.endswith(), s.find(), s.isalnum(), s.iksalpha(), s.isdigi(), s.islower(), s.isspace(), s.join(), s.lstrip(), s.replace(), s.rfind(), s.rstrip(), s.split(), s.startswith(), s.strip(), s.swapcase()


List:
array=[1,2,3,4]
len(array)
array[1]="blub"
array[2]=["bla", [1,2,3], 'ups']

append(), extend(), count(), index(), insert(), remove(), pop(), reverse(), sort()


range()

range(4)
[0,1,2,3]
range(5,10,2]
[5,7,9]

Tuples
same as list but imutable

t1=(1,2,3)


indicies and slices


s[4:]
s[4:7]
s[:-7]
s[-4:]
s[:]
max(), min(), len()

a,b,c = [ 1, 2, 3 ]

del delete a varaible

a= 1
del a


assoziative lists, hash

h = { k:v, k2:v2, k3:v3 }
t={"peter":23450, "anna":39454, "blub":439549}
t["blub"]
items(), keys(), values(), has_key(), update()
t.has_key("anna")
len(), del()


None, type(), in-operator

if 2 in lists:
...
if not "a" in string:
...


Functions:

def p(n):
"bla bla, describe function for help"
for a in range(2,n):
...

p(3)


def fct(a, b="bla"):
...

fct(3, "lala")
fct(3)

several arguments with *

def h(g, *args):
for m in args:
...

h("bl", "blub", "abc", "dbc")


def h(**args):
if len(args==0): return
for n in args.keys():
print args[n]+" : "+ n

h(v1="blub", v2="aga")

def complexparameters(firma, delim="-", *address, **inventars):

def doNothing():
pass


functionale coding

add = lambda x,y: x+y
add(2,3)

map(ord, "string")
[115,116,114,...]

[ord(a) for a in "string"]

map(lambda x:x+1,(1,2,3))
map for tuple (1,2,3) x+1

def p(n):
for a in range(2,n):
if n%a == 0:
return 0
else:
return 1

filter(p, range(100))


reduce(function, collection)

a=5
exec("var" +str(a)+"="+str(a))
=> var5=5

docstrings:
def bla():
""" blal bla"""
...

bla.__doc__

Modules

Keine Kommentare:

Kommentar veröffentlichen