python的code函数 您所在的位置:网站首页 python的code对象 python的code函数

python的code函数

2023-08-20 12:21| 来源: 网络整理| 查看: 265

#python中函数的定义:函数是逻辑结构化和过程化的一种编程方法,用来实现某些特定的功能.

#python中函数简单定义的方法

# def test(x):

# "this is new function"

# x+=1

# return x

#def:定义函数的关键字

#test函数名

#():括号内可定义形参

#""文档描述(非必要,建议加上)

#x+=1泛指代码块

#return 定义返回值

#调用运行:可以带参数也可以不带,函数名()

#print(test(1))

#过程定义:就是简单特殊没有返回值的函数

# def test02():

# msg="test02"

# print(msg)

# return 1,2,3,4,"a",[1,2],{"name":"tang"},(5,6,7)

#

# dom=test02()

# print(dom[2])

#总结:

#返回值=0 返回None

#返回值=1 返回object

#返回值>1 返回tuple

#函数参数

# def calc(x,y):#形参函数调用是赋值,函数结束就释放内存

# # z=x**y

# # return z

# #

# #

# # calc(2,3)#实参

#位置参数,必须一一对应,关键字参数:位置无序固定,有多个形参,就必须有多少个实参

def rest(x,y,z):

print(x)

print(y)

print(z)

# rest(1,2,3)

#

# rest(y=2,x=3,z=1)

#位置参数必须在关键字参数左边

#rest(1,2,z=3)

# def handle(x,type=None):

# print(x)

# print(type)

# handle("hello",type="select")#关键字参数

# handle("hello","sqlite")#位置参数

#参数组: **字典 *元祖

def test(x,*args):#多的实参会被当做元祖放入*args

print(x)

print(args)

#print(args[0])

#test(1,2,3,4,5,6)

#test(1,*{"name":"alex",'hh':"dhhd"},{'aa':'wss'})

# test(1,*['x','y','z'])

# test(1,*("x","y","z"))

#test(1)

# test(1,{"name":"alex"})

# def test(x,**kwargs):

# print(x)

# print(kwargs)

# test(x=1,y=2,j=3)#会报错:一个参数不能传两个值

def test(x,*args,**kwargs):

print(x,args,kwargs)

test(1,*[1,2,3],**{'q':1})



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有