2.1if-else语句
a =10
b =20if a > b and a or b :
c = a > b and a or b
print(c)2.2if-elif-else语句
import math
# 输入边长
a =int(input("请输入边长a(cm):"))
b =int(input("请输入边长b(cm):"))
c =int(input("请输入边长c(cm):"))if a <0or b <0or c <0:print("输入不合法")elif a + b <= c or a + c <= b or b + c <= a:print("不满足条件")elif a - b >= c or a - c >= b or b - c >= a:print("不满足条件")else:# 求取半边长
p =(a + b + c)/2# 计算面积
s = math.sqrt(p *(p - a)*(p - b)*(p - c))# 输出结果print("三角形面积为:", s)2.3 三元运算
a =int(input("请输入数字:"))print("可以上网"if a >18else"不可以上网")
3. 循环结构
3.1for语句
for i inrange(10):if i ==2:continue# 结束本次循环if i ==5:break# 跳出循环else:print(i)3.2while语句
# while 语句的功能是 只要condition的布尔值为True 则一直执行"do something"# 批量重命名,重复做一件事N次while condition:
do something
else:
do something
# condition 表示条件 条件的布尔值为True继续循环,布尔值为False,则结束循环# else 当前的while是正常循环退出时会执行else部分的代码3.3breakcontinuepassbreak结束循环
continue结束本次循环
pass占位符