|
八.字典(键值对)
alien_0={'color':'green','points':5}
print(alien_0['color'])
print(alien_0['points'])
alien_0['x_position']=0
alien_0['y_position']=25#直接添加键值对
alien_0['color']='yellow'#直接修改键值对的值
del alien_0['points']#删除对应键值对
#遍历键值对
for key,value in alien_0:
print(f"\nKey:{key}")
print(f"Value:{value}")
for name in alien_0.keys():
print(name.title())
for name in alien_0.values():
print(name.title())#单独提键,单独提值字典列表
alien_0={'color':'green','points':5}
alien_1={'color':'yellow','points':6}
alien_2={'color':'red','points':7}
aliens=[alien_0,alien_1,alien_2]
for alien in aliens:
print(alien)
#批量创建字典,并修改前三个的值
for alien_number in range (30):
new_alien = {'color':'green', 'points':5, 'speed':'slow'}
aliens.append(new_alien)
for alien in aliens[:3]:
if alien['color']== 'green':
alien['color'] = 'yellow'
alien['speed'] = 'medium'
alien['points'] = 10
#字典中存储列表
pizza={'crust':'thick','toppings':['mushrooms','extra cheese']}
#也可以在字典中存储字典九.输入函数
input()
name=input("Please enter your name")#input只接受字符串
print(f"\nhello,{name}!")
#int可以将输入值转化为数值
height=int(height)
十.使用while
unconfirmed_user=['alice','bran','kelly']
confirmed_user=[]
while unconfirmed_user:
current_user=unconfirmed_user.pop()
confirmed_user.append(current_user)十一.函数
def greet_user(username):#定义函数
print(f"Hello,{username.title()}")
greet_user('jesse')#函数调用,username为形参,jesse为实参函数参数
1.位置实参(有两个形参或多个形参,则传递实参时,默认一一对应)
def describe_pet(animal_type, pet_name):
print(f"\nI have a {animal_type}.")
print(f"My {animal_type}'s name is {pet_name.title()}.")
describe_pet('hamster', 'harry')2.关键字实参
def describe_pet(animal_type, pet_name):
print(f"\nI have a {animal_type}.")
print(f"My {animal_type}'s name is {pet_name.title()}.")
describe_pet(animal_type='hamster', pet_name='harry')3.默认值
def describe_pet(pet_name, animal_type='dog'):
print(f"\nI have a {animal_type}.")
print(f"My {animal_type}'s name is {pet_name.title()}.")
describe_pet(pet_name='willie')#如果显式为animal_type提供参数,则不再使用默认值
返回值
def get_formatted_name(first_name, last_name):
full_name = f"{first_name} {last_name}"
return full_name.title()
musician = get_formatted_name('jimi', 'hendrix')
print(musician)
#返回值可选
def get_formatted_name(first_name, last_name, middle_name=''):
if middle_name:
full_name = f"{first_name} {middle_name} {last_name}"
else:
full_name = f"{first_name} {last_name}"
return full_name.title()
musician = get_formatted_name('jimi', 'hendrix')
print(musician)
musician = get_formatted_name('john', 'hooker', 'lee')
print(musician)
#返回字典
def build_person(first_name, last_name):
person = {'first':first_name, 'last':last_name}
return person
musician = build_person('jimi', 'hendrix')
print(musician)
#传递列表
def greet_users(names):
for name in names:
msg = f"Hello, {name.title()}!"
print(msg)
usernames = ['hannah', 'ty', 'margot']
greet_users(usernames)
#向函数传递列表的副本,即所有修改都在副本中,而不影响unprinted_designs本身
print_models(unprinted_designs[:],completed_models)
#传递任意数量的实参
def make_pizza(*toppings):
print("\nMaking a pizza with the following toppings:")
for topping in toppings:
print(f"- {topping}")
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
#导入模块
import 模块名称(导入后,即可使用模块中的函数甚至类) |
|