条件控制语句:if
循环语句:while、for
其他语句:continue、break、pass
1、if语句
# ================================ a = 1 if a < 2: print(‘true‘) else: print(‘false‘) # ================================ if a: print(‘true‘) else: print(‘false‘) # ================================ if a == 0: print(‘a = 0‘) elif a == 1: print(‘a = 1‘) elif a == 2: print(‘a = 2‘) else: print(‘a为其他值‘)
2、while语句
# =================================== a = 1 sum = 0 while a <= 100: sum += sum a += 1 print(‘1到100的和为:‘, sum) # =================================== a = 0 while a < 5: print(‘a小于5‘) a += 1 else: print(‘a大于等于5‘) # =================================== a = 0 while a < 5: a += 1 if a == 2: continue if a == 4: break print(a, end=‘ ‘)
3、for语句
# ================================== lst = [‘aaa‘, ‘bbb‘, ‘ccc‘, ‘ddd‘] for s in lst: print(s, end=‘ ‘) print() # ================================== lst = [‘aaa‘, ‘bbb‘, ‘ccc‘, ‘ddd‘] for s in lst: if s == ‘aaa‘: continue if s == ‘ccc‘: break print(s, end=‘ ‘) else: print(‘finish‘) print()
4、pass语句
a = 0 if a == 0: pass elif a == 1: pass elif a == 2: pass else: pass
原文地址:https://www.cnblogs.com/wbz-blogs/p/12081559.html
时间: 2024-10-29 16:25:56