首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

python快速入门3

2012-12-18 
python快速入门三abs(num) 返回 num 的绝对值coerce(num1, num2) 将num1和num2转换为同一类型,然后以一个

python快速入门三
abs(num) 返回 num 的绝对值
coerce(num1, num2) 将num1和num2转换为同一类型,然后以一个 元组的形式
返回。
divmod(num1, num2) 除法-取余运算的结合。返回一个元组(num1/num2,num1 %
num2)。对浮点数和复数的商进行下舍入(复数仅取实
数部分的商)
pow(num1, num2, mod=1) 取 num1 的 num2次方,如果提供 mod参数,则计算结果
再对mod进行取余运算
round(flt, ndig=0) 接受一个浮点数 flt 并对其四舍五入,保存 ndig位小数。
若不提供ndig 参数,则默认小数点后0位。

整数
hex(num) 将数字转换成十六进制数并以字符串形式返回
oct(num) 将数字转换成八进制数并以字符串形式返回
chr(num) 将ASCII值的数字转换成ASCII字符,范围只
能是0 <= num <= 255。
ord(chr) 接受一个 ASCII 或 Unicode 字符(长度为1的字符串),返回相应的ASCII
或Unicode 值。
unichr(num) 接受Unicode码值,返回 其对应的Unicode字符。所接受的码值范围依赖于
你的Python是构建于UCS‐2还是UCS‐4。


bool(obj) b 返回obj对象的布尔值,也就是
obj.__nonzero__()方法的返回值
int(obj, base=10) 返回一个字符串或数值对象的整数表
示, 类似string.atoi();从Python 1.6起,
引入了可选的进制参数。
long(obj, base=10) 返回一个字符或数据对象的长整数表
示,类似string.atol(), 从Python1.6起,
引入了可选的进制参数
float(obj) 返回一个字符串或数据对象的浮点数
表示,类似string.atof()
complex(str) or
complex(real, imag=0.0) 返回一个字符串的复数表示,或
者根据给定的实数(及一个可选
的虚数部分)生成一个复数对象。


decimal 十进制浮点运算类 Decimal
array 高效数值数组(字符,整数,浮点数等等)
math/cmath 标准C库数学运算函数。常规数学运算在match模块,
复数运算在cmath模块
operator 数字运算符的函数实现。比如 tor.sub(m,n)等价
Edit By Vheavens
Edit By Vheavens
于 m - n
random 多种伪随机数生成器
randrange() 它接受和 range() 函数一样的参数, 随机返回
range([start,]stop[,step])结果的一项
uniform() 几乎和 randint()一样,不过它返回的是二者之间的一个浮点数(不包括范围
上限)。
random() 类似 uniform() 只不过下限恒等于0.0,上限恒等于1.0
choice() 随机返回给定序列的一个元素

随机整数:>>> import random>>> random.randint(0,99)21随机选取0到100间的偶数:>>> import random>>> random.randrange(0, 101, 2)42随机浮点数:>>> import random>>> random.random() 0.85415370477785668>>> random.uniform(1, 10)5.4221167969800881随机字符:>>> import random>>> random.choice('abcdefg&#%^*f')'d'多个字符中选取特定数量的字符:>>> import randomrandom.sample('abcdefghij',3) ['a', 'd', 'b']多个字符中选取特定数量的字符组成新字符串:>>> import random>>> import string>>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).replace(" ","")'fih'随机选取字符串:>>> import random>>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )'lemon'洗牌:>>> import random>>> items = [1, 2, 3, 4, 5, 6]>>> random.shuffle(items)>>> items[3, 2, 5, 6, 4, 1]


>>> pow(2,5)32>>> pow(3,3)27>>> pow(3,2)9>>> pow(3,2,2)1>>> round(3.5)4.0>>> round(3.3)3.0>>> round(3.49999,2)3.5>>> round(3.49999,3)3.5>>> round(3.2344,2)3.23>>> round(3.23445, 3)3.234>>> import math>>> for eachNum in rang(10):print round(math.pi, eachNum)Traceback (most recent call last):  File "<pyshell#13>", line 1, in <module>    for eachNum in rang(10):NameError: name 'rang' is not defined>>> for eachNum in range(10):print round(math.pi, eachNum)3.03.13.143.1423.14163.141593.1415933.14159273.141592653.141592654>>> round(math.pi, 100)3.1415926535897931>>> math.pi3.1415926535897931>>> round(-3.5)-4.0>>> int(3.5)3>>> round(3.5)4.0>>> float(3.5)3.5>>> .20.20000000000000001>>> for eachNum in (.2, .7, 1.2, 1.7, -.2, -.7, -1.2, -1.7):print "int(%.1f)\t%+.1f" %(eachNum, float(int(eachNum)))print "floor(%.1f)\t%+.1f" % (eachNum, math.floor(eachNum))print "round(%.1f)\t%+.1f" % (eachNum, round(eachNum))print "-"*20int(0.2)+0.0floor(0.2)+0.0round(0.2)+0.0--------------------int(0.7)+0.0floor(0.7)+0.0round(0.7)+1.0--------------------int(1.2)+1.0floor(1.2)+1.0round(1.2)+1.0--------------------int(1.7)+1.0floor(1.7)+1.0round(1.7)+2.0--------------------int(-0.2)+0.0floor(-0.2)-1.0round(-0.2)-0.0--------------------int(-0.7)+0.0floor(-0.7)-1.0round(-0.7)-1.0--------------------int(-1.2)-1.0floor(-1.2)-2.0round(-1.2)-1.0--------------------int(-1.7)-1.0floor(-1.7)-2.0round(-1.7)-2.0-------------------->>> for eachChr in 'attitude':print eachChrattitude>>> ord([for eachChr in 'attitude'])SyntaxError: invalid syntax>>> sum(ord('a'))Traceback (most recent call last):  File "<pyshell#36>", line 1, in <module>    sum(ord('a'))TypeError: 'int' object is not iterable>>> ord('a')97>>> ord([for ch in 'a'])SyntaxError: invalid syntax>>> >>> sum([ord('a')])97>>> sum(ord(c) - 96 for c in 'attitude')100>>> 0.10.10000000000000001>>> from decimal import Decimal>>> dec = Decimal(.1)Traceback (most recent call last):  File "<pyshell#44>", line 1, in <module>    dec = Decimal(.1)  File "C:\Python26\lib\decimal.py", line 649, in __new__    "First convert the float to a string")TypeError: Cannot convert float to Decimal.  First convert the float to a string>>> dec = Decimal('.1')>>> decDecimal('0.1')>>> print dec0.1>>> dec + 1.0Traceback (most recent call last):  File "<pyshell#48>", line 1, in <module>    dec + 1.0TypeError: unsupported operand type(s) for +: 'Decimal' and 'float'>>> dec + Decimal('1.0')Decimal('1.1')>>> randrang(1)Traceback (most recent call last):  File "<pyshell#50>", line 1, in <module>    randrang(1)NameError: name 'randrang' is not defined>>> import math>>> random(1)Traceback (most recent call last):  File "<pyshell#52>", line 1, in <module>    random(1)NameError: name 'random' is not defined>>> random()Traceback (most recent call last):  File "<pyshell#53>", line 1, in <module>    random()NameError: name 'random' is not defined>>> random.random()Traceback (most recent call last):  File "<pyshell#54>", line 1, in <module>    random.random()NameError: name 'random' is not defined>>> import random>>> random.randint(0,99)75>>> random.randrange(0,101)86>>> random.randrange(0,101,3)84

热点排行