支持python 3.7
#自定义函数
#定义简单问候语
def hello():
print('Hello!')
hello()#调用函数
'''
consle:
Hello!
'''
#带参数的函数
def hello(name):
print(f'Hello,{name.title()}!')
hello('dream')
'''
consle:
Hello,Dream!
'''
#使用关键字
def hello(name,sent):
print(f'{sent.title()},{name.title()}!')
hello(name = 'dream',sent = 'hello')
'''
consle:
Hello,Dream!
'''
#提供默认值
def hello(name,sent = 'hello'):
print(f'{sent.title()},{name.title()}!')
hello(name = 'Dream')
'''
consle:
Hello,Dream!
'''
#参数可选(这里是ask参数可选)
def hello(name,ask = '',sent = 'hello'):
if ask:#如果ask填写了
print(f'{sent.title()},{name.title()}!{ask.title()}?')
else:
print(f'{sent.title()},{name.title()}!')
#提供返回值
def su(o,t,th):#不能和python中sum()写的一样
a = o + t + th #局部变量,只能在函数内使用
return a #如果漏掉了这一句,这个函数就没有返回值了
bb = su(1,1,3)
print(bb)
'''
consle:
5
'''
#多个参数
def pizza(*topping):
print('做一个有这些材料的披萨:')
for need in topping:
print(f'--------{need}')
pizza('蘑菇','青椒')
'''
consle:
做一个有这些材料的披萨:
--------蘑菇
--------青椒
'''
#收集任意数量的关键字实参
def new_user(family_name,name,**information):
information['姓'] = family_name
information['名'] = name
return information
user = new_user('姓','名字')
print(user)
'''
consle:
{'姓':'姓','名':'名字'}
'''
#导入自定义模块(起名字有点难)
'''第一个文件new_pizza.py'''
def make_pizza(*topping):
print('做一个有这些材料的披萨:')
for need in topping:
print(f'--------{need}')
'''第二个文件make_new_pizza.py'''
import new_pizza
new_pizza.make_pizza('青椒','蘑菇')
'''
我们还可以用:
1,from new_pizza import make_pizza 只导入一个方法
make_pizza(...)
2,from new_pizza import make_pizza 导入所有方法,运行时粘贴在你的程序中,使用方法如下
make_pizza(...)
'''
#没了