温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python 函数的定义

发布时间:2020-06-05 04:33:53 来源:网络 阅读:497 作者:虎皮喵的喵 栏目:编程语言

格式:

        def 函数名(参数):

                内容 / pass;# pass为占位符,空

               return; /return None;


一、普通函数

#!/usr/bin/python

def my_abs(x): #空函数
    None;


def my_pass(): #空函数
    pass;


def my_print(x):
    print "haha"


def my_try(a):
    return None;


二、函数参数检查:isinstance(参数,参考的tuple)

def your_age(x)

    if not isinstance(x, (int, float)):   #检查参数x 是否为tuple中的int/float类型

        raise TypeError('the age's type is error, please check again');  #出错信息打印,类似于C的perror();

    if x < 18:

        print 'teenager';

    if x >= 18:

        print 'adult';

    return None;


三、返回多个值

def my_abs(a, b)

    ret1 = abs(a);

    ret2 = abs(b);

    return ret1, ret2;  #其实返回值是tuple类型,ret1,re2是其元素;



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI