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[0])
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. 下标是否可以越界,取值范围多少?

# 验证1
t1 = 123, 321, 'python'
t2 = (123, 321, 'python')
print(t1 == t2)
# 结果:True
# 结论1:括号不是必须的.

# 验证2
##t = 123
##print(t[0])
# 结果: TypeError

##t = (123)
##print(t[0])
# 结果: TypeError

t1 = 123,
print(t1[0])
t2 = (123,)
print(t2[0])
# 结果:True
# 结论: 逗号必须的.

#验证3
t1 = 1,
t2 = 2,
t3 = 3,
t = t1, t2, t3
print(t)
# 结果: ((1,), (2,), (3,))
# 结论:元组可以嵌套

# 验证4
t = 1,2;
a = -1
print(t, t[-2])
# 结果: IndexError
# 结论: 下标从0开始,不可以越界.
# 注意: 下标为 (<=元组长度) 的负数或False时,当作0处理;True时当作1处理.

# 2. Module Search Path(模块搜索路径) : *.py文件搜索过程:从左到右搜索sys.path中的目录.

# 查看:
import os
import sys
print(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 sys
sys.path.append('E:\\ext')
print(sys.path)

# 3. 内置函数
# dir() : 查看已经定义的模块名
print(dir())
# ['__builtins__', '__doc__', '__name__', '__package__']

# 技巧:
# 查看已经定义的内置模块,函数,变量.
import builtins
print(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))


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值