python fun函数的功能是_Python基金会——四种功能,python,基础,函数,的,类型

1.第一种没有参数,没有返回值

def go():

print("hello fun")

for i in range(10):

go()

运行一下:

D:\untitled\venv\Scripts\python.exe D:/untitled/6.py

hello fun

hello fun

hello fun

hello fun

hello fun

hello fun

hello fun

hello fun

hello fun

hello fun

Process finished with exit code 0

定义一个函数,通过调用这个函数,打印循环打印10次

2.第二种有参数,没有返回值

def goprint(num):

print("python",num)

for i in range(10):

goprint(i)

运行一下:

D:\untitled\venv\Scripts\python.exe D:/untitled/6.py

python 0

python 1

python 2

python 3

python 4

python 5

python 6

python 7

python 8

python 9

Process finished with exit code 0

3.第三种没有参数,有返回值

def getdata():

return random.randint(0,100)

这是一个获取随机数的方法,函数中没有参数,但是又返回值,用return返回0-100之间的随机数

运行一下:

hello fun

python 0

36

hello fun

python 1

93

hello fun

python 2

3

hello fun

python 3

35

hello fun

python 4

24

hello fun

python 5

38

hello fun

python 6

20

hello fun

python 7

4

hello fun

python 8

45

hello fun

python 9

33

4.第四种既有参数,又有返回值

def add(num1,num2):

return num1+num2

res=add(100,260)

print(res)

运行一下

360

这种是既有输入又有输出,是一种最常见的函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值