1.代码格式
2.基本语法
syntax基本语法
a = 1234 print(a) a = 'abcd' print(a) try: print(b) except Exception as e: print(e) a = [1, 2, 3 , 4] def func(a): a[0] = 2 func(a) print(a) try: # Python 2.x 支持 print(100, 200, 300) except Exception as e: print(e)
1234 abcd name 'b' is not defined [2, 2, 3, 4] 100 200 300
condition_and_loop 循环判断
score = 80 if score > 90: print('A') elif score > 70: print('B') elif score >= 60: print('C') else: print('D') total = 0 i = 1 while i <= 100: total += i i += 1 # 没有++i或者--i print(total) ''' for循环只作用于容器!!! 没有这种写法: for (i = 0; i < 100; ++i): # TODO 上面这种循环只能用while实现 ''' i = 0 while i < 3: j = 0 while j <= 3: if j == 2: j += 1 continue # 又去了while j <= 3 print(i, j) j += 1 i += 1
B 5050 0 0 0 1 0 3 1 0 1 1 1 3 2 0 2 1 2 3
func函数
1 def hello(who = 'world'): 2 print('hello %s!' % (who)) 3 4 hello() 5 hello('sea') 6 7 # f(x) = x * 5 + 100 8 # g(x) = x * 5; f(x) = x + 100 9 # => f(g(x)) = x * 5 + 100 10 def g(x): 11 return x * 5 12 def f(gf, x): 13 return gf(x) + 100 14 print(f(g, 100)) 15 print(f(lambda x: x * 5, 100)) 16 17 def f(gf, x, y): 18 return gf(x, y) + 100 19 print(f(lambda x, y: x * y, 100, 200)