python中什么是递归_Python基础入门----递归

Python Recursion

In this article, you will learn to create a recursive function; a function that calls itself.

Table of Contents

Python 递归

在这篇文章,你将学习添加一个递归函数;函数可以调用自己。

表格内容

在Python里什么是递归?

Python递归函数

递归的优点

递归的缺点

What is recursion in Python?

Recursion is the process of defining something in terms of itself.

A physical world example would be to place two parallel mirrors facing each other. Any object in between them would be reflected recursively.

在Python里什么是递归?

递归是在自己本身定义一些东西的程序。

一个物理世界的例子像放置2面平行的镜子面对面。任何他们间的对象将会递归反射。

Python Recursive Function

We know that in Python, a function can call other functions. It is even possible for the function to call itself. These type of construct are termed as recursive functions.

Following is an example of recursive function to find the factorial of an integer.

Factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 6 (denoted as 6!) is 1*2*3*4*5*6 = 720.

Python递归函数

我们知道,函数能调用其它函数。它甚至可能调用函数本身。这类型的购置称为递归函数。

以下是查找一个整数阶乘递增函数的例子。

数字阶乘是1到这个数的整数乘积。例如,这个6的阶乘(代表为6!)是1*2*3*4*5*6=720.

Example of recursive function

# An example of a recursive function to

# find the factorial of a number

def calc_factorial(x):

"""This is a recursive function

to find the factorial of an integer"""

if x == 1:

return 1

else:

return (x * calc_factorial(x-1))

num = 4

print("The factorial of", num, "is", calc_factorial(num))

In the above example, calc_factorial() is a recursive functions as it calls itself.

When we call this function with a positive integer, it will recursively call itself by decreasing the number.

递归函数的例子

In [1]: def calc_factorial(x):

...: """This is a recursive function

...: to find the factorial of an integer"""

...: if x == 1:

...: return 1

...: else:

...: return(x * calc_factorial(x-1))

...:

In [2]: num = 4

In [4]: print("The factorial of",num ,"is",calc_factorial(num))

The factorial of 4 is 24

在上面的例子,calc_factorail()是一个递归函数因为它调用它自己。

当我们调用这个函数并带上一个位置函数,它将递减一个数再调用自己。

Each function call multiples the number with the factorial of number 1 until the number is equal to one. This recursive call can be explained in the following steps.

calc_factorial(4) # 1st call with 4

4 * calc_factorial(3) # 2nd call with 3

4 * 3 * calc_factorial(2) # 3rd call with 2

4 * 3 * 2 * calc_factorial(1) # 4th call with 1

4 * 3 * 2 * 1 # return from 4th call as number=1

4 * 3 * 2 # return from 3rd call

4 * 6 # return from 2nd call

24 # return from 1st call

Our recursion ends when the number reduces to 1. This is called the base condition.

Every recursive function must have a base condition that stops the recursion or else the function calls itself infinitely.

每次函数调用这个数都乘以1的阶乘指导这个数等于1.这个递归调用的解释以下步骤解释。

calc_factorial(4) # 1st call with 4

4 * calc_factorial(3) # 2nd call with 3

4 * 3 * calc_factorial(2) # 3rd call with 2

4 * 3 * 2 * calc_factorial(1) # 4th call with 1

4 * 3 * 2 * 1 # return from 4th call as number=1

4 * 3 * 2 # return from 3rd call

4 * 6 # return from 2nd call

24 # return from 1st call

Advantages of Recursion

Recursive functions make the code look clean and elegant.

A complex task can be broken down into simpler sub-problems using recursion.

Sequence generation is easier with recursion than using some nested iteration.

递归的优点

递归函数能使代码看起来干净优雅。

使用递归能使一个复杂任务被分解常简单的子问题

序列生成使用递归比使用一些内嵌迭代更简单。

Disadvantages of Recursion

Sometimes the logic behind recursion is hard to follow through.

Recursive calls are expensive (inefficient) as they take up a lot of memory and time.

Recursive functions are hard to debug.

递归的缺点

有时候递归的背后的逻辑是很难理解的。

递归调用花费高(效率低),因为他们占用很多内存和时间。

递归函数很难调试。

Check out these examples to learn more:

标签:function,入门,递归,Python,factorial,call,calc

来源: https://blog.csdn.net/sudo_tudou/article/details/88622676

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值