Python Study Notes Day 6

Sentence every day: Errors happen in the details, and success depends on the system.

if statement processing list

requested_toppings=['mushrooms','gress peppers','extra cheese']
for requested_topping in requested_toppings:
    print("adding"+requested_topping+'.')
print("\nFinished making your pizza!")
addingmushrooms.
addinggress peppers.
addingextra cheese.

Finished making your pizza!
requested_toppings=['mushrooms','gress peppers','extra cheese']
for requested_topping in requested_toppings:
    if requested_topping=='gress peppers':
        print("Sorry,we are out of green peppers right now")
    else:
        print("adding "+requested_topping+'.')
print("\ nFinished Making your Pizza! " )
 # traversing the list when requested_topping equal gress peppers, Print else statement, the if statement contrary Print
 
adding mushrooms.
Sorry,we are out of green peppers right now
adding extra cheese.

Finished making your pizza!
requested_toppings=[]
if requested_toppings:
    for requested_topping in requested_toppings:
        print("adding"+requested_topping+'.')
    print("\nFinished making your pizza!")
else:
    print("Are you sure you want a plain pizza?")
# 当列表为空时,for循环无法执行,执行了else
Are you sure you want a plain pizza?
avaiblable_toppings=['mushrooms','olives','gress peppers','pepperoni','pineapple','extra cheese']
requested_toppings=['mushrooms','french fries','extra cheese']
for requested_topping in requested_toppings:
    if requested_topping in avaiblable_toppings:
        print("adding"+requested_topping+'.')
    else:
        print("Sorry,we don't have"+requested_topping+".")
Print ( " \ nFinished Making your Pizza! " )
 # traversing the list requested_toppings, when one element is not present in the list avaiblable_toppings, the else statement is executed, whereas if statement execution
addingmushrooms.
Sorry,we don't havefrench fries.
addingextra cheese.

Finished making your pizza!

dictionary

The dictionary is wrapped by {}
alien_0={'color':'green','point':5}
print(alien_0['color'])
print(alien_0['point'])
# 'color':'green'组成了一个键值对
# 'color'代表了键,'green'代表值,两者一一对应
green
5
alien_0={'color':'green','point':5}
print(alien_0['color'])
# 通过访问字典的键来获取字典的值
green

添加键值对

alien_0={'color':'green','point':5}
print(alien_0)
alien_0['x_position']=0
alien_0['y_position']=25
print(alien_0)
# 字典[]=x 可以添加键值对
{'color': 'green', 'point': 5}
{'color': 'green', 'point': 5, 'x_position': 0, 'y_position': 25}
alien_1={}
alien_1['color']='green'
alien_1['point']=5
print(alien_1)
{'color': 'green', 'point': 5}

修改键值对

alien_0={'color':'green'}
print(alien_0)
alien_0['color']='bule'
print(alien_0)
# 通过对键的重新赋值来修改值
{'color': 'green'}
{'color': 'bule'}

删除键值对

alien_2={'color':'green','point':5}
print(alien_2)
del alien_2['color']
print(alien_2)
# del 键  删除键值对  删除后键值对将永远消失
{'color': 'green', 'point': 5}
{'point': 5}

Guess you like

Origin www.cnblogs.com/python-study-notebook/p/12682829.html