初识Python
1、列表的操作方法:
cast = ["Cleese", 'Palin', 'Jones', "Idle"] print(cast) # 打印整个列表 print(len(cast)) # 打印列表长度 print(cast[0]) # 打印列表首项 cast.append("Gillian") #在列表末尾增加一个数据项 cast.pop() # 删除列表末尾数据项 cast.extend(["Gilliam","Chapman"]) # 在列表末尾增加数据项的集合 cast.remove("Chapman") # 找到并删除一个特定的数据项 cast.insert(0, "Chapman") #在制定位置插入数据项
2、迭代数据的操作方法:
fav_movies = ["The Holy Grail", "The Life of Brain"] for each_flick in fav_movies: print(each_flick)
count = 0 while count < len(movies): print(movies[count]) count = count + 1
3、 Python 大小写敏感
4、BIF: isinstance() 某个特定的标识符是否包含某个特定类型的数据
for each_item in movies: if isinstance(each_item, list): # 某个特定字符串是否包含某个特定类型的数据 for nested_item in each_item: print(nested_item) else: print(each_item)
5、函数和递归
movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gillian", 91, ["Graham Champman", ["Michael Palin", "john Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]]] def print_lol(movie_list): for item in movie_list: if isinstance(item, list): print_lol(item) else: print(item)
print_lol(movies)