一、邏輯控制語句
二、條件判斷 if
1、語法
if 條件:條件為真的操作條件為真的操作
else:條件為假的操作條件為假的操作
data_01 = int(input("數(shù)字: "))if data_01 > 10:print("ok!!!")print("正確!!!")print("------------")
data_01 = int(input("數(shù)字: "))# 數(shù)字不為0,表示條件為真
if data_01:print("A")
data_01 = int(input("數(shù)字: "))if data_01 > 10:print("ok!!!")
else:print("warn!!!")
score = int(input("成績: "))if score > 90 and score <= 100:print("優(yōu)秀")
elif score > 80 and score <= 90:print("良好")
elif score > 70 and score <= 80:print("中等")
elif score > 60 and score <= 70:print("及格")
else:print("差")
age = int(input("年齡: "))if age <= 20:username = input("姓名: ")if username == "張佳奇":print("槍斃")else:print("晉級")
else:print("淘汰")
三、for循環(huán)
1、語法
for 變量 in 取值列表:執(zhí)行的操作執(zhí)行的操作for i in range(3):print("python自動化運維")print("---------------")for i in range(1,11):print("ping -c 1 172.16.%s.1" % i)
四、while循環(huán)
1、常規(guī)語法
while 條件:執(zhí)行的操作執(zhí)行的操作
i = 1
while i <= 5:print("ping -c 1 172.16.%s.1" % i)i = i + 1
2、語法2
while True:執(zhí)行的操作執(zhí)行的操作
import timewhile True:print("python自動化運維")time.sleep(2)
五、終止循環(huán)的語句
1、break
for i in range(1,6):if i == 3:breakelse:print("ping -c 1 172.16.%s.1" % i)
2、continue
for i in range(1,6):if i == 3:continueelse:print("ping -c 1 172.16.%s.1" % i)