比較運(yùn)算符
邏輯運(yùn)算符
## 比較運(yùn)算符
a, b, c = 10, 3, 7
print(a >= b) #True
print(a > b) #True
print(a < b) #True
print(a <= b) #True
print(a == b) # False
print(a != b) # False
# 邏輯運(yùn)算符 與 或 非
print((a > c) and (a > b)) # True &&
print((a > c) and (a < b)) # False
print((a > c) or (a > b)) # True ||
print((a < c) or (a > b)) # True
print((a < c) or (a < b)) # False
print(not (a < c)) # True !
程序執(zhí)行結(jié)構(gòu)
- 順序結(jié)構(gòu)
- 分支結(jié)構(gòu)
- 循環(huán)結(jié)構(gòu)
分支結(jié)構(gòu)(條件語(yǔ)句)
if語(yǔ)句
age = int(input("請(qǐng)輸入您的年齡"))
# TypeError: '>=' not supported between instances of 'str' and 'int'
if age >= 18 and age <=60:
print("恭喜你成年了, 可以去網(wǎng)吧了")
print("恭喜你成年了, 還不趕快行動(dòng)")
elif age > 60 and age <=150:
print("退休啦")
elif age >=0 and age <=18:
print("你還是個(gè)寶寶")
else:
print("輸入數(shù)據(jù)有誤")
作業(yè)
BMI計(jì)算器
計(jì)算公式為:BMI=體重÷身高^(guò)2。(體重單位:千克;身高單位:米低散。)
猜拳游戲
# 猜拳游戲
from random import randint
computer = randint(1, 3) # [1, 3] 左閉右閉
# print(computer)
player = int(input("請(qǐng)出拳: ?頭(1)剪?(2)布(3)"))
if (player==1 and computer==2)or(player==2 and computer==3)or(player==3 and computer==1):
print("玩家勝利啦")
elif player==computer:
print("平局")
else:
print("你真菜, 我贏了")
循環(huán)
# while
# while 條件:
# 循環(huán)體
i = 1
mySum = 0
while i <= 100:
print(i, end="\t")
mySum += i
i+=1
print()
print(mySum)
# break 跳出本層循環(huán)
# 累加和達(dá)到大于1000停止
i = 1
mySum = 0
while i <= 100:
if mySum > 1000:
break
mySum += i
i+=1
print()
print("mySum = ", mySum)
# continue 結(jié)束本次循環(huán)執(zhí)行下一次循環(huán)
# 1~100之間所有奇數(shù)的累加和
i = 1
mySum = 0
while i <= 100:
if i%2 == 0:
i+=1
continue
mySum += i
i+=1
print()
print("mySum = ", mySum)
for 循環(huán)
# for循環(huán)
# for 臨時(shí)變量 in 可迭對(duì)象:
# 循環(huán)體
# range(起始, 終止, 步長(zhǎng)) 左閉右開(kāi)區(qū)間 步長(zhǎng)不能為小數(shù)
# for i in range(10):
# for i in range(1, 101):
# for i in range(1, 101, 10):
mySum = 0
for i in range(1, 101):
mySum +=i
print("mySum = ", mySum)
# # break 跳出本層循環(huán)
# # 累加和達(dá)到大于1000停止
mySum = 0
for i in range(1, 101):
if mySum > 1000:
break
mySum +=i
print("mySum = ", mySum)
# # continue 結(jié)束本次循環(huán)執(zhí)行下一次循環(huán)
# # 1~100之間所有奇數(shù)的累加和
mySum = 0
for i in range(1, 101):
if i %2 == 0:
continue
mySum +=i
print("mySum = ", mySum)