python四叶玫瑰数_Python Day21 函数2

1、参数分类

1.1、形式参数(parameter)

函数在创建过程中'()'里的内容:def MyOneFunction(name):

1.2、实际参数(argument)

函数在使用过程中'()'里传递进去的内容:MyOneFunction('Cyrus')

1.3、显示函数文档的方法

>>> MyFunction1.__doc__

'这里是函数文档\n\t函数定义过程中name是叫形参,当函数执行时传递进来的参数叫实参'

>>> MyFunction1.__doc__

'这里是函数文档\n\t函数定义过程中name是叫形参,当函数执行时传递进来的参数叫实参'

>>> help(MyFunction1)

Help on function MyFunction1 in module __main__:

MyFunction1(name)

这里是函数文档

函数定义过程中name是叫形参,当函数执行时传递进来的参数叫实参

>>> print.__doc__

"print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n\nPrints the values to a stream, or to sys.stdout by default.\nOptional keyword arguments:\nfile: a file-like object (stream); defaults to the current sys.stdout.\nsep: string inserted between values, default a space.\nend: string appended after the last value, default a newline.\nflush: whether to forcibly flush the stream."

2、关键字参数

主要作用是避免参数顺序错误

>>> def MyFunction2(name, words):

print(name + '-->' +words)

>>> MyFunction2('Cyrus', 'Love Python')

Cyrus-->Love Python

>>> MyFunction2('Love Python', 'Cyrus')

Love Python-->Cyrus

>>> MyFunction2(words = 'Love Python', name = 'Cyrus')

Cyrus-->Love Python

3、默认参数

如果参数调用时,没有实参,参数会默认使用形参

>>> def MyFunction3(name = 'Cyrus', words = 'Love Python'):

print(name + '-->' +words)

>>> MyFunction3()

Cyrus-->Love Python

>>> MyFunction3('Cady')

Cady-->Love Python

>>> MyFunction3('Cady', 'Love Cyrus')

Cady-->Love Cyrus

4、收集参数(可变参数)

收集参数就是把函数内所有参数用元组打包

>>> def test(*params):

print('参数长度:', len(params));

print('第二个参数是:', params[1]);

>>> test(1, 'python', 3.14, True)

参数长度: 4

第二个参数是: python

>>> def test(*params, exp = 3.14):

print('参数长度:', len(params), exp);

print('第二个参数是:', params[1]);

>>> test(1, 'python', 3.14, True)

参数长度: 4 3.14

第二个参数是: python

>>> def test(*params, exp):

print('参数长度:', len(params), exp);

print('第二个参数是:', params[1]);

>>> test(1, 'python', 3.14, True, exp = 'Cyrus')

参数长度: 4 Cyrus

第二个参数是: python

习题

求所有的四叶玫瑰花数

def Rose():

for each in range(1000, 10000): #从1000-9999

temp = each #temp初始等于1000

sum = 0

while temp:

sum = sum + (temp%10) ** 4#求temp的个位数,并算出4次幂

temp = temp // 10 #减少位数

if sum == each:

print(each, end = ' ')

print('所有的四叶玫瑰数是:', end = ' ')

Rose()

所有的四叶玫瑰数是: 1634 8208 9474

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值