恩恩,博主要开始学习Python啦!
自勉
环境:
- Python 2.7.6
- Mac OX 10.10.4
- PyCharm CE
参考书籍:
<<Head First Python (O'REILLY)>>
==========================================
名词
BIF:内建函数,built-in functions
==========================================
列表,类似数组的数据结构,数据项自上而下堆放,形成一个堆栈。
与很多语言一样,Python从零开始计数。
Python的列表不限定数据类型,有点像Mongodb,是一个集合
movies = ["The Holy Grail","The Life of Brain","The Meaning of Life"]
列表常用函数:
# coding=gbk
movies = ["The Holy Grail","The Life of Brain","The Meaning of Life"]
# 计算数量
len(movies)
# 末尾增加
movies.append("movie 1")
# 末尾删除(弹出)
movie = movies.pop()
print(movie)
# 末尾追加集合
movies.extend(["movie 2","movie 3"])
# 删除指定数据项
movies.remove("movie 2")
#在特定位置前面添加一个数据项
movies.insert(0,"movie 3")
For循环
for movie in movies:
print(movie)
While循环,需要小心数组越界—>IndexError
count = 0
while count < len(movies):
print(movies[count])
count = count +1
======================================
函数定义:
def print_list(msg):
if isinstance(msg,list):
for item in msg:
if isinstance(item,list):
print_list(item)
else:
print(item)
else:
print(msg)
恩恩,发现其实不用先判断instance(msg,list)
改进如下:
def print_list_simple(msg):
for item in msg:
if isinstance(item,list):
print_list(item)
else:
print(item)
=======================================
第一天先到这里了