python必备知识点
-
变量
- 数字、字母、下划线任意组成
- 不能以数字开头
- 不能使用python关键字
-
input输入
name = input('please input your name:') print(name)
input输入数据类型为字符串
-
if条件语句
-
if条件
a = 1 if a>0: print('yes')
-
if …else…
a = 1 if a>0: print('yes') else a<0: print('no')
-
if…elif…elif…
a = 3 if a=1: print('one') elif a=2: print('two') elif a=3: print('three')
-
if 嵌套
-
-
while循环
-
无限循环
while True: print('1') print('2')
-
有限循环
-
改变条件
flag = True while flag: print('1') print('2') flag = False print('3') print('4')
运行结果:
-
break(遇到break直接跳出循环)
while True: print('1') print('2') break print('3') print('4')
运行结果:
-
-
-
continue
退出本次循环,继续下一次循环
flag = True while flag: print('1') print('2') continue print('3') print('4')
-
while else
count = 1 while count<5: print(count) count = count+1 else: print('666')
注意:while循环如果被break打断,则不执行else语句
count = 1 while count<5: print(count) if count ==2: break count = count+1 else: print('666')
运行结果: