Recursive invocation of multiple types of values and redundant parameters and functions in Python functions

Source: Internet
Author: User

1. Multi-type pass-through values and redundancy parameters


Multi-type pass-through values:

def fun (x, y):

return x +y

Print Fun(3,5)

8

Print Fun(*t)

3

def fun(x, Y, z):

return x + y + Z

T1 = (a)

Fun(*t1)

6

Fun(* (2,4,5))

11

Fun(1,*t)

4

Print T

(1, 2)

Fun (x=1,y=3,z=5)

9

>>> dic = {' x ': 1, ' Y ': 3, ' Z ': 6}

>>> Fun(**dic)

Ten

Redundant parameters:

>>> def fun (x,*args,**kwargs):

... print x

.. print args

... print Kwargs

...    

>>> Fun (1)

1

()

{}

>>> Fun (+)

1

(2,)

{}

>>> Fun ( all in all)

1

(2, 3)

{}

>>> T

(1, 2)

>>> Fun (' A ', [1,2],*t,a=3,**{' t ': one, ' P ':')

1

(2, 3, ' a ', [1, 2], 1, 2)

{' A ': 3, ' P ': $, ' t ': one}


2. Recursive invocation of functions

Considerations for recursion:

You must have the final default result:

if n = = 0

Recursive parameters must converge to the default result:

Factorial (n-1)


Factorial script:

#!/usr/bin/env python

#-*-Coding:utf-8-*-

# @Time: 2018/1/4 11:57

# @Author: Feng xiaoqing

# @File: jiecheng.py

# ======================

def factorial (n):

sum = 0

For i in range (1,n+1):

sum + = i

return sum

print factorial (+)


Another method:


def factorial (n):

if n = = 0:

return 1

Else:

return n * factorial (n-1)

print factorial (5)


For the sum of 1-100 additions:

def factorial (n):

if n = = 0:

return 0

Else:

return n + factorial (n-1)

print factorial (+)


Recursive invocation of multiple types of values and redundant parameters and functions in Python functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.