美文网首页
Python Day20 函数

Python Day20 函数

作者: 读书的番茄 | 来源:发表于2017-06-01 15:14 被阅读0次

1,函数关键字 def

举个例子:
>>> def MyFirstFunction():
    print('第一个函数!')

>>> MyFirstFunction()    #调用函数时只要输入函数名称即可
第一个函数!

2,函数的参数

举个例子:
>>> def MySecondFunction(name):    #()里的内容既是参数
    print(name + 'python')

>>> MySecondFunction('love,')
love,python

>>> def add1(num1, num2):        #函数可以设置多个参数,但是,参数越多越容易出错
    result = num1 + num2
    print(result)

>>> add1(2, 2)
4

3,习题,习题来自fishc

0. 编写一个函数power()模拟内建函数pow(),即power(x, y)为计算并返回x的y次幂的值。
>>> def power(x, y):
    result = 1
    for i in range(y):
        result *= x
    return result

>>> print(power(2, 3))
8
>>> print(power(3, 4))
81
>>> print(power(4, 2))
16 

相关文章

  • Python Day20 函数

    1,函数关键字 def 举个例子: 2,函数的参数 举个例子: 3,习题,习题来自fishc 0. 编写一个函数p...

  • 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 Day20 函数

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