6、Python_函数调用与定义

1、基本常用函数:

    abs(100)---->绝对值

    max(1,2)---->取大值

    int('123') ---->类型转换

    float('123') ---->类型转换

    str(123) ---->类型转换

    bool(1) ---->类型转换

    hex(18)---->十进制转十六进制

    power(2)---->开平方

    strip()------->字串两头去空格

    sum(list)-------->求和

    string.upper()----->转大写

    string.lower()----->转小写

    string.find('c')------>找到字符在字串中的索引位置

2、函数赋值:

    a = abs;

    a(-1)

    ------------>结果:1

3、函数定义(abstest.py):

    def my_abs(x):

        if x >= 0:

            return x

        else :

            return -x

    函数无return语句,默认返回None。return None 可以简写成return

4、函数引入并调用:

    #引入

    from abstest import my_abs

    #调用

    my_abs(-9)

5、空函数:

    def nop():

        pass

    pass可以当语句占位符

    isinstance()---->用于检查类型是否匹配

6、返回多个值:

    def move(x,y,step,angle):

        nx = x + step * math.cos(angle)

        ny = y - step * math.sin(angle)

        return nx,ny

    其实返回的是一个tuple

    t = (12,333)

猜你喜欢

转载自blog.csdn.net/qiangzai110110/article/details/85128270