#定义列表,列表像数组一样,cast 为标识符(没有类型)
cast = ["chinese","English","math","Music"]
#像数组一样下标从0开始
print (cast[1])
#len()输出列表的元素个数
print len(cast)
#在末尾添加新元素
cast.append("History")
#删除末尾的元素
cast.pop()
#在列表末尾添加一个新的列表里的元素
cast.extend(["aaaa","bbbb"])
#删除列表中为"math"的元素
cast.remove("math")
#在下标为0的位置上插入"cccc"
cast.insert(0,"cccc")
#for循环(和java中的增强for循环有点像)
for _name in movies:
print _name,
#while循环,得先定义一个变量,一般都用for循环不用while
count = 0
while count < len(movies):
print movies[count]
count = count + 1
#定义一个复杂的列表,列表中嵌套多个列表(\为转义字符)
movies1=["The Holy\" Grail",[1975,["The Life of Brian",1979,"The Meaning of Life",1983]]]
print movies1;
#一个列表一个维度
print movies1[1][1][3] #输出的是1983
#for循环可以解决列表嵌套列表的输出问题
for name in movies1:
print name
#isinstance()作用是判断count是否为列表,返回boolean
print isinstance(count,list)
print isinstance(1.00,float)
#movies就是标识符,标识符没有"类型"
movies1=["The Holy\" Grail",[1975,["The Life of Brian",1979,"The Meaning of Life",1983]]]
#for循环可以解决列表嵌套列表的输出问题(但是列表过多会很麻烦,调用太多for和if语句,所以这种情况下用递归比较好)
for name in movies1:
if isinstance(name,list):
for name1 in name:
if isinstance(name1,list):
for name2 in name1:
print name2,
else:
print name1,
else:
print name,
dir(__builtins__)在命令行下输入这句话,就会查看全部的BIF(内置函数)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning',
'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError',
'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError',
'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented',
'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError',
'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit',
'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError',
'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError',
'__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 'all', 'any', 'apply', 'basestring', 'bin',
'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile', 'complex',
'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter',
'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'intern',
'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'memoryview', 'min',
'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'raw_input', 'reduce', 'reload',
'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple',
'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']
如果想查看相应的BIF的用法就用help(isinstance)就可以查看它的用法例子如下
>>>help(isinstance)
Help on built-in function isinstance in module builtins:
isinstance(...)
isinstance(object, class-or-type-or-tuple) -> bool
#递归,python3默认递归深度为100,如果有需要还可以修改深度上线
#标识符没有"类型"所以ls也不用声明类型
def a(ls):
for name in ls:
if isinstance(name,list):
a(name)
else:
print name
#调用函数
print a(movies1)