if語句
1>利用if語句判斷用戶是否被禁言
banned_users.py
banned_users=['Lily','jonh','Susan']
user='Lily'
if user not in baned_users:
print(user.title()+",you can post a response if you wish.")```
######2>if else 語句
```age=17
if age>=18:
print("You are old enough to vote!")
print("Have You registed to vote yet?")
else:
print("Sorry,you are too young to vote.")```
######3>if elif elif else語句(不一定有else語句結(jié)束蕊程,可以用elif語句結(jié)束)
amusement_park.py
```python
age=12
if age<=4:
price=0
elif age<=18:
price=5
elif age<=65:
price=10
else:
price=5```
toppings.py
```python
requested_toppings=['mushroom','extra cheese']
if 'mushroom' in requested_toppings:
print ("Adding mushroom.")
if 'extra cheese' in requested_toppings:
print("Adding extra cheese.")
print("Finished making your pizza.")```
記住哦:如果想執(zhí)行多個代碼代碼塊椒袍,就用多個if語句,如果想執(zhí)行代碼塊的部分內(nèi)容藻茂,就用if elif elif else語句
2>一個簡單的字典
驹暑!可以利用字典存儲一個對象的多種信息;
辨赐!也可以利用字典存儲多個對象的相似信息优俘;
alien.py
```python
>>> alien_0={}
>>> alien_0['color']='green'
>>> alien_0['point']=9
>>> print alien_0
{'color': 'green', 'point': 9}
>>> alien_0['color']='yellow'
>>> print alient_0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'alient_0' is not defined
>>> print alien_0
{'color': 'yellow', 'point': 9}
字典是可以不斷添加鍵值對的
例如往alien_0里面添加兩個鍵,x位置的坐標(biāo)掀序,以及y位置的坐標(biāo)帆焕。
插入字典的鍵值對不是按照時間先后順序的。
alien_0['y_position']=25
>>> print alien_0
{'color': 'green', 'y_position': 1, 'x_position': 0, 'point': 5}
利用字典存儲alien的速度森枪,從而計算alien的移動速度视搏。
用if語句判斷怎么計算在x軸方向的增量
alien_0={'x_position':1,'y_position':3,'speed':'slow'}
print ('original_x_position:'+str(alien_0['x_position']))
if alien_0['speed']=='slow':
x_increment=1
elif alien_0['speed']=='medium':
x_increment=30
else:#this alien's speed must be very fast
x_increment=300
#new position=old_position +x_increment
alien_0['x_position']+=x_increment
print ("new_position:"+str(alien_0['x_position']))
輸出>>>
winney@winney-fighting:~/桌面$ python test.py
original_x_position:1
new_position:2```
刪除鍵值對
审孽!直接刪除鍵時,連對應(yīng)的值也會被刪除的
浑娜!并且是永久刪除
關(guān)于遍歷字典中的所有鍵值對
```python
for key,value in alien_0.items():
print('key:'+key)
print('value:'+value)
遍歷所有的鍵佑力,與值無關(guān)
for key in alien_0.keys():
print key```
可以用sorted獲得按一定順序排序的字典(記住:sort()函數(shù)是永久排序筋遭,sorted()是創(chuàng)建排序后的字典副本)
```python
for name in sorted(list_langusge).keys():
print name
想要獲取字典中沒有重復(fù)的鍵或者值時打颤,可以用函數(shù)set()
for key,value in set(list_language.items()):
print ("keys were mentioned are:"+key)
print ("values were mentioned are:"+values)```
嵌套:將一系列字典存儲在列表中或者將一系列的列表作為值存儲在字典中。
字典列表:(將字典儲存在列表中)
```python
alien_0={
'color':'green',
'point':5
}
alien_1={
'color':'yellow',
'point':10
}
alien_2={
'color':'red',
'point':15
}
aliens=[alien_0,alien_1,alien_2,]
for alien in aliens:
print alien
aliens=[]
for alien_number in range(30):
new_alien={
'color':'green',
'point':'25'
}
aliens.append(new_alien)
for alien in aliens[:5]:
print alien
print ("....")
print ("Total number of aliens:"+str(len(aliens)))
winney@winney-fighting:~/桌面$ python test.py
{'color': 'green', 'point': '25'}
{'color': 'green', 'point': '25'}
{'color': 'green', 'point': '25'}
{'color': 'green', 'point': '25'}
{'color': 'green', 'point': '25'}
....
Total number of aliens:30```
在字典中存儲列表
```Python
pizza={
'crust':'thick',
'topping':['mushroom','cheese']
}
print ("You order a "+pizza['crust']+" crust pizza with the following toppings:")
for topping in pizza['topping']:
print topping
在字典中存儲字典
例如:
把用戶名當(dāng)做鍵漓滔,把用戶信息(是一個字典:含有用戶全名编饺,用戶城市)作為值
users={
'user_1':{
'full_name':'winney',
'city':'GuangZhou',
},
'user_2':{
'full_name':'sam',
'city':'ChengDu',
},
}
for user,user_info in sorted(users.items()):
print("Username:"+user)
print("User information:")
print user_info['full_name']
print user_info['city']