#!/usr/bin/env python
#-- coding:utf-8 --
#1.简单的函数
def greet_user(): # 函数定义
print("Hello!") """显示简单的问候语"""
greet_user() # 函数调用
#2.函数带参数
def greet_user(username):
print("Hello, " + username.title() + "!") """显示简单的问候语"""
greet_user("jesse") # 单引号、双引号都可以
#3.位置实参
def describe_pet(animal_type, pet_name):
"""显示宠物的信息"""
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet('hamster', 'harry')
#4.关键值实参
def describe_pet(animal_type, pet_name):
"""显示宠物的信息"""
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet(animal_type='hamster', pet_name='harry')
describe_pet(pet_name='harry', animal_type='hamster')
#5.给形参指定默认值
def describe_pet(pet_name, animal_type='dog'): # 有默认值的形参放在后面
"""显示宠物的信息"""
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet(pet_name='willie')
describe_pet('willie')
describe_pet('willie', 'cat')
#6.返回值
def get_formatted_name(first_name, last_name):
"""返回整洁的姓名"""
full_name = first_name + ' ' + last_name
return full_name.title()
musician = get_formatted_name('max', 'liu')
print(musician)
#7.实参变为可选的
def get_formatted_name(first_name, last_name, middle_name = ''):
"""返回整洁的姓名"""
if middle_name:
full_name = first_name + ' ' + middle_name + ' ' + last_name
else:
full_name = first_name + ' ' + last_name
return full_name.title()
musician = get_formatted_name('john', 'lee', 'hooker')
print(musician)
musician = get_formatted_name('john', 'hooker')
print(musician)
#8.1返回字典
def build_person(first_name, last_name):
"""返回一个字典, 其中包含有关一个人的信息"""
person = {'first': first_name, 'last': last_name}
return person
musician = build_person('Max', 'Liu')
print(musician)
#8.2.利用可变实参,扩展字典
def build_person(first_name, last_name, age = ''):
"""返回一个字典, 其中包含有关一个人的信息"""
person = {'first': first_name, 'last': last_name}
if age:
person['age'] = age # 扩展字典
return person
musician = build_person('Max', 'Liu', 18)
print(musician)
#9.函数和while结合
def get_formatted_name(first_name, last_name):
"""返回整洁的姓名"""
full_name = first_name + ' ' + last_name
return full_name.title()
#这是一个无限循环!
while True:
print("\nPlease tell me your name:")
print("(enter 'q' at any time to quit)")
f_name = input("First name: ")
if f_name == 'q':
break
l_name = input("Last name: ")
if l_name == 'q':
break
formatted_name = get_formatted_name(f_name, l_name)
print("\nHello, " + formatted_name + "!")
#10.传递列表
def greet_users(names):
"""向列表中的每位用户都发出简单的问候"""
for name in names:
msg = "Hello, " + name.title() + "!"
print(msg)
usernames = ['hannah', 'ty', 'margot']
greet_users(usernames)
#11.在函数中修改列表
def print_models(unprinted_designs, completed_models):
"""
模拟打印每个设计, 直到没有未打印的设计为止
打印每个设计后, 都将其移到列表completed_models中
"""
while unprinted_designs:
current_design = unprinted_designs.pop()
# 模拟根据设计制作3D打印模型的过程
print("Printing model: " + current_design)
completed_models.append(current_design)
def show_completed_models(completed_models):
"""显示打印好的所有模型"""
print("\nThe following models have been printed:")
for completed_model in completed_models:
print(completed_model)
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
#print_models(unprinted_designs, completed_models)
print_models(unprinted_designs[:], completed_models) # 切片表示法[:] 创建列表的副本,原来的列表unprinted_designs不变
show_completed_models(completed_models)
print(unprinted_designs)