定义做衣服函数,传参数大小和印在Tee上的字
def make_shirt(size,text="I love python"):
print("Your shirt making now.Wait PLS")
print("
size: " + size.upper())
print("
text: " + text.title())
make_shirt(size = "xxl")
输出:
Your shirt making now.Wait PLS
size: XXL
text: I Love Python
return
def get_formatted_name(first_name,last_name): full_name = first_name + ' ' + last_name return full_name.title() # musician = get_formatted_name('jeremy','brett') print(musician) 输出: Jeremy Brett
python将非空字符串解读为True
返回字典
def build_person(first_name,last_name): person = {'first':first_name,'last':last_name} return person musician = build_person('jimi','hendrix') print(musician) 输出: {'first': 'jimi', 'last': 'hendrix'}
结合while
def get_formatted_name(first_name,last_name): full_name = first_name + ' ' + last_name return full_name.title() while True: print(" Please tell me your name: enter 'q' at any time to quit!") first_name = input("Enter first name: ") if first_name == 'q': break last_name = input("Enter last name: ") if last_name == 'q': break fullname = get_formatted_name(first_name,last_name) print(" Hello, " + fullname + "!") 输出: Please tell me your name: enter 'q' at any time to quit! Enter first name: alex Enter last name: han Hello, Alex Han! Please tell me your name: enter 'q' at any time to quit! Enter first name: suki Enter last name: zhu Hello, Suki Zhu! Please tell me your name: enter 'q' at any time to quit! Enter first name: q
任意数量参数
def make_pizza(size,*toppings): print("Making a " + str(size) + "-inch pizza with the following toppings:") for topping in toppings: print("- " + topping) make_pizza(12,'pepperoni') make_pizza(16,'mushrooms','green peppers','extra cheese') 输出 Making a 12-inch pizza with the following toppings: - pepperoni Making a 16-inch pizza with the following toppings: - mushrooms - green peppers - extra cheese
def build_profile(first,last,**user_info): profile = {} profile['first'] = first profile['last'] = last for key,value in user_info.items(): profile[key] = value return profile user_profile = build_profile('albert','einstein',location = 'princeton',field = 'physics') print(user_profile) 输出: {'first': 'albert', 'last': 'einstein', 'location': 'princeton', 'field': 'physics'}
==================================
导入整个模块
文件pizza.py内容
def make_pizza(size,*toppings): print("Makeing a " + str(size) + "-inch pizza with the following toppings:") for topping in toppings: print("- " + topping)
文件making_pizza.py内容
import pizza #导入模块 pizza.make_pizza(16,'pepperoni') #使用模块中的函数,module_name.function_name() pizza.make_pizza(12,'mushrooms','green peppers','extra cheese')
输出结果:
Makeing a 16-inch pizza with the following toppings: - pepperoni Makeing a 12-inch pizza with the following toppings: - mushrooms - green peppers - extra cheese
导入特定函数:from module_name import function_name_0,function_name_1,function_name_2
文件making_pizza.py内容可改写为:
from pizza import make_pizza make_pizza(16,'pepperoni') make_pizza(12,'mushrooms','green peppers','extra cheese')
from pizza import make_pizza as mp #避免导入函数名与本程序中的同名,将导入的函数起了别名
make_pizza(16,'pepperoni')
make_pizza(12,'mushrooms','green peppers','extra cheese')
导入模块中的所有函数:from module_name import *(无需使用句点)