Python學習的第六天
- 前言
- if陳述句的補充
- 字典
前言
今天是學習python的第六天,太忙了,所以更新有點推遲了,話不多說咱們直接上干貨吧,
if陳述句的補充
省略else代碼塊
為什么要省略else陳述句?
因為else是一條包羅萬象的陳述句,只要不滿足任何if或者elif中的條件測驗,其代碼就會被執行,這可能會引入無效或者惡意的資料,
測驗多個條件(僅使用多個if陳述句)
目的:檢查我所測驗的條件是否出現錯誤,并且對多個同時進行提高檢測效率
request_toppings = ['mushroom','extra cheese']#對多個條件進行檢測時,我們使用的是單個if陳述句而不是if_elif_else結構來進行檢測的
if 'mushroom' in request_toppings:
print("Adding mushrooms")
if 'pepperoni' in request_toppings:
print("Adding pepperoni")
if 'extra cheese' in request_toppings:
print("Adding extra cheese")
print("\n Finished making your pizza")
結果:
Adding mushrooms
Adding extra cheese
Finished making your pizza
使用if陳述句來處理串列
主要作用是:對特定的元素進行特定的操作
檢查特殊元素
request_toppings = ['mushroom','green peppers','extra cheese']
for request_topping in request_toppings:#for的用法:for + 變數 in 串列:就歐克了 其實就相當于遍歷
if request_topping == 'green peppers':#這里的特殊元素是“green peppers”
print("Sorry ,we are out of green peppers right right now")
else:
print(f"Adding {request_topping}.")
print("\nFinished making your pizza")
結果:
Adding mushroom.
Sorry ,we are out of green peppers right right now
Adding extra cheese.
Finished making your pizza
確定串列不是空的
確定串列是不是空的我們所使用的陳述句是 if與for鑲嵌 將串列中的元素作為if的判斷條件
request_toppings = []
if request_toppings:#如果使用一個串列用作條件的話,Python將在串列內容為空的情況下 回傳False,當至少存在一個元素時都會回傳True
for request_topping in request_toppings:
print(f"Adding {request_topping}.")
print("\nFinished making your pizza!")#如果有元素則會列印出該陳述句
else:
print("Are you sure you want a plain pizza")
結果:
Are you sure you want a plain pizza
使用多個串列
使用的場景,假如在一家披薩店,有一個官方的配料表格,而一些顧客缺喜歡往披薩里面加入一些非常奇怪的配料,但是我們也得滿足顧客的需求
available_toppings = ['mushroom','olives','green peppers','pepperoni','pineapple','extra cheese']#定義官方的選單
requested_toppings = ['mushroom','french fries','extra cheese']#這里是客戶所需要的選單
for requested_topping in requested_toppings:#回圈客戶中的選單
if requested_topping in available_toppings:#將顧客的表單中的元素在官方的選單中進行對應
print(f"Adding {requested_topping}.")
else:
print(f"Sorry,we don't have {requested_topping}.")#而“french fries”是官方選單中沒有的所以我們將其列印出來,并進行解釋
print("\nFinished making your pizza")
結果:
Adding mushroom.
Sorry,we don’t have french fries.
Adding extra cheese.
Finished making your pizza
設定if陳述句
在變數的前后加上空格和符號前后,使得編碼更加的美觀
字典
字典相當于,一個線索對應一個值吧,可以這樣理解,
一個簡單的字典
yinyang = {'age':'18','tall':'180'}
print(yinyang['age'])
print(yinyang['tall'])
結果:
18
180
總結:字典的名稱是yinyang,它儲存了年齡:18,身高:180,而且這里字典的定義是使用的“{}”,并且每類資訊對應的具體資訊是使用冒號來的
使用字典
alien_0 = {'color':'green','points':'5'}
new_points = alien_0['points']
print(f"You just earned {new_points} points!")
結果:
You just earned 5 points!
總結:當想要使用一個字典時,首先得進行定義,其次需要定義一個新變數將兩者相關聯,再使用于相關的陳述句,
注意:color:green 是一對鍵值對 'color’是鍵,'green’是值,兩個一塊稱為鍵值對,
添加鍵值對
字典往往需要進行更新,所以得添加鍵值對
alien_0 = {'color':'green','points':'5'}
print(alien_0)
alien_0['x_position'] = 0
alien_0['y_position'] = 25
print(alien_0)
結果:
{‘color’: ‘green’, ‘points’: ‘5’}
{‘color’: ‘green’, ‘points’: ‘5’, ‘x_position’: 0, ‘y_position’: 25}
總結:對于鍵值對的添加,首先要明確的是你定義了一個字典,并且進行添加鍵值對的格式是:字典名[‘類別名’] = 具體資訊,并且加入的數值也是按順序來的
今天就到這里了,其實整篇看下來應該也不到20來分鐘吧,應該會更短的,
加油哦!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/279262.html
標籤:python
