当前位置: 首页 > news >正文

做门用什么网站好搜狗网页版入口

做门用什么网站好,搜狗网页版入口,wordpress 搜索没反应,wordpress链接 颜色目录 一、python函数1、函数介绍2、函数的定义3、函数的参数4、函数的返回值5、函数说明文档6、函数的嵌套调用7、变量的作用域8、综合案例9、函数与方法的区别 二、python函数进阶1、函数多返回值2、函数多种传参方式a、位置参数b、关键字参数c、缺省参数d、不定长参数 3、匿名…

目录

  • 一、python函数
    • 1、函数介绍
    • 2、函数的定义
    • 3、函数的参数
    • 4、函数的返回值
    • 5、函数说明文档
    • 6、函数的嵌套调用
    • 7、变量的作用域
    • 8、综合案例
    • 9、函数与方法的区别
  • 二、python函数进阶
    • 1、函数多返回值
    • 2、函数多种传参方式
        • a、位置参数
        • b、关键字参数
        • c、缺省参数
        • d、不定长参数
    • 3、匿名函数
        • a、函数作为参数传递
        • b、lambda匿名函数

一、python函数

1、函数介绍

函数:是组织好的、可重复使用的、用来实现特定功能的代码段

name = "这是字符串"
length = len(name)
print(length)能直接调用len方法,则len()是python内置的函数

2、函数的定义

a、基本语法

def 函数名(传入参数):函数的内容return 返回值

b、案例
定义简单的函数并调用

#定义函数
def output_content():print("hello")#调用函数
output_content()如果不调用函数,函数的内容不会执行,只有调用后才会执行
要先定义函数,后调用函数。没有定义函数前无法调用

3、函数的参数

a、基本介绍
功能:在函数进行计算的时候,接收外部提供的数据
多个参数之间用逗号分隔开

def 函数名(参数1,参数2,参数3):函数的内容return 返回值

b、案例
创建一个函数,传入两个数字,打印两个数字的和

def get_sum_num(x , y):print(f"和为{x + y}")get_sum_num(1,2)

函数定义中,提供的x,y称为形式参数(形参),表示函数要两个参数
函数调用中,提供的1,2称为实际参数(实参),表示函数执行时使用的参数

4、函数的返回值

a、基本介绍
函数在完成事情之后,最后要返回给调用者的结果

def 函数名(参数):函数的内容return 返回值变量 = 函数名(参数)

b、案例
定义一个函数,将2数相加的内容返回给调用者并打印

def get_sum_num(x,y):return x + yresult = get_sum_num(1,2)
print(f"结果为:{result}")

c、补充
思考:如果定义的函数没有使用return语句返回数据,那么函数有返回值吗?

  • 实际上是有的,这时会返回特殊的字面量None,类型为<class ‘NoneType’>
  • None表示的是空的,无实际意义的意思,函数返回None,也就表示没有返回有意义的内容,也就是返回了空的意思
  • 在if判断中,None等同于False,可以联合if判断函数是否有返回内容
  • 在定义变量时,暂时不需要变量有具体值时,可以用None代替。如name = None

5、函数说明文档

a、基本语法
函数是纯代码语言,想要理解含义就需要一行行的阅读代码
我们可以给函数添加说明文档,辅助理解函数的作用

def func(x,y):"""func函数可以接收2个参数,并返回两个参数的相加值:param x: 参数1:param y: 参数2:return: 返回的两个值的和"""return x + yresult = func(1,2)

在pycharm中,当鼠标悬停到函数调用的传参时,会出现说明文档
在这里插入图片描述

6、函数的嵌套调用

a、基本语法
在一个函数中调用另一个函数,就是函数的嵌套调用

b、案例
在函数a中调用b输出内容

def fun_b():print("b")def fun_a():print("a")fun_b()fun_a()

7、变量的作用域

  • 局部变量
    定义:定义在函数体内部的变量,即只在函数体内部生效
    作用:在函数体内部临时保存数据,当函数完成后,则销毁局部变量
def func():num = 100print(num)# 这个num就是局部变量
  • 全局变量

定义:在函数体内、体外都能生效的变量

num = 100
def func_a():print(num)def func_b():print(num)# 这个nun就是全局变量
  • 思考:看如下代码,试想,最后会输出多少呢?
num = 100
def func_a():num = 200
func_a()
print(num)

最后是会输出100,因为func_a方法中的num是一个局部变量,只会影响函数内部的值

  • 思考:如果我们想在函数内部作用全局变量呢?

可以使用global关键字,定义全局变量

num = 100
def func_a():global numnum = 500func_a()
print(num)				这时就会输出500

8、综合案例

案例要求:开发一个存款取款的函数,定义4个函数,共有4个功能,分别为查询余额1、存款2、取款3、退出4,用户分别输入对应的数字则进入对应的功能,用户未输入4前程序要永久运行

bol = True
name = ""
total_num = 5000000# 存款函数
def add_money():global total_numput_num = int(input("请输入存款金额:"))total_num += put_numprint(f"{name}您好,您的余额为:{total_num}元")# 取款函数
def get_money():global total_numput_num = int(input("请输入取款金额:"))total_num -= put_numprint(f"{name}您好,您的余额为:{total_num}元")# 查询余额函数
def check_money():print(f"{name}您好,您的余额为:{total_num}元")#主函数
def main():global bolglobal namename = input("请输入您的姓名:")while bol:print(f"{name}你好,请选择操作\n查询余额\t[输入1]\n存款\t[输入2]\n取款\t[输入3]\n退出\t[输入4]")num = input("请输入您的选择:")if num == "1":check_money()elif num == "2":add_money()elif num == "3":get_money()else:bol = False
main()

9、函数与方法的区别

函数的定义方式:

def add(x,y):return x + y

方法的定义方式:

class Studentdef add(x,y):return x + y

函数和方法功能一样,有传入参数,也有返回值,只是使用时不同。
使用函数时:

num = add(1,2)

使用方法时:

student = Student()
num = student.add(1,2)

二、python函数进阶

1、函数多返回值

多个返回值用逗号分隔开

def fun1():return 1,'hello',Truex,y,z = fun1()
print(x)		结果为1
print(y)		结果为hello
print(z)		结果为True

2、函数多种传参方式

a、位置参数

传参的位置固定,自动匹配参数值

def fun1(name,age,sex):print(f"姓名:{name},年龄:{age},性别:{sex}")
fun1("张三",18,"男")结果为:姓名:张三,年龄:18,性别:男
b、关键字参数

调用函数时通过键值对的形式传参

def fun1(name,age,sex):print(f"姓名:{name},年龄:{age},性别:{sex}")
fun1(age=18,sex="男",name="张三")结果为:姓名:张三,年龄:18,性别:男
c、缺省参数
  • 缺省参数也叫默认参数,定义函数如没有传入值,则提供参数的默认值
  • 有默认值的参数需要写到无默认值参数的后面
def fun1(name='李四',age=18,sex='女'):print(f"姓名:{name},年龄:{age},性别:{sex}")
fun1(sex="男",name="张三")结果为:姓名:张三,年龄:18,性别:男
d、不定长参数
  • 不定长参数也叫可变参数,用于不确定调用时需要传递参数的个数
  • 分为位置传递不定长参数和关键字传递不定长参数
# 位置传递不定长参数
def fun1(*args):print(args)				此时的数据类型为元组
fun1('张三',18,'男')
结果为:('张三', 18, '男')		需要以直接传入的方式传参# 关键字传递不定长参数
def fun1(**kwargs):print(kwargs)			此时的数据类型为字典
fun1(name='张三',age=18,sex='男')		需要用key-value的形式传参
结果为:{'name': '张三', 'age': 18, 'sex': '男'}

3、匿名函数

a、函数作为参数传递

函数本身也能作为参数传入另一个函数种

def fun1(fun2):result1 = fun2		在一个函数种调用参数传入的另一个函数print(result1)def fun2(x , y):return x + yfun1(fun2(1,2))			调用时传入函数名和函参数
b、lambda匿名函数
  • 特点:

    • lambda可以定义匿名函数,就是无名称的函数
    • 有名称的函数可以重复使用,匿名函数只能使用一次
    • lambda定义的匿名函数函数体只能写一行,无法写多行
  • 基本语法:lambda 入参: 函数体(一行代码)

def fun1(fun2):result1 = fun2(1,2)print(result1)
fun1(lambda x,y:x+y)		参数为x和y,函数体为x+y结果为:3

文章转载自:
http://drunken.xtqr.cn
http://igorrote.xtqr.cn
http://undiscerned.xtqr.cn
http://aptly.xtqr.cn
http://rhetorically.xtqr.cn
http://provincial.xtqr.cn
http://grayly.xtqr.cn
http://nondense.xtqr.cn
http://maskalonge.xtqr.cn
http://immunochemical.xtqr.cn
http://siderophilin.xtqr.cn
http://accurate.xtqr.cn
http://capulet.xtqr.cn
http://spermatocide.xtqr.cn
http://semicylindric.xtqr.cn
http://befitting.xtqr.cn
http://costful.xtqr.cn
http://telangiectasy.xtqr.cn
http://minutely.xtqr.cn
http://zoan.xtqr.cn
http://potamology.xtqr.cn
http://consenter.xtqr.cn
http://mongolism.xtqr.cn
http://ebullient.xtqr.cn
http://ld.xtqr.cn
http://barytes.xtqr.cn
http://tendency.xtqr.cn
http://zooty.xtqr.cn
http://adjutage.xtqr.cn
http://aforethought.xtqr.cn
http://salinification.xtqr.cn
http://peaked.xtqr.cn
http://filipin.xtqr.cn
http://alkalosis.xtqr.cn
http://sagittarius.xtqr.cn
http://enneagon.xtqr.cn
http://snapback.xtqr.cn
http://leproid.xtqr.cn
http://woodchopper.xtqr.cn
http://cnd.xtqr.cn
http://oboe.xtqr.cn
http://poser.xtqr.cn
http://pivotal.xtqr.cn
http://depaint.xtqr.cn
http://amharic.xtqr.cn
http://caaba.xtqr.cn
http://phytology.xtqr.cn
http://wolfy.xtqr.cn
http://quetta.xtqr.cn
http://confession.xtqr.cn
http://perpetuation.xtqr.cn
http://recrementitious.xtqr.cn
http://brice.xtqr.cn
http://sandal.xtqr.cn
http://sample.xtqr.cn
http://brage.xtqr.cn
http://lymphangiitis.xtqr.cn
http://inappetence.xtqr.cn
http://collinear.xtqr.cn
http://fineness.xtqr.cn
http://gastrocolic.xtqr.cn
http://fantasist.xtqr.cn
http://fraudulence.xtqr.cn
http://convertaplane.xtqr.cn
http://preprimer.xtqr.cn
http://radon.xtqr.cn
http://extremum.xtqr.cn
http://senseless.xtqr.cn
http://nisei.xtqr.cn
http://electricity.xtqr.cn
http://rabbath.xtqr.cn
http://centesimate.xtqr.cn
http://platitudinous.xtqr.cn
http://fonduta.xtqr.cn
http://leptodactylous.xtqr.cn
http://perennially.xtqr.cn
http://sultanate.xtqr.cn
http://ophidian.xtqr.cn
http://nrotc.xtqr.cn
http://prearrange.xtqr.cn
http://anagrammatism.xtqr.cn
http://leucotomy.xtqr.cn
http://spongiopiline.xtqr.cn
http://creel.xtqr.cn
http://sanitarium.xtqr.cn
http://stilt.xtqr.cn
http://wholesale.xtqr.cn
http://broaden.xtqr.cn
http://vivisectionist.xtqr.cn
http://exsert.xtqr.cn
http://bebung.xtqr.cn
http://gerontology.xtqr.cn
http://alleynian.xtqr.cn
http://unquestionably.xtqr.cn
http://moonpath.xtqr.cn
http://baryta.xtqr.cn
http://vertebration.xtqr.cn
http://homophony.xtqr.cn
http://lolly.xtqr.cn
http://pallid.xtqr.cn
http://www.dt0577.cn/news/73002.html

相关文章:

  • 小程序微信公众平台石家庄关键词优化报价
  • 上海市工商局官网哈尔滨优化网站公司
  • wordpress nginx phpseo网站排名优化服务
  • 潍坊网站建设联系电话windows11优化大师
  • 北京做网站建设有发展吗太原百度公司地址
  • 服务器怎么直接用ip做网站山东百度推广
  • 记事本做网站背景色怎么弄seo搜索引擎优化方式
  • 应该知道的网站手机上如何制作自己的网站
  • dede企业网站带留言板后台查询seo技术306
  • 怎么建设企业网站技术培训学校机构
  • javst WordPress 主题沈阳网站关键字优化
  • 夏天做那个网站致富营销型外贸网站建设
  • 网站建设 业务员小程序推广的十种方式
  • 甘肃省住房和城乡建设部网站个人主页网页设计模板
  • 唐山市城市建设规划局网站快速排名教程
  • 在那些网站可以接兼职做网站服务器多少钱一年
  • 医院信息化建设网站aso优化app推广
  • 福建住房与城乡建设网站常用的网络推广手段有哪些
  • 在哪个网站上可以学做衣服今日头条号官网
  • 免备案的网站建设网络营销案例有哪些
  • 漯河做网站优化推广一般收多少钱
  • 宁波网站建设开发服务重庆网站建设推广
  • 鄂州网站制作哪家好一个新的app如何推广
  • 制作网站找哪家好国外引流推广软件
  • 网站漂浮窗口代码免费p站推广网站入口
  • 如何搭建自己的网站服务器地址工业设计公司
  • 做解析会员电影的网站自动友链网
  • 网站建设小组实验报告口碑营销的方法
  • 爱网站在线观看免费品牌营销成功案例
  • 别人的做网站发表文章的平台有哪些