#1 内建函数

  乘方:pow()

  >>> pow(2,3)
  8
  >>>

  取绝对值:abs()

  >>> abs(-1)
  1
  >>>

  四舍五入为最接近的整数:round()

  >>> round(2/3)
  1
  >>> round(3/2)
  2
  >>>

#2 形参、实参

  def mythirdfunc(name1,name2):
      print(name1+'我爱你')#函数定义过程中的参数为形参,函数调用过程中则为实参
      return (name1+name2)#返回值(没有返回值时候 则返回None)

#3 函数文档

  print(print.__doc__)#查看函数print的函数文档(此方法直接在shell中输入使用)
  help(print)#查看函数print的函数文档

#4 关键字参数:调用函数时给实参指定形参名称

  mythirdfunc(name1='欢欢',name2='我爱你')

#5 默认参数:定义函数时指定形参参数对应的默认值,若在调用时未给参数赋值则使用默认值

  def mythirdfunc(name1='欢欢',name2='我爱你'):
         return (name1+name2)#返回值

#6 收集参数:调用函数时先收集所有非关键字参数为该参数的实参(使用元组--以,作为标志)

  def myfifthfunc(*name,name1=2):
      #*name--收集参数:调用函数时先收集所有非关键字参数为该参数的实参
      print(name,name1)

#7 局部变量、全局变量

  函数内定义、修改--局部变量

  函数外定义、修改--全局变量

  函数内修改全局变量:global 变量名

  def mysixthfunc():
      print('调用函数6')
      def myseventhfunc():#内嵌函数--只在定义该函数的函数内有效(定义局部内有效)
          print('调用函数7')
      myseventhfunc()

#8 闭包:自由变量+函数--调用了一个函数A,这个函数A返回了一个函数B。这个返回的函数B就叫做闭包。调用函数A的时候传递的参数就是自由变量。

  def myeighthfunc(x):
      def myninethfunc(y):
          return x*y
      return myninethfunc

  def myeighthfunc():
      x=[1]
      def myninethfunc():
          x[0]*=x[0]
          return x[0]#列表不是存放在栈里面 不受全局变量影响
      return myninethfunc()

  def myeighthfunc():
      x=1
      def myninethfunc():
          nonlocal x#nonlcal使用方法类似global--将变量定义为非局部变量
          x*=x
          return x
      return myninethfunc()

#9 lambda、过滤器filter、映射map

  1)lambda函数:lambda 参数:返回值

  g=lambda x,y:x+y
  print(g(1,2))

  2)过滤器filter--只返回True结果

  help(filter)
  print(list(filter(None,[0,1,2,False,True])))
  print(list(filter(lambda x:x%2,range(0,10,1))))

  3)映射map--返回所有结果

  print(list(map(lambda x:x%2,range(0,10,1))))

#10 递归(分支思想)--函数调用自身+正确的函数终止条件

  1)迭代实现阶乘

  def myeleventhfunc(n):#迭代实现
      result = n
      for i in range(1,n,1):
          result *= i
      return result
  number = 5
  result = myeleventhfunc(number)
  print('%d 的阶乘是:%d'%(number,result))

  2)递归实现阶乘

  def mytwelvethfunc(n):#递归实现
      if n == 1:
          return 1
      else:
          return n*mytwelvethfunc(n-1)
  number = 6
  result = mytwelvethfunc(number)
  print('%d 的阶乘是:%d'%(number,result))

  3)迭代实现菲波那切数列

  def mythirteenthfunc(n):#迭代实现
      n1 = 1
      n2 = 1
      n3 = 1
      if n < 1:
          print('输入错误')
          return -1
      else:
          if n < 3:
              return 1
          else:
              for i in range(1,n-1,1):
                  n3 = n1+n2
                  n1 = n2
                  n2 = n3
              return n3
  n = 3
  result = mythirteenthfunc(n)
  print('菲波那切数列中第%d个数为%d'%(n,result))

  4)递归实现菲波那切数列

  def myfourteenthfunc(n):#递归实现--分支思想

      if n < 1:
          print('输入错误')
          return -1
      if n == 1 or n ==2:
          return 1
      else:
          return myfourteenthfunc(n-1)+myfourteenthfunc(n-2)
  n = 4
  result = myfourteenthfunc(n)
  print('菲波那切数列中第%d个数为%d'%(n,result))

  5)递归实现汉诺塔

  def myfifteenthfunc(n,x1,x2,x3):
      if n == 1:
          print(x1,'--',x3)
      else:
          myfifteenthfunc(n-1,x1,x3,x2)
          print(x1,'--',x3)
          myfifteenthfunc(n-1,x2,x1,x3)
  n = 5
  myfifteenthfunc(n,'a','b','c')

Python-9 函数的更多相关文章

  1. python的函数

    函数一词起源于数学,但是在编程中的函数和数学中的有很大不同.编程中的函数式组织好的,可重复使用的,用于实现单一功能或相关联功能的代码块. 我们在学习过程中已经使用过一些python内建的函数,如pri ...

  2. python strip()函数 介绍

    python strip()函数 介绍,需要的朋友可以参考一下   函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm)        删除s字符串中开头.结尾处,位于 rm删除 ...

  3. python split()函数

    Python split()函数 函数原型: split([char][, num])默认用空格分割,参数char为分割字符,num为分割次数,即分割成(num+1)个字符串 1.按某一个字符分割. ...

  4. Python数学函数

    1.Python数学函数 1.abs(x):取绝对值,内建函数 2.math.ceil(x):向上取整,在math模块中 3.cmp(x,y):如果 x < y ,返回-1:如果 x == y ...

  5. Python回调函数用法实例详解

    本文实例讲述了Python回调函数用法.分享给大家供大家参考.具体分析如下: 一.百度百科上对回调函数的解释: 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函 ...

  6. Python之函数与变量

    本节内容 函数介绍及其作用 函数的定义与调用 函数的参数说明 全局变量与局部变量 值传递和引用传递 一.函数的介绍及其作用 编程语言中的函数与数学中的函数是有区别的:数学中的函数有参数(输入),就会有 ...

  7. Python基础-函数篇

    本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数  函数与函数式编程 1.面向对象: 华山派-- ...

  8. 【C++实现python字符串函数库】strip、lstrip、rstrip方法

    [C++实现python字符串函数库]strip.lstrip.rstrip方法 这三个方法用于删除字符串首尾处指定的字符,默认删除空白符(包括'\n', '\r', '\t', ' '). s.st ...

  9. 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith

    [C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...

  10. 【C++实现python字符串函数库】一:分割函数:split、rsplit

    [C++实现python字符串函数库]split()与rsplit()方法 前言 本系列文章将介绍python提供的字符串函数,并尝试使用C++来实现这些函数.这些C++函数在这里做单独的分析,最后我 ...

随机推荐

  1. python 后台爆破工具(多线程)

    非阻塞 q.put(item) 写入队列,timeout等待时间 q.put_nowait(item) 相当q.put(item, False) threads多线程     首先导入threadin ...

  2. Angularjs scope

    $scope: var myapp = angular.module('myapp', []); myapp .controller('parent', function ($scope,$timeo ...

  3. angular2 笔记

    动态添加一个component: import { ViewContarinerRef, Component, ComponentFactoryResolver, ViewChild } from ' ...

  4. ionic2配置问题集

    我在配置ionic2中遇到的问题,及我的解决方案. ionic start MyIonic2Project tutorial --v2 网络很坑爹,配置lantern后,也是多次尝试才下载成功. io ...

  5. Geolocation API 原理及方法

    使用IP地址:基于Web的数据库:无线网络连接定位:三角测量:GPS技术:来测量经度和纬度.(综合了所有技术)地理定位的精确度,有很多方法可以定位用户的地理位置,并且每种方法都有不同的精度.桌面浏览器 ...

  6. python 实现文件下载

    Requests库,高度封装的http库 import requests url = 'http://down.sandai.net/thunder9/Thunder9.0.18.448.exe' f ...

  7. vm虚拟机安装雨林木风ghost镜像

    每次安装总是提示没办法加载镜像,或者镜像不存在,总之就是读取不到光驱里的镜像文件. 这是需要注意的两点:cd光驱模式设置为IDE,不能是scsi和sata两种模式,然后再进入winpe系统就行.

  8. 《C与指针》第十五章练习

    本章例程 15.1打开和关闭文件 #include <stdlib.h> #include <stdio.h> int main(int ac, char **av) { in ...

  9. tomcat building

    https://tomcat.apache.org/tomcat-7.0-doc/building.html https://tomcat.apache.org/tomcat-7.0-doc/BUIL ...

  10. SCRIPT7002: XMLHttpRequest: 网络错误 0x2ef3, 由于出现错误 00002ef3 而导致此项操作无法完成,浏览器中的Keep-Alive

    http://www.cnblogs.com/OpenCoder/p/5089258.html     IE中Keep-Alive机制引起的错误 我们知道Http协议是基于TCP/IP连接的,也就是说 ...