首页 / PYTHON / Python基础入门----递归
Python基础入门----递归
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python基础入门----递归,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4644字,纯文字阅读大概需要7分钟。
内容图文
![Python基础入门----递归](/upload/InfoBanner/zyjiaocheng/837/c1e185c1435e433d84d52ecf2e6fae6c.jpg)
Python Recursion
In this article, you will learn to create a recursive function; a function that calls itself.
Table of Contents
- What is recursion in Python?
- Python Recursive Function
- Advantages of Recursion
- Disadvantages of Recursion
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:
-
Python Program to Display Fibonacci Sequence Using Recursion
-
Python Program to Find Sum of Natural Numbers Using Recursion
内容总结
以上是互联网集市为您收集整理的Python基础入门----递归全部内容,希望文章能够帮你解决Python基础入门----递归所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。