1. unix ctrl+d,windows ctrl +z or import sys; sys.exit();
2. int, float, complex number(real,imag,abs), _ represent last mentioned number, string(\",\n\, r" " \n doesn't work, quotation can cross-reference ,+, auto connected;
word[:2] first two number; word[2:] all but the first two number; word[2:4]; can not rewrite string,such word[2] = 'x' wrong; index can be nagative,then count from right
Word[-1] last character; word[0] = word[-0]; word[-100:] work while word[-100] doesn't;);
unicode string(eg. u"hello world", u"hello\u0020world",str(u"abc"), u"吃饭".encode('utf-8'), unicode('\xc3\xa4','utf-8'))
linked table(a = ['abc','eft',123,222], a*3, a+['x'], can changed every element's value, remove: a[0:2] = [], replace: a[1:2] = [223,233], insert: a[1:1] = ['adfd','sdfdf'], insert a copy
at begin: a[:0] = a, len(a), )
3. Fibonacci example:
a,b = 0,1
while b<10:
print b
a,b = b,a+b
4. if x < 0:
statement
elif x == 0:
statement
else:
statement
5. a = ['one','two',three]
for x in a:
print x,len(x)
for x in a[:]:
if len(x) > 4: a.insert(0,x)
6. range(10) // ten linked number from 0 to 10
range(5,10) // five number from 5 to 10
range(0,10,3) // from 0 to 10 and increment is 3
range(-10,-100,-30) // similar to above
7. for n in range(2,10):
for x in range(2,n):
if n%x == 0:
print n,'equals',x,'*',n/x
break
else:
print n,'is primer a number'
8. pass doesn't do anything
9. function def
def flib(n): # first line must be retract
a,b = 0,1
while b<n:
print b, #space
a,b = b,a+b
flib(100)
flib # show the address of this function
f = fib # reference of this function
f(100)
print f(0) # print None
10. def fib2(n):
result = []
a,b = 0,1
while b<n:
result.append(b) # add element to linked table
a,b = b,a+b
return result
f100 = fib2(100)
f100
11. def ask_ok(prompt, series = 4, compaliant = 'Yes or no, please'):
while True:
ok = raw_input(prompt) # input with reminder
if ok in ('y','ye','yes'): return 1
if ok in ('n','no','none'):return 0
series = series - 1
if series<0:raise IOError, 'refusenik user'
print compaliant
i=5
def f(arg=i):
print arg
c=6
f() will print 5 # a argument will be assigned only once in using
12. def f(a,L = None)
if L is None:
L= []
else:
L.append(a)
13. call function by explicate parameters
def f(a,b = 'hah',c='what')
print a,b,c
f(100),f(10,b='vvv'),f(0,b='wqu',c='mater')
# required argument missing, non-key argument following key argument
# duplicate value for argument, unknow keyword
14. argument like *name must present before **name
we can consider **name as a dictionary
def f(one,*two,**three):
for x in two:print x
keys = three.keys()
keys.sort() # sort a list
for kw in keys: print kw, ':', three[kw]
15. call a function with changeble count of argument
def fprintf(file, format, *argv)
file.write( format % argv)
16. lambda example
def make_incremator(n)
return lambda x:x+n
f = make_incremator(10)
f(10) # result is 20
17. print function
def my_function()
""" Do nothing, but document it
Not, really, it doesn't do anything.
"""
pass
print my_function.__doc__