Python 3.1 基础知识一
base.py#!usr/bin/python# -*- coding: utf-8 -*-# Filename : base.py# Author : amos_tl# Date : 2010-12-30''' PYTHON 基础知识 Linux 用户执行时需要给权限: chmod a+x base.py'''# 1. Tuples 元组: 简单理解就是由逗号分隔开的一组值。t = 123, 321, 'python'print(t)print(t)u = t, (1, 2, 3, 4, 5)print(u)# 结果:# >>> # 123# (123, 321, 'python')# ((123, 321, 'python'), (1, 2, 3, 4, 5))# 分析:# 1. 括号是不是必须的?# 2. 逗号是不是必须的?# 3. 元组可以嵌套?# 4. 下标是否可以越界,取值范围多少?# 验证1t1 = 123, 321, 'python't2 = (123, 321, 'python')print(t1 == t2)# 结果:True# 结论1:括号不是必须的.# 验证2##t = 123##print(t)# 结果: TypeError##t = (123)##print(t)# 结果: TypeErrort1 = 123,print(t1)t2 = (123,)print(t2)# 结果:True# 结论: 逗号必须的.#验证3t1 = 1,t2 = 2,t3 = 3,t = t1, t2, t3print(t)# 结果: ((1,), (2,), (3,))# 结论:元组可以嵌套# 验证4t = 1,2;a = -1print(t, t[-2])# 结果: IndexError# 结论: 下标从0开始,不可以越界.# 注意: 下标为 (<=元组长度) 的负数或False时,当作0处理;True时当作1处理.# 2. Module Search Path(模块搜索路径) : *.py文件搜索过程:从左到右搜索sys.path中的目录.# 查看:import osimport sysprint(sys.path)# [# 'E:/Python313/pyfiles', 'E:\\Python313\\Lib\\idlelib',# 'E:\\Python313\\python31.zip', 'E:\\Python313\\DLLs',# 'E:\\Python313\\lib', 'E:\\Python313\\lib\\plat-win',# 'E:\\Python313', 'E:\\Python313\\lib\\site-packages'# ]# 技巧:# 附加路径到 sys.path import syssys.path.append('E:\\ext')print(sys.path)# 3. 内置函数# dir() : 查看已经定义的模块名print(dir())# ['__builtins__', '__doc__', '__name__', '__package__']# 技巧:# 查看已经定义的内置模块,函数,变量.import builtinsprint(dir(builtins))# [# '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',# 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit',# 'TabError', 'True', 'TypeError',# 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError',# 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning',# 'ValueError',# 'Warning', 'WindowsError',# 'ZeroDivisionError',# '__build_class__',# '__debug__', '__doc__', '__import__', '__name__','__package__',# 'abs', 'all', 'any', 'ascii',# 'bin', 'bool', 'bytearray', 'bytes',# 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits',# 'delattr', 'dict', 'dir', 'divmod',# 'enumerate', 'eval', 'exec', 'exit',# 'filter', 'float', 'format', 'frozenset',# 'getattr', 'globals',# 'hasattr', 'hash', 'help', 'hex',# 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len',# 'license', 'list', 'locals',# 'map', 'max', 'memoryview', 'min',# 'next',# 'object', 'oct', 'open', 'ord',# 'pow', 'print', 'property',# 'quit',# 'range', 'repr', 'reversed', 'round',# 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super',# 'tuple', 'type',# 'vars', 'zip'# ]# 4. 包:实际上是目录名.# sys.path='e:\\' 那么在 e:\\parentDir\\subDir\\module.py 文件的模块的包# 为 import parentDir.subDir.module.py,而 base.py 在e:\\下.# 反过来 from ..subDir import base.py# 5. repr() 表达式计算print(repr(1+2))
页:
[1]