日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python之if经典语句_Python之if语句、字典

發布時間:2025/3/12 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python之if经典语句_Python之if语句、字典 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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語句結束,可以用elif語句結束)

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.")```

記住哦:如果想執行多個代碼代碼塊,就用多個if語句,如果想執行代碼塊的部分內容,就用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 "", line 1, in

NameError: name 'alient_0' is not defined

>>> print alien_0

{'color': 'yellow', 'point': 9}

字典是可以不斷添加鍵值對的

例如往alien_0里面添加兩個鍵,x位置的坐標,以及y位置的坐標。

插入字典的鍵值對不是按照時間先后順序的。

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```

刪除鍵值對

!直接刪除鍵時,連對應的值也會被刪除的

!并且是永久刪除

關于遍歷字典中的所有鍵值對

```python

for key,value in alien_0.items():

print('key:'+key)

print('value:'+value)

遍歷所有的鍵,與值無關

for key in alien_0.keys():

print key```

可以用sorted獲得按一定順序排序的字典(記住:sort()函數是永久排序,sorted()是創建排序后的字典副本)

```python

for name in sorted(list_langusge).keys():

print name

想要獲取字典中沒有重復的鍵或者值時,可以用函數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

在字典中存儲字典

例如:

把用戶名當做鍵,把用戶信息(是一個字典:含有用戶全名,用戶城市)作為值

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']

總結

以上是生活随笔為你收集整理的python之if经典语句_Python之if语句、字典的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。