Python学习_IDLE快捷键以及列表相关杂记
IDLE快捷键
Tab完成:键入部分代码,按下TAB键,IDLE将给出列表帮助完成语句
回退代码语句:按下Alt+P(Previous),可以回退到IDLE中之前输入的代码语句,
下一个代码语句:按下Alt+N(Next)可以移动至下一个代码语句
调整IDLE首选项:通过Options>Configure IDLE调整默认行为
列表:
列表末尾增加数据:list.append()
列表末尾删除数据:list.pop()
列表末尾增加数据项集合:list.extend()
列表中特定位置删除数据项:remove()
列表中特定位置增加数据项:insert()
>>>cast=["clee","palin","Idle"] >>> cast.append("Gilliam") >>> print cast ['clee', 'palin', 'Idle', 'Gilliam'] >>> cast.pop() 'Gilliam' >>> print cast ['clee', 'palin', 'Idle'] >>> cast.extend(["Gilliam","Chapman"]) >>> cast ['clee', 'palin', 'Idle', 'Gilliam', 'Chapman']
>>> cast.remove("Chapman") >>> print cast ['clee', 'palin', 'Idle', 'Gilliam'] >>> cast.insert(0,"Chapman") >>> cast ['Chapman', 'clee', 'palin', 'Idle', 'Gilliam']
检查列表项本身是否为列表:isinstance()
>>> movies=["The Holy Grall","Terry Jones & Terry Gilliam",91,["Graham Chapman",["Michael Palin","John Cleese","Eric Idle"]]] #循环方法输入所有列表项 >>> for item in movies: if isinstance(item,list): for inner_item in item: if isinstance(inner_item,list): for deep_item in inner_item: print deep_item else: print inner_item else: print item The Holy Grall Terry Jones & Terry Gilliam 91 Graham Chapman Michael Palin John Cleese Eric Idle #定义迭代函数方法输入所有列表项 >>> def print_allitems(given_list): for item in given_list: if isinstance(item,list): print_allitems(item) else: print item >>> print_allitems(movies) The Holy Grall Terry Jones & Terry Gilliam 91 Graham Chapman Michael Palin John Cleese Eric Idle
返回当前作用域中定义的所有名的集合:locals()
if 'data' in locals(): data.close()