一、IDLE
1.TAB完成:先嵌入一些代码,再按TAB键。IDLE会提供一些建议,帮助你完成这个语句。
2.回退代码语句:使用shell时,Alt-P,回退到IDLE之前输入的代码语句;Alt-N,移至下一个代码语句(如果有的话)。Mac下将Alt改为Ctrl。
二、套嵌列表
输出套嵌列表:
movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91, ["Graham Chapman", ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]]] print(movies) for x in movies: if isinstance(x,list): for y in x: if isinstance(y,list): for z in y: print(z) else: print(y) else: print(x)
改进:将重复代码变成一个函数
movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91, ["Graham Chapman", ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]]] def print_lol(a_list): for each_item in a_list: if isinstance(each_item, list): print_lol(each_item) else: print(each_item) print_lol(movies)
三、BIF
isinstance(),len(),print()
时间: 2024-10-10 17:41:38