日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python筆記3

系統(tǒng) 1972 0
閱讀更多
####if語句###
cars=['audi','bmw','subaru','toyota']
for car in cars:
if car=='bmw':?? #==檢查是否相等? 即相等時返回Ture,不相等時返回Flase
print(car.upper())
else:
print(car.title())

cars=['audi','bmw','subaru','toyota']
for car in cars:
if car !="bmw":?? #!=檢查是否不相等? 即不相等時返回Ture,相等時返回Flase
print(car.upper())
else:
print(car.lower())

#補python終端檢查是否相等
#>>>car='bmw'
#>>>car=='bmw'則輸出Ture 否則返回Flase, !=同理

#if例子
#檢查一個人是否是20歲(單個條件)
answer=18??
if answer!=20:? #當然條件可以是大于小于等等 <,>,<=,>=,等等
print('That id not the correct answer,Please try again!')

#檢查多個條件 and ###關鍵字or與and類似 不予列舉
answer=18??
if answer>=17 and answer<=20:
print('That id not the correct answer,Please try again!')
#靈活運用 可對多個人進行檢查
answer_1=18
answer_2=20?
if answer_1>=16 and answer_1<=20 and answer_2<30:
print('That id not the correct answer,Please try again!')

##檢查特定值是否在列表中:可利用python終端來執(zhí)行##
#1.>>>cars=['audi','bmw','subaru','toyota']
#>>>"bmw" in cars
#2.
cars=['audi','bmw','subaru','toyota']
car='bmw'
if car in cars:? #in前加not即沒有在列表內
print(car.title()+' '+"in the list")
######
#if-elif-else結構 ##只能測試指定一個條件(通過一個測試條件將跳過余下的測試)#
#可根據需要使用任意數量的elif代碼塊
age=15
if age<4:
price=0 #print("Your admission cost is $0")
elif age<18:
price=5? #print("Your admission cost is $5")
else:
price=10? #print("Your admission cost is $10")
print("Your admission cost is $"+str(price)+".")#str()函數:將某一個類型強制轉換為字符串型。如,a = 1,a的類型就是數值型,a = str(a),a就是字符串型了
##測試多個條件
print('\n')
requested_toppings=['mushrooms','extra cheese']
if 'mushrooms' in requested_toppings:
print("Adding mushrooms.")
if 'extra cheese' in requested_toppings:
print("Adding extra cheese.")
if 'pepperoni' in requested_toppings:
print("Adding pepperoni.")
print("Finished making your pizza!")
#與for循環(huán)連用 (檢查列表元素中有無特殊元素)
#披薩店滿足顧客的單個條件
print('\n')
requested_toppings=['mushrooms','extra cheese','pepperoni','green pappers']
for requested_topping in requested_toppings:
if requested_topping=='pepperoni':
print("Sorry,we are out of pepperoni right now.")
else:
print('Adding '+requested_topping+'.')
print('Finished making your pizza!')
#披薩店滿足顧客的多個條件
print('\n')
available_toppings=['mushrooms','extra cheese','pepperoni','green pappers','olives']
requested_toppings=['mushrooms','extra cheese','pineapple']
for requested_topping in requested_toppings:
if requested_topping in available_toppings:
print('Adding '+requested_topping+'.')
else:
print("Sorry,we don't have"+requested_topping+'.')
#判斷列表是否為空
print('\n')
requested_toppings=[]
if requested_toppings:? #如果該列表為空時返回False;至少有一個元素時返回True,進行下面的for循環(huán)
? for requested_topping in requested_toppings:
? print('Adding '+requested_topping+'.')
else:
print("Are you sure you want a plain pizza?")

更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 广丰县| 礼泉县| 郯城县| 江城| 正镶白旗| 仙居县| 思南县| 平山县| 台南市| 涿州市| 延寿县| 鹤壁市| 嘉禾县| 蒙阴县| 营山县| 惠安县| 瓮安县| 万源市| 无锡市| 仲巴县| 樟树市| 濮阳市| 乐至县| 竹山县| 宾阳县| 连南| 许昌县| 庐江县| 琼海市| 留坝县| 青岛市| 富民县| 左云县| 泸溪县| 西盟| 满城县| 忻城县| 方山县| 铜陵市| 佛坪县| 嘉义县|