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 "<stdin>", line 1, in <module>
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']