安装Python3
- Mac OS 或 Linux下
Python3 -V - Windows下
c:\Python31\python.exe -V
注: 这里的V必须大写.使用小写的v会进入Python解释器.使用quit()命令会退出解释器,返回到操作系统提示符.
创建简单的Python列表
num = ["a","b","c"]
标识符是指示数据对象的名字,python的标识符本身木有”类型”,但是标识符指示的数据对象有类型.比如下面要介绍的isinstance() BIF.
列表就像是数组
在Python创建一个列表时,解释器会在内存中创建一个类似数组的数据结构来存储数据.数据项自下而上堆放(堆栈).index从0开始.
访问数据
print(num[0])
列表的长度
len(num)
列表末尾增加一个数据项
num.append("d")
列表末尾删除一个数据项
num.pop()
列表末尾增加一个数据集合
num.extend(["d","e"])
列表删除一个特定的数据项
num.remove("d")
在某个位置增加一个特定是数据项
num.insert(3,"d")
Python的列表可以包含混合类型的数据.Python列表是一个高层集合,原本设计要为存储一个”相关事物”的集合.但是列表并不关心这些事物的类型.因为列表只是为了提供一种机制,从而可以采用列表的形式存储.所有就算是你存储混合类型的数据,Python也是支持的.
迭代
for 循环
for items in num:
print(items)
注: 注意缩进及后面的分号
while循环
count = 0
while count < len(num):
print(num[count])
count += 1
注: while和for完成的工作是一样的,但是使用while时必须考虑”状态信息”.这就要求使用一个计数器.而使用for时,Python解释器会帮你考虑”状态信息”.
列表中存储列表
每个列表都是一个列表项集合,个列表项相互之间用逗号隔开,另外列表要用中括号括起来.当然,任何列表项本身也可以是另一个列表.
有一个电影基本信息列表,它本身包含一个主要演员列表,Graham Chapman下又是一个配角列表:
The Holy Grail,1975,Terry Gilliam, 91mins
Graham Chapman
Michael Palin, John Cleese, Terry Gilliam
movies = [
"The Holy Grail", 1975, "Terry Gilliam", 91,
["Graham Chapman",
["Michael Palin", "John Cleese,", "Terry Gilliam"]]]
访问可以使用
print(movies[4][1][3])
条件语句
if else
if 2 > 3:
print("true")
else:
print("false")
isinstance
该BIF用于检查某个特定标识符是否包含某个特定类型的数据
isinstance(num,list)
函数
def fun_name(param):
code
迭代
Python3默认迭代深度不超过100.
Python术语
- BIF: 内置函数
- 组(suite): Python代码块,会通过缩进来指示分组
问答
- 迭代器处理一个列表是不是总有for而不是while?
除需要提供额外控制的时候,最好使用for循环. - 字符串单双引号问题?
没有任何区别.但是有一个规则: 保持一致,前面使用了什么后面就要用什么.不能混用. - 如果需要在一个字符串中嵌入一个双引号时怎么办?
可以对双引号进行转义: \”, 或者用单引号引起这个字符串 大小写敏感与否?
Python属于”敏感型”.区分大小写.并且只有当标识符已经被赋值后才能在代码中使用.未赋值的标识符会导致”NameError”运行时错误 name ‘**’ is not definedPython有很多BIF吗?
Python有70多个BIF这些BIF分别是做什么的?
输入dir(__builtins__)可以看到Python的BIF列表.具体要看 某个BIF是做什么的可以输入help(*)比如help(input)['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', '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']
Python 基础教程
751

被折叠的 条评论
为什么被折叠?



