python function parameter

本文通过一系列示例详细介绍了在Python中如何定义和使用函数,包括基本语法、参数传递、返回值处理及可变参数等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python 2.7.10 (default, Oct 14 2015, 16:09:02) 
[GCC 5.2.1 20151010] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> def function():定义函数
    ptintf("run")

    
>>> function()

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    function()
  File "<pyshell#2>", line 2, in function
    ptintf("run")
NameError: global name 'ptintf' is not defined
>>> def fun():
    print ("hello")

    
>>> fun()
hello
>>> def fun2(name):
    print(name)

    
>>> fun2(new)

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    fun2(new)
NameError: name 'new' is not defined
>>> fun2("new")
new
>>> 
>>> 
>>> def fun3(num1,num2):
    return num1+num2函数的返回值

>>> print (fun3(1,2))
3
>>> def fun4(name)
SyntaxError: invalid syntax
>>> def fun4(name):
    'name is a param'
    print(name)

    
>>> fun4("hello world")
hello world
>>> fun4._doc_函数的注释文档

Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    fun4._doc_
AttributeError: 'function' object has no attribute '_doc_'
>>> help(fun4)
Help on function fun4 in module __main__:

fun4(name)
    name is a param

>>> def fun5(*param)
SyntaxError: invalid syntax
>>> def fun5(*param):
    print ("%d",len(param))
    print (param)

    
>>> fun5(1,2,3,4)
('%d', 4)
(1, 2, 3, 4)
>>> def fun5(*param,par):
    print ("%d",len(param))
    print (param)
    
SyntaxError: invalid syntax
>>> 
>>> def fun6(*param,par):函数的收集参数
    print ("%d",len(param))
    print (param)
    
SyntaxError: invalid syntax
>>> def fun7(*param,name):
    
SyntaxError: invalid syntax
>>> def fun8(*pa , name1):
    
SyntaxError: invalid syntax
>>> 

Python 2.7.10 (default, Oct 14 2015, 16:09:02) [GCC 5.2.1 20151010] on linux2Type "copyright", "credits" or "license()" for more information.>>> def function():ptintf("run")
>>> function()
Traceback (most recent call last):  File "<pyshell#3>", line 1, in <module>    function()  File "<pyshell#2>", line 2, in function    ptintf("run")NameError: global name 'ptintf' is not defined>>> def fun():print ("hello")
>>> fun()hello>>> def fun2(name):print(name)
>>> fun2(new)
Traceback (most recent call last):  File "<pyshell#11>", line 1, in <module>    fun2(new)NameError: name 'new' is not defined>>> fun2("new")new>>> >>> >>> def fun3(num1,num2):return num1+num2
>>> print (fun3(1,2))3>>> def fun4(name)SyntaxError: invalid syntax>>> def fun4(name):'name is a param'print(name)
>>> fun4("hello world")hello world>>> fun4._doc_
Traceback (most recent call last):  File "<pyshell#25>", line 1, in <module>    fun4._doc_AttributeError: 'function' object has no attribute '_doc_'>>> help(fun4)Help on function fun4 in module __main__:
fun4(name)    name is a param
>>> def fun5(*param)SyntaxError: invalid syntax>>> def fun5(*param):print ("%d",len(param))print (param)
>>> fun5(1,2,3,4)('%d', 4)(1, 2, 3, 4)>>> def fun5(*param,par):print ("%d",len(param))print (param)SyntaxError: invalid syntax>>> >>> def fun6(*param,par):print ("%d",len(param))print (param)SyntaxError: invalid syntax>>> def fun7(*param,name):SyntaxError: invalid syntax>>> def fun8(*pa , name1):SyntaxError: invalid syntax>>> 

转载于:https://www.cnblogs.com/13224ACMer/p/6045471.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值