if / if...else / if...elif...else 语句
1 a = 1 2 b = 2 3 if a < b: 4 print('a < b') 5 if a > b: 6 print('a > b')
1 a = 1 2 b = 2 3 if a < b: 4 print('a < b') 5 else: 6 print('a > b')
1 # 这里介绍 if elif else语句 2 3 a = 1 4 b = 2 5 c = 3 6 7 if a > b: 8 print('a > b') 9 elif b < c: 10 print('b < c') 11 else: 12 print('上面都不执行,这里执行')
for / while 循环语句
for i in range(1,11): print(i)
1 a = 0 2 b = 1 3 while a < 10: 4 print(a) 5 a, b = b, a+b
berak / continue 停止语句
for n in range(2, 10): for x in range(2, n): if n % x == 0: print(n, '=', x, '*', n//x) else: print(n, '是质数') break
for num in range(2, 10): if num % 2 == 0: print("偶数: ", num) continue else: print("奇数: ", num)
定义函数: def
def compare(): print('一句话') c = 1 + 2 print(c) compare() #不调用不执行
定义类: class
class Lei(): def a(): print('a') def b(): pass Lei.a()
全局/局部 变量
''' 全局变量:在当前.py文件内,都随意地方都可以调用,例如函数内部调用全局变量 局部变量:局部变量一般定义在函数体内部,只能当前这个函数调用,超过这个范围,其他无权访问这个局部变量 ''' x = 6 def printFuc(): y = 8 z = 9 print(y + z) print(x) # 函数内部可以调用局部变量 printFuc() # print(y) 提示y没有定义,这个地方调用局部变量会报错
困 -.-