Head First Python之人人都爱列表(1-初识Python)

IDLE

内置函数==BIF==built-in function

默认地,内置函数都是紫色,字符串是绿色,关键字是橙色。

tab:自动补全

Alt-P:前一个

Alt-N:下一个

列表的特性

列表看起来很像数组

 python变量标识符没有类型,所以不用定义

列表中可以混合存储不同类型的数据

movies = ['The Holy Grail', "The Life of Brain", "The meaning of Life"]
print(movies[1])
# 计算列表长度
print(len(movies))
# 增加一个数据项
movies.append("Gilliam")
print(movies)
# 增加一个数据项组合
movies.extend(['a', 'b'])
print(movies)
# 删除特定的项
movies.remove("The Life of Brain")
print(movies)
# 在特定位置前增加数据项
movies.insert(0, "Chapman")
print(movies)
# for循环处理任意大小的列表
for i in movies:
    print(i)

 列表中存储列表

我们可以存储多层嵌套列表,如下:

movies = ['The Holy Grail', "The Life of Brain", 91, ["chapman", ["John", "Eric Idle"]]]

内置方法(BIF)

python3中有70个多内建方法,dir(__builtins__)进行查询

查询方法用法,如input方法:  help(input)

用递归和函数处理复杂的列表

movies = ['The Holy Grail', "The Life of Brain", 91, ["chapman", ["John", "Eric Idle"]]]
def print_lol(li):
    for each in li:
        if isinstance(each, list):
            print_lol(each)
        else:
            print(each)

print_lol(movies)

  

原文地址:https://www.cnblogs.com/lingzeng86/p/6691744.html