python--函数

自定义函数一:

导入函数导入函数方法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

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值