课上记录的一些python基础应用,都是很简单的。
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函数
def hello(who = 'world'):
print('hello %s!' % (who))
hello()
hello('sea')
# f(x) = x * 5 + 100
# g(x) = x * 5; f(x) = x + 100
# => f(g(x)) = x * 5 + 100
def g(x):
return x * 5
def f(gf, x):
return gf(x)