Python中递归思想,打印斐波那契数列

本文探讨了Python编程中的递归思想,通过举例展示了如何使用递归函数打印斐波那契数列,并介绍了如何利用Python的内置函数如map、filter和reduce进行列表操作。同时,还讨论了目录的递归访问、嵌套函数的定义以及列表排序的实现方法。
摘要由CSDN通过智能技术生成

目录

一.递归访问目录:目录中嵌套目录,有层次的列出所有的文件和文件夹

二.定义一个嵌套函数

三.定义一个递归函数:打印菲波那契数列F(n)=F(n-1)+F(n-2)(n>=2,F(0)=0,F(1)=1)

四.对列表进行排序

五.利用map函数:计算三个列表,相同位置元素之和.

六.利用filter函数过滤列表中所有带a的字符串.

七.利用reduce计算1+2+3+...+100之和


一.递归访问目录:目录中嵌套目录,有层次的列出所有的文件和文件夹

先在D盘新建一个目录test,test目录中包含一个test1文件夹和test.txt文件,test1文件夹中包含test2和test.xsl,test2文件夹中包含test3和test.doc,test2文件夹中包含test3.txt

import os
 
 
def list_dir_content(dir_path, count=0):   
    for file_name in os.listdir(dir_path):
        sub_path = os.path.join(dir_path, file_name)
        if os.path.isdir(sub_path):
            print(count * "\t" + file_name)
            sub_count = count + 1
            list_dir_content(sub_path, sub_count)
        if os.path.isfile(sub_path):
            print(count * "\t" + file_name)
 
list_dir_content("D:\\test")
print(os.getcwd())
 
#执行结果
test.txt.txt
test1
	test.xls.xls
	test2
		test.doc.doc
		test3
			test3.txt.txt
D:\Python\python_code

二.定义一个嵌套函数

外层函数打印this is outing function

内层函数打印This is inner function

def outing():
    print('this is outing fuction')
    def inner():
        print('this is inner fuction')
    inner()
outing()
 
#执行结果
this is outing fuction
this is inner fuction
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值