python基础之函数

简介: 一、函数定义,参数,以及返回值def 函数名():函数体返回值:通过return函数>>> def fun():>>> def add(a,b): print(a+b) >>> add(34,78)...

一、函数定义,参数,以及返回值

def 函数名():

函数体

返回值:通过return函数

>>> def fun():
>>> def add(a,b):
	print(a+b)

	
>>> add(34,78)
112
>>> def add1(a,b):
	return a*b

>>> add1(12,5)
60
>>> 


print('第一个函数')>>> fun()第一个函数>>> def fun(name):print("第二个函数"+name)>>> fun(age)Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> fun(age)NameError: name 'age' is not defined>>> fun('age')第二个函数age
 
 

 
 二、函数文档 
 

>>> def fun(name):
	'函数定义过程的name是形参!'
	#因为表示一个形式,标志占据一个位置
	print('传递参数'+name+'是实参,有具体参数值')

	
>>> fun('xiao')
传递参数xiao是实参,有具体参数值
>>> fun._doc_
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    fun._doc_

>>> help(fun)
Help on function fun in module __main__:

fun(name)
    函数定义过程的name是形参!


关键参数

>>> fun(name='关键参数')
传递参数关键参数是实参,有具体参数值
>>> 

收集参数

参数前加*

>>> def test(*param):
	print("参数的长度是:",len(param))
	print("第一个参数是:",param[1])

	
>>> test(1,2,3,4)
参数的长度是: 4
第一个参数是: 2
>>> 

>>> def test(*param):
	print("参数的长度是:",len(param))
	print("第一个参数是:",param[1])

	
>>> test(1,2,3,4)
参数的长度是: 4
第一个参数是: 2
>>> def test1(*param,exp)
SyntaxError: invalid syntax
>>> def test1(*param,exp):
	print("参数的长度是:",len(param))
	print("第一个参数是:",param[1])
	print('exp是:',exp)

	
>>> test1(1,2.3,4,exp="普通参数")
参数的长度是: 3
第一个参数是: 2.3
exp是: 普通参数
>>> 

函数与过程:

函数有返回值,python中只有函数、、


全局变量和局部变量:

def discount(price,rate):
    final_price=price*rate
    return final_price
old_price=float(input("请输入原价:"))
rate=float(input("请输入折扣率:"))
new_price=discount(old_price,rate)
print("打折后的价格:",new_price)

内嵌函数和闭包

>>> count=5	#全局变量
>>> def myfun():
	count=10	#局部变量
	print(10)

	
>>> myfun()
10
>>> print()count
SyntaxError: invalid syntax
>>> print(count)
5
>>> 

全局变量用global申明:

>>> def myfun():
	global count
	count=10
	print(count)


	
>>> nyfin()
Traceback (most recent call last):
  File "<pyshell#63>", line 1, in <module>
    nyfin()
NameError: name 'nyfin' is not defined
>>> myfun()
10
>>> print(count)
10
>>> count=1
>>> print(count)
1
>>> myfun(count)
Traceback (most recent call last):
  File "<pyshell#69>", line 1, in <module>
    myfun(count)
TypeError: myfun() takes 0 positional arguments but 1 was given
>>> myfun()
10
>>> 


相关文章
|
12天前
|
Python
python函数的参数学习
学习Python函数参数涉及五个方面:1) 位置参数按顺序传递,如`func(1, 2, 3)`;2) 关键字参数通过名称传值,如`func(a=1, b=2, c=3)`;3) 默认参数设定默认值,如`func(a, b, c=0)`;4) 可变参数用*和**接收任意数量的位置和关键字参数,如`func(1, 2, 3, a=4, b=5, c=6)`;5) 参数组合结合不同类型的参数,如`func(1, 2, 3, a=4, b=5, c=6)`。
14 1
|
27天前
|
Python
Python函数使用(四)
Python函数使用(四)
60 0
|
5天前
|
Serverless 开发者 Python
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
37 1
|
5天前
|
索引 Python
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
|
7天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
47 0
|
7天前
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
|
8天前
|
Python
python学习10-函数
python学习10-函数
|
8天前
|
Python
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
|
11天前
|
测试技术 开发者 Python
Python中的装饰器:优雅而强大的函数修饰工具
在Python编程中,装饰器是一种强大的工具,用于修改函数或方法的行为。本文将深入探讨Python中装饰器的概念、用法和实际应用,以及如何利用装饰器实现代码的优雅和高效。
|
16天前
|
Python
Python函数学习应用案例详解
【4月更文挑战第7天】学习Python函数的应用,包括计算两数之和、判断偶数、计算阶乘、生成斐波那契数列及反转字符串。示例代码展示了函数接收参数和返回结果的功能,如`add(a, b)`求和,`is_even(num)`判断偶数,`factorial(n)`计算阶乘,`fibonacci(n)`生成斐波那契数,以及`reverse_string(s)`反转字符串。
14 1