python函数编程

python函数编程

在这里插入图片描述
如果觉得写得好,欢迎给位关注公众号呀,直接搜索 做个记事本 就好了。
接下来就是今天分享的python函数

python 函数的定义和调用
python 函数的参数
python 函数的划分

	python函数定义
		函数(function):是一个可以被重复调用的带有一个入口和一个出口的固定的程序段
		函数比较代码复制
		1、减少冗余代码
		2、代码结构清晰
		3、有助于保持代码的一致性

		function
		python:
		1、关键字:def
		2、函数名:
			命名规范:
				1、字母开头
				2、不允许有关键字
				3、不允许有特殊符号
				4、不允许莫名其妙的函数名a,b,
		3、参数:参数式定义在括号里 的,由调用时传入,作用在函数内部的变量
			1、如果有参数,写在参照括号里
			2、如果没有,写空括号
			3、缩进语句块
def say_hello():
    name = str(input('请输入你的名字:'))
    print('hello' + name.title())
say_hello()

python函数的调用
	函数没有调用之前不执行
	函数名加括号,并且进行对应的传参形式

def say_hello(name):
    # name = str(input('请输入你的名字:'))		
    #表示注释的,注释之后语句时不执行的
    print('hello' + name.title())
say_hello('shuai')

helloShuai



函数的参数
	按照定义和传递我们将参数划分为形参和实参
	在定义函数时定义的参数我们称之为 形参
	在调用函数时我们传递是参数称之为 实参


def say_hello(name):
    # name = str(input('请输入你的名字:'))		
    #表示注释的,注释之后语句时不执行的
    print('hello' + name.title())
say_hello('shuai')

name 是形参
shuai 是实参


在使用的过程中划分了五种参数
在python2中式四种
	位置参数
	关键字参数
	默认参数
	参数组

	python3中新加入	命名关键字参数

1、位置参数
		是在我们传参的时候,实参传递的顺序按照形参定义的顺序进行传递的参数
			def say_hello(name,age):
			    print('hello %s is %d years old'%(name,age))
			say_hello('pei',18)
			say_hello(19,’noshuai‘)
			say_hello('shuai',10,19)

			hello pei is 18 years old
			Traceback (most recent call last):
			  File "E:/pycharm/python_test2.py", line 11, in <module>
			    say_hello(19,'noshuai')
			  File "E:/pycharm/python_test2.py", line 9, in say_hello
			    print('hello %s is %d years old'%(name,age))
			TypeError: %d format: a number is required, not str

			Traceback (most recent call last):
			  File "E:/pycharm/python_test2.py", line 12, in <module>
			    say_hello('shuai',10,19)
			hello pei is 18 years old
			TypeError: say_hello() takes 2 positional arguments but 3 were given

			Process finished with exit code 1

	直接按照形参定义的位置,依次传入实参,有多少传多少


2、关键字参数
	实在我们传参的时候,以形参等于实参的形式忽略形参定义的顺序进行传参的传参的方式
			def say_hello(name,age):
			    print('hello %s is %d years old'%(name,age))
			say_hello(age = 19,name = "shuai")


			hello shuai is 19 years old

			Process finished with exit code 0
	注意在进行参数传递的时候,形参不要写错了

3、默认参数
	是在我们定义参数的时候,我们给参数的一个默认值,在峨嵋调用函数的时候,如果不给有默认参数的形参,传参,会自动采用默认值
			def say_hello(name,age=18):
			    print('hello %s is %d years old'%(name,age))
			say_hello('shuai')
			say_hello('shuai',19)
			say_hello(10,'shuai')	默认值参数必须写在,正常参数后面
			

			hello shuai is 18 years old
			Traceback (most recent call last):
			hello shuai is 19 years old
			  File "E:/pycharm/python_test2.py", line 21, in <module>
			    say_hello(10,'shuai')
			  File "E:/pycharm/python_test2.py", line 18, in say_hello
			    print('hello %s is %d years old'%(name,age))
			TypeError: %d format: a number is required, not str

			Process finished with exit code 1
注意:在定义函数的时候,默认值的形参也必须写在,正常参数的后面

4、参数组
	指我们将参数变成数组或者字典,忽略参数个数进行传参
		元组数组
			通过给形参前面加一个*是参数变成一个元组,所有传递的参数变成元组的元素

				def say_hello(*names):
				    print(names)
				say_hello('shuai','ge','pei')
				say_hello(*range(100))


				('shuai', 'ge', 'pei')
				(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 。。。。。。。。。。。)

				Process finished with exit code 0


字典参数
	通过给形参前面加**是参数变成一个字典,所有传递的参数变成字典的键值对,这里传参要求键等于值的形式。

				def say_hello(**names):
				    print(names)
				say_hello()
				say_hello(name='shuai')
				say_hello(**{'age':18})

				{}
				{'name': 'shuai'}
				{'age': 18}

				Process finished with exit code 0


5、命名关键字参数
	在定义形参之前加入独立的一个*,要求传递参数的时候必须以正确的形参等于实参的形式进行传参

				def say_hello(*,name,age):
				    print('hello %s is %d ysers old'%(name,age))
				say_hello(name = 'shuai',age=(100))
				say_hello('shuai',19)

				Traceback (most recent call last):
				hello shuai is 100 ysers old
				  File "E:/pycharm/python_test2.py", line 40, in <module>
				    say_hello('shuai',19)
				TypeError: say_hello() takes 0 positional arguments but 2 were given

				Process finished with exit code 1
	使用关键字命名的时候一定要注意,形参与实参对应


python函数划分
	按照定义划分
		传统函数
		匿名函数

	匿名函数
				a = lambda x,y:print(x+y)
				a(5,6)

				11

				Process finished with exit code 0

按照返回值划分
	返回形函数   有返回值
	计算型函数   没有返回值
					def num():
					    print('this is num')
					    print(1)

					this is num_1
					1

					Process finished with exit code 0


					def num_1():
					    print('this is num_1')
					    return 1
					num_1()

					this is num_1

					Process finished with exit code 0

return将函数中的值返回出来

					def num_2():
					    return 1
					print(num_2()+3)


					4

					Process finished with exit cod

当return结束时,函数结束,函数当中return下面的语句不会执行

				def num_3():
				    return 2
				    print(2)
				num_3()



				Process finished with exit code 0

return 只能返回一个值
				def num_4():
				    return 1,2,3
				print(num_4())

				(1, 2, 3)

				Process finished with exit code 0

有多个return时,也只返回一个return

				def num_4():
				    return 1,2,3
				    return 1
				print(num_4())

				(1, 2, 3)

				Process finished with exit code 0

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值