1. 函数定义与调用

def MyFirstFunction():
print('这是我创建的第一个函数')
#调用
MyFirstFunction()
这是我创建的第一个函数

2. 函数文档

def MySecondFunction(name):
#下面这个字符串即为函数文档
'函数定义过程中的name叫形参'
print('你的名字是' + name)
MySecondFunction('Nigream')
#两个双下划线表示系统属性
print(MySecondFunction.__doc__)
#也可以用
help(MySecondFunction)
你的名字是Nigream
函数定义过程中的name叫形参
Help on function MySecondFunction in module __main__: MySecondFunction(name)
函数定义过程中的name叫形参

3. 关键字参数

def SaySome(name , words):
print(name + '-->' + words)
SaySome(name='Nigream',words='hello')
Nigream-->hello

4. 默认参数

def SaySome(name = 'Nigream', words = 'hello'):
print(name + '-->' + words)
SaySome()
SaySome('苍井空')
SaySome('苍井空' , '我脱光衣服躺在镜头前,是为了生存;而你衣冠楚楚地站在镜头前,却是为了私欲和欺骗!')
Nigream-->hello
苍井空-->hello
苍井空-->我脱光衣服躺在镜头前,是为了生存;而你衣冠楚楚地站在镜头前,却是为了私欲和欺骗!

5. 收集参数

def test(*params):
print('参数的长度是:',len(params))
print('第三个参数是:',params[2]) test(1,2,3,4,'Nigream',5)
参数的长度是: 6
第三个参数是: 3

6. 返回值

def back():
return [1,'Nigream',3.14]
print(back())
#运行结果
[1, 'Nigream', 3.14]
def back1():
return 1,'Nigream',3.14
#这里被看成一个元组
print(back1())
[1, 'Nigream', 3.14]
(1, 'Nigream', 3.14)

7. 作用域

def discounts(price,rate):
final_price = price * rate
return final_price old_price = float(input('请输入原价:'))
rate = float(input('请输入折扣率:'))
new_price = discounts(old_price,rate)
print('打折后的价格是:',new_price)
请输入原价:100
请输入折扣率:0.8
打折后的价格是: 80.0
def discounts(price,rate):
final_price = price * rate
return final_price old_price = float(input('请输入原价:'))
rate = float(input('请输入折扣率:'))
new_price = discounts(old_price,rate)
print('打折后的价格是:',new_price)
print('这里试图打印局部变量final_price的值:',final_price)
请输入原价:100
请输入折扣率:0.8
打折后的价格是: 80.0
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-9-bd414db96855> in <module>()
7 new_price = discounts(old_price,rate)
8 print('打折后的价格是:',new_price)
----> 9 print('这里试图打印局部变量final_price的值:',final_price) NameError: name 'final_price' is not defined
def discounts(price,rate):
final_price = price * rate
print('这里试图打印全局变量old_price的值:',old_price)
return final_price old_price = float(input('请输入原价:'))
rate = float(input('请输入折扣率:'))
new_price = discounts(old_price,rate)
print('打折后的价格是:',new_price)
请输入原价:100
请输入折扣率:0.8
这里试图打印局部变量old_price的值: 100.0
打折后的价格是: 80.0
def discounts(price,rate):
final_price = price * rate
#在这里python会重新定义一个名字相同的局部变量
old_price = 50
print('这里试图打印局部变量old_price的1值:',old_price)
return final_price old_price = float(input('请输入原价:'))
rate = float(input('请输入折扣率:'))
new_price = discounts(old_price,rate)
print('这里试图打印全局变量old_price的2值:',old_price)
print('打折后的价格是:',new_price)
请输入原价:100
请输入折扣率:0.8
这里试图打印局部变量old_price的1值: 50
这里试图打印全局变量old_price的2值: 100.0
打折后的价格是: 80.0

8. global

count = 5
def MyFun():
count = 10
print(10)
MyFun()
print(count)
10
5
#要在函数内部修改count
count = 5
def MyFun():
global count
count = 10
print(10)
MyFun()
print(count)
10
10

9. 内嵌函数

def fun1():
print('fun1正在被调用')
def fun2():
print('fun2正在被调用')
fun2()
fun1()
fun1正在被调用
fun2正在被调用

10. 闭包

#如果在一个内部函数里,对在外部作用域(但不是在全局作用域)的变量
#进行引用,那么内部函数就被认为是闭包(closure)。
def FunX(x):
def FunY(y):
return x*y
return FunY
i = FunX(8)
print(type(i)) print(i(5))
print(FunX(8)(5))
<class 'function'>
40
40
def Fun1():
x = 5
def Fun2():
#这里由于外层作用域已经定义了x,
#所以此时系统会重新定义局部变量x,
#而又未次局部变量赋值,所以该x = x + 2会报错
x *= x
return x
return Fun2() Fun1()
---------------------------------------------------------------------------

UnboundLocalError                         Traceback (most recent call last)

<ipython-input-32-d753abbbddb3> in <module>()
9 return Fun2()
10
---> 11 Fun1() <ipython-input-32-d753abbbddb3> in Fun1()
7 x *= x
8 return x
----> 9 return Fun2()
10
11 Fun1() <ipython-input-32-d753abbbddb3> in Fun2()
5 #所以此时系统会重新定义局部变量x,
6 #而又未次局部变量赋值,所以该x = x + 2会报错
----> 7 x *= x
8 return x
9 return Fun2() UnboundLocalError: local variable 'x' referenced before assignment
#python2解决,利用列表等容器
def Fun1():
#将其定义为列表
x = [5]
def Fun2():
x[0] *= x[0]
return x[0]
return Fun2() Fun1()
25
#python3解决,利用关键字nonlocal
def Fun1():
x = 5
def Fun2():
nonlocal x
x *= x
return x
return Fun2() Fun1()
25

Python学习笔记——Python 函数的更多相关文章

  1. Python学习笔记之函数

    这篇文章介绍有关 Python 函数中一些常被大家忽略的知识点,帮助大家更全面的掌握 Python 中函数的使用技巧 1.函数文档 给函数添加注释,可以在 def 语句后面添加独立字符串,这样的注释被 ...

  2. Python学习笔记11—函数

    建立第一个函数 /usr/bin/env Python #coding:utf-8 def add_function(a,b): c = a+b print c if __name__==" ...

  3. 小甲鱼:Python学习笔记003_函数

    >>> # 函数>>> def myFirstFunction(params1,params2...): print("这是我的第一个函数!") ...

  4. Python学习笔记 - day6 - 函数

    函数 函数在编程语言中就是完成特定功能的一个词句组(代码块),这组语句可以作为一个单位使用,并且给它取一个名字.可以通过函数名在程序的不同地方多次执行(这叫函数的调用).函数在编程语言中有基本分为:预 ...

  5. Python学习笔记—Python基础1 介绍、发展史、安装、基本语法

    第一周学习笔记: 一.Python介绍      1.Python的创始人为吉多·范罗苏姆.1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言 ...

  6. Python学习笔记--Python字符串连接方法总结

    声明: 这些总结的学习笔记,一部分是自己在工作学习中总结,一部分是收集网络中的知识点总结而成的,但不到原文链接.如果有侵权,请知会,多谢. python中有很多字符串连接方式,总结一下: 1)最原始的 ...

  7. python学习笔记-python程序运行

    小白初学python,写下自己的一些想法.大神请忽略. 安装python编辑器,并配置环境(见http://www.cnblogs.com/lynn-li/p/5885001.html中 python ...

  8. Python学习笔记系列——函数

    今年下半年的计划主要是Python和Mysql了,公司不方便看书和视频,就照着廖雪峰的Python网站开始看了.以下纯为个人笔记记录,若是想系统学习的小伙伴还是看这里的好一些,毕竟系统.https:/ ...

  9. Python学习笔记(五)函数和代码复用

    函数能提高应用的模块性,和代码的重复利用率.在很多高级语言中,都可以使用函数实现多种功能.在之前的学习中,相信你已经知道Python提供了许多内建函数,比如print().同样,你也可以自己创建函数, ...

  10. python学习笔记(4)--函数

    1.函数 函数是指将一组语句的集合通过一个名字封装起来.要想执行这个函数,只需调用其函数名即可. 函数的特性: 1.减少重复代码 2.使程序变的课扩展 3.使程序变得易维护 语法定义: def pri ...

随机推荐

  1. StrictMode 详解

    StrictMode类是Android 2.3 (API 9)引入的一个工具类,可以用来帮助开发者发现代码中的一些不规范的问题.比如,如果你在UI线程中进行了网络或者磁盘操作,StrictMode就会 ...

  2. MySQL_JDBC_jar包的下载与使用(Windows)

    1. 下载 (1) 打开MySQL_JDBC的下载网站:https://dev.mysql.com/downloads/connector/j/ (2) 选择操作系统:Platform Indepen ...

  3. 粒子群优化算法及其java实现

    憋了两周终于把开题报告憋出来了,再一次证明自己不适合搞学术,哎--,花了点时间把报告中提到的粒子群算法看了看,看了些资料,用java跑起来. 算法简介 粒子群算法最先由Barnhart博士和Kenne ...

  4. ngx.shared.DICT.incr 详解

    ngx.shared.DICT.incr 原文: ngx.shared.DICT.incr syntax: newval, err, forcible? = ngx.shared.DICT:incr( ...

  5. Linux中强大的top命令

    top命令算是最直观.好用的查看服务器负载的命令了.它实时动态刷新显示服务器状态信息,且可以通过交互式命令自定义显示内容,非常强大. 在终端中输入top,回车后会显示如下内容:   top - 21: ...

  6. Oracle scope中 spfile、memory、both 的区别

    Oracle里面有个叫做spfile的东西,就是动态参数文件,里面设置了Oracle 的各种参数. 所谓的动态,就是说你可以在不关闭数据库的情况下,更改数据库参数,记录在spfile里面. 更改参数的 ...

  7. Android高频单词

    Display 显示 Camera 照相机 Bluetooth 蓝牙 Flash Memory 闪存 Audio 音频 Management 管理 SurFace 界面 Media 多媒体 Frame ...

  8. ps 快捷键大全

    一.工具箱(多种工具共用一个快捷键的可同时按[Shift]加此快捷键选取)矩形.椭圆选框工具 [M]移动工具 [V]套索.多边形套索.磁性套索 [L]魔棒工具 [W]裁剪工具 [C]切片工具.切片选择 ...

  9. 阶段5 3.微服务项目【学成在线】_day18 用户授权_01-用户授权业务流程分析

    1 用户授权业务流程 用户授权的业务流程如下: 业务流程说明如下: 1.用户认证通过,认证服务向浏览器cookie写入token( 身份令牌) 2.前端携带token请求用户中心服务获取jwt令牌 前 ...

  10. 【416】Ubuntu 配置

    修改桌面主题:How to Install Desktop Themes on Ubuntu 18.04 LTS 命令行显示短路径:怎样ubuntu下命令行终端显示短路径 gedit 扩展插件:Use ...