`

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
分享到:
评论

相关推荐

    最新 python 3.1 下载

    1. **Python 3.1 版本特点**: - Python 3.1引入了轻量级的生成器表达式语法,使得创建生成器更加简洁。 - 引入了`__future__`模块,允许开发者在Python 2和3之间切换语法,以便于向Python 3迁移。 - `map()`,`...

    python_3.1官方入门指南中文版

    Python 3.1官方入门指南中文版是一本针对初学者的详尽教程,旨在帮助读者快速掌握Python 3.1的基础知识和编程技巧。这本书涵盖了Python语言的核心概念、语法结构以及常用的内置模块,是学习Python编程的理想起点。 ...

    《Beginning Python:Using Python 2.6 and Python 3.1》PDF

    - **Web开发**:可能会介绍使用Python进行简单的Web开发,如Flask或Django框架的基础知识。 - **数据分析**:Python在数据分析领域的应用,如NumPy和Pandas库的介绍。 通过这本书,读者可以系统地学习Python编程...

    Beginning Python Using Python2.6 and Python3.1

    - **第1章:编程基础与字符串**:介绍Python的基础知识,包括编程环境的搭建、基本的数据类型、变量以及字符串的操作方法。 - **第2章:数字与运算符**:详细介绍Python中的数值类型和各种运算符的使用方法,包括...

    python 3.1入门指南.pdf

    输入输出部分介绍了格式化输出,文件读写的基础知识,以及如何使用pickle模块进行对象的序列化和反序列化。错误和异常处理章节则详细讲解了语法错误、异常的分类、异常的处理方法,以及如何抛出异常和定义用户自定义...

    python3.1 中文版教程随书代码

    综上所述,"python3.1 中文版教程随书代码"这套资源将为学习者提供全面的Python3.1知识体系,从基础知识到高级应用,从理论讲解到实战演练,对于系统性地提升Python编程技能具有极大的帮助。通过阅读...

    Beginning Python:Using Python 2.6 and Python 3.1-628页

    《Beginning Python:Using Python 2.6 and Python 3.1》是Python语言的入门书籍,涵盖了Python语言的基础知识、语法结构和高级主题等方面的内容。该书籍适合初学者和中级开发者,帮助读者快速上手Python语言。

    Beginning Python Using Python 2.6 and Python 3.1

    《使用 Python 2.6 和 Python 3.1 进行编程的入门经典》是一本专为初学者设计的Python编程教程,旨在帮助读者掌握Python语言的基础知识,并能灵活运用到实际项目中。这本书涵盖了从安装Python环境到编写复杂程序的全...

    Wrox.Beginning.Python.Using.Python.2.6.and.Python3.1.Feb.2010.rar

    《Wrox.Beginning.Python.Using.Python.2.6.and.Python3.1.Feb.2010》这本书是面向初学者的一本Python编程教程,它涵盖了Python 2.6和Python 3.1两个主要版本。这本书旨在帮助读者从零开始,逐步掌握Python语言的...

    Beginning Python - Using Python 2.6 and Python 3.1

    《初识Python——基于Python 2.6和...通过阅读《初识Python——基于Python 2.6和Python 3.1》,读者不仅可以学习到Python的基础知识,还能了解到Python在不同应用场景下的使用方法,为未来深入学习和开发奠定坚实基础。

    Beginning Python:Using Python 2.6 and Python 3.1

    #### 二、基础知识与字符串(第1章) - **程序基础**:介绍编程的基本概念,包括算法、流程控制等。 - **字符串操作**:讲解字符串的基本概念,如字符串的创建、索引、切片等操作,以及字符串格式化方法。 - **字符...

    python3.1 files

    以下是关于Python 3.1的一些关键知识点: 1. **Unicode默认编码**: 在Python 3.1中,字符串(str)类型默认为Unicode编码,这使得处理不同语言和字符集变得更加容易。这意味着所有字符串都以Unicode表示,为开发者...

    Beginning Python:Using Python 2.6 and Python 3.1.pdf

    1. Python基础:书中第一部分“Dipping Your Toe into Python”是为初学者准备的,涵盖了编程基础知识和字符串的处理,例如在“Chapter 1: Programming Basics and Strings”中,读者将学习如何编写Python程序,了解...

    Beginning Python Using Python 2.6 and Python 3.1 - James W. Payne.pdf )

    #### 第1章:编程基础知识与字符串 本章为读者提供了Python编程的基础概念,包括语法结构、变量类型以及如何处理文本数据。通过讲解字符串的创建、操作和格式化,帮助读者建立对Python基本数据类型的理解。 #### ...

    Python知识手册-V3.1.pdf

    Python知识手册-V3.1.pdf Python知识手册-V3.1是...《Python知识手册-V3.1》是Python学习者的一本实用手册,涵盖了Python基础知识、数据分析、数据可视化等方面的内容。本手册适合具有一些Python基础知识的读者阅读。

    Beginning Python Using Python 2.6 and Python 3.1 - 2010.pdf

    本书不仅涵盖了Python语言的基础知识,还深入介绍了高级主题和实际应用场景,适合希望从零开始学习Python或希望提升Python技能的读者。通过丰富的示例和练习,读者可以逐步掌握Python编程的精髓,为未来的职业生涯或...

Global site tag (gtag.js) - Google Analytics