美文网首页
Python 函数

Python 函数

作者: E_H_I_P | 来源:发表于2017-12-08 11:38 被阅读0次

参数检查

def my_abs(x):
    if not isinstance(x,(int,float)):
        raise TypeError('Bad operand type')
    if x >= 0:
        return x
    else:
        return -x
a = my_abs(-10)
print(a)
  • 使用了isinstance函数进行数据类型的检查

默认参数

  • 例:
def power_n(x,n=2):
    s = 1
    while n > 0:
        n = n - 1
        s = s * x
    return s

a = power_n(5,3)
print(a)
  • 如果不写3默认函数power_n的参数为2

相关文章

  • Python - 2017/01/28-函数

    调用python内置函数 函数名(参数) 即可调用python内置函数 help(函数名) 返回python对于函...

  • Python函数式介绍一 - 高阶函数

    Python函数式介绍一 - 高阶函数Python函数式介绍二 - 链式调用 最近为了给朋友推广Python函数式...

  • Python高阶函数学习笔记

    python中的高阶函数是指能够接收函数作为参数的函数 python中map()函数map()是 Python 内...

  • Python学习笔记1

    Python注释 Python变量 Python运算符 Python输入输出 输入函数 输出函数(3.x) ...

  • Python:内置函数

    python的内置函数,匿名函数 内置函数 内置函数就是python给你提供的,拿来直接用的函数,比如print,...

  • 二级Python----Python的内置函数及标准库(DAY

    Python的内置函数 嵌入到主调函数中的函数称为内置函数,又称内嵌函数。 python的内置函数(68个) Py...

  • python3 range() 函数和 xrange() 函数

    python3 range 函数 python3 取消了 xrange() 函数,并且和 range() 函数合并...

  • 7、函数

    1、Python之什么是函数 2、Python之调用函数 Python内置了很多有用的函数,我们可以直接调用。 要...

  • Python入门

    Python3教程 安装Python 第一个Python程序 Python基础 函数 高级特性 函数式编程 模块 ...

  • Python函数详解

    函数是Python里组织代码的最小单元,Python函数包含以下几个部分: 定义函数 调用函数 参数 函数的返回值...

网友评论

      本文标题:Python 函数

      本文链接:https://www.haomeiwen.com/subject/taotixtx.html