python编程思维

一、常用操作

1、列表元素遍历

二、常见逻辑

1、循环次数未知,直到判断条件不成立时,跳出循环——while循环

使用场景示例:对于输入进行检测,直到输入符合规则才能进入下一步

    operation = input()
    option = ["add", "remove", "clear", "show", "quit"]
    while operation not in option:
        print("输入不合法,请重新输入需要进行的操作:add,remove,clear,show,quit")
        operation = input()

2、套娃操作——递归

使用场景示例:如文件夹包含子文件夹,子文件夹又包含文件夹,如统计文件夹的大小

    def cal_size(self,file_path):
        sum=0
        try:
            os.listdir(file_path)
        except:
            pass
        else:
            for f in os.listdir(file_path):
                abspath=os.path.join(file_path,f)
                if os.path.isfile(abspath):
                    sum+=os.path.getsize(abspath)
                elif os.path.isdir(abspath):
                    sum+=self.cal_size(abspath)    #重复调用该函数
        return sum

3、一切皆对象

python中的所有都是对象,即都具备三个特性:身份(id),类型(type),值(value)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值