一、常用操作
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)