自定义函数一:
导入函数导入函数方法1:(模块导入)
import test_function 导入函数
print(test_function.find_factor(10))
导入函数方法2:(函数调用)
from test_function import find_factor 调入函数
print(find_factor(10))
from test_function import * //导入所有函数
print(find_factor(10))
导入函数方法3:(模块名,函数名别名方式)
3.1模块别名:
import test_functions as t1 //设置模块别名t1
print(t1.find_factor(8)) //别名代替函数名调用函数
3.2函数别名:
from test_function import find_factor as f1 //设置函数别名f1
print(f1(10))
函数放到模块中:
模块搜索路径
import sys
自定义函数二:
参数变化
1.位置参数:
def test1(name,age):
print('姓名%s,年龄%s'%(name,str(age)))
test1('Tom',11)
即定义的参数与输出的结果一一对应,位置不能打乱。
2.关键字参数
test1(name = 'John',age = 20)
必须所有参数都要指定。
前一个可以不加参数名,后一个需要加
3.默认值
test1():报错缺失条件
test1('Tom',1):
test1(18):当输入值与默认值同时确定,即name='tom' age=19,test1(18),则会输出tom,18
4.不定长参数
1)传递任意数量参数值
def watermelon(name,*attributes): #带*的为可传递参数
print(name)
print(type(attributes))
description = ' '
for get_t in attributes: #以集合数据类型进行操作
description +=' '+get_t #形成字符串属性说明内容
print(description)
watemelon('西瓜','甜','无籽','红壤')
def watermelon(name, **attributes): # 带*的为可传递参数 print(name) print(type(attributes)) return attributes print(watermelon)
小练习:
定制手机套餐:
https://www.cnblogs.com/20860492232qqqqq/p/16719232.html