python 教程 第十二章、 标准库
来源:互联网 发布:苹果电脑怎么关闭软件 编辑:程序博客网 时间:2024/06/08 15:28
第十二章、 标准库
See Python Manuals ? The Python Standard Library ?
1) sys模块
import sys
if len(sys.argv) < 2:
print 'No action specified.'
sys.exit()
if sys.argv[1].startswith('--'):
option = sys.argv[1][2:]
if option == 'version':
print 'Version 1.2'
elif option == 'help':
print 'This program prints help'
else:
print 'Unknown option.'
sys.exit()
探索更多知识可使用help(sys)
2) os模块
os.name字符串指示你正在使用的平台。
os.getcwd()得到当前工作目录
os.getenv()和os.putenv()分别读取和设置环境变量。
os.listdir()返回指定目录下的所有文件和目录名。
os.remove()函数用来删除一个文件。
os.system()函数用来运行shell命令。
os.linesep字符串给出当前平台使用的行终止符
os.path.split()函数返回一个路径的目录名和文件名。
os.path.isfile()和os.path.isdir()函数分别检验给出的路径是一个文件还是目录。
os.path.existe()函数用来检验给出的路径是否真地存在。
探索更多知识可使用help(os)
3) 数学运算
Math模块
import math
print math.pi
print math.sqrt(9)
print sin(2 * pi / 180)
random模块
import random
print random.random()
print random.choice(['Life of Brian', 'Holy Grail'])
print random.randint(1, 10)
decimal小数
>>> import decimal # Decimals: fixed precision
>>> d = decimal.Decimal('3.141')
>>> d + 1 #Decimal('4.141')
>>> decimal.getcontext().prec = 2
>>> decimal.Decimal('1.00') / decimal.Decimal('3.00')
Decimal('0.33')
fractions分数
import fractions
f = fractions.Fraction(2, 3) #2/3
print f + 1 #5/3
print f + fractions.Fraction(1, 2) #7/6
0 0
- python 教程 第十二章、 标准库
- python 教程 第十二章、 标准库
- 简明Python教程 --- 14.Python标准库
- Python教程之九-----标准库的简短描述
- 第十四章 python标准库
- Python读书笔记第十二章:异常
- python学习笔记 第十二章
- python -- Python 标准库
- 数学分析教程 第十二章学习感受
- 「学习笔记——Python」Python标准库简明教程
- 「学习笔记——Python」Python标准库简明教程II
- 【C++】《C++标准程序库》小结第十二章-数值
- python标准库 第五章 数学计算
- python 标准库库
- Python标准库
- python标准库
- Python标准库
- python 标准库
- python 教程 第七章、 数据结构
- python 教程 第八章、 第一个python程序
- python 教程 第九章、 类与面向对象
- python 教程 第十章、 输入/输出
- python 教程 第十一章、 异常
- python 教程 第十二章、 标准库
- python 教程 第十三章、 特殊的方法
- SecureCRT自动登录Linux并设置时间
- SecureCRT循环检查设备状态
- SecureCRT下载/注册/安装镜像文件
- python 教程 第十四章、 地址薄作业
- python 教程 第十五章、 结构布局
- python 教程 第十六章、 正则表达式
- python 教程 第十七章、 网络编程