转自 http://www.cnblogs.com/BeginMan/archive/2013/06/08/3125876.html

一、标准类型函数

cmp():比较大小

str():转换为字符串

type():类型

cmp(...)
    cmp(x, y) -> integer
    Return negative(负数) if x<y, zero(0) if x==y, positive(正数) if x>y.

如下:

>>> cmp(5,3.2)
1
>>> cmp(3.5,8)
-1

二、转换工厂函数

存在精度损失

>>> int(1.847)
1
>>> long(42)
42L
>>> float(42)
42.0
>>> complex(42)
(42+0j)
>>> complex(2.4,-8)
(2.3999999999999999-8j)

三、功能函数

用于数值运算:asb()、coerce()、divmod()、pow()、round()

asb():absolute:绝对的;完全的;专制的;n:绝对值

>>> abs(-1)
1

coerce():vt. 强制,迫使,
类型转换,但是提供了不依赖python解释器而是通过自定义两个数值类型转换。返回一个元祖,存在强制行为。
coerce(...)
    coerce(x, y) -> (x1, y1)
    
    Return a tuple consisting of the two numeric arguments converted to
    a common type, using the same rules as used by arithmetic operations.
    If coercion is not possible, raise TypeError.

>>> coerce(1,2)
(1, 2)
>>> coerce(1.2,2l)
(1.2, 2.0)
>>> coerce(1.2,2)
(1.2, 2.0)
>>> coerce(1,2.3)
(1.0, 2.2999999999999998)
>>> coerce(1j,123)
(1j, (123+0j))

divmod():.divmod 整除求余、返回包含商和余数的元祖

>>> divmod(10,3)
(3, 1)
>>> divmod(3,10)
(0, 3)
>>> divmod(10,2.5)
(4.0, 0.0)

pow():power of a number:指数的意思

pow()与**都可以实现指数运算,pow()先出生些。

>>> pow(2,5)
32
>>> 2**5
32

round():四舍五入

round(...)
    round(number[, ndigits]) -> floating point number

Round a number to a given precision in decimal digits (default 0 digits).
    This always returns a floating point number.  Precision may be negative.

>>> round(1.234,2)
1.23
>>> round(3.14)
3.0
>>> for each in range(10):
         print round(math.pi,each)
     
3.0
3.1
3.14
3.142
3.1416
3.14159
3.141593
3.1415927
3.14159265
3.141592654

四、仅用于整数的函数

oct():octonary number system 八进制字符串形式

>>> oct(255)
'0377'

hex():hexadecimal number system十六进制字符串形式

>>> hex(255)
'0xff'

ASCII码转换函数

ord():ordinal:序数,将字符转换成对应整数值

>>> ord('A')
65

chr():char: 单个字符,数字对应当个ASCII字符

>>> chr(65)
'A'

五、操作符

>>> x>=80 and x<=100
True
>>> 80<=x<=100
True
-----------------------------
总是写错:
>>> 80=

六、致用

1、分数等级

def result(x):
   dic={9:'A',8:'B',7:'C',6:'D'}
   myre=int(x)/10
   for obj in sorted(dic.keys(),reverse=True):   #True 和False 表示是否逆序
       if myre>= obj:
           out=dic[obj]
           break
       else:
           out='F'
   return out
 
if __name__=="__main__":
    sorce = input('Enter your sorce:')

    print 'level:%s' %result(sorce) 

最新文章

  1. ASINetworkQueues(经典2)
  2. HDU 4122 Alice&#39;s mooncake shop --RMQ
  3. 使用supervisor提高nodejs调试效率 (已验证)
  4. DNS端口说明
  5. Perl小知识点之排序sort
  6. JQ 让光标在文本框最末尾
  7. phpcms V9 联动菜单的调用
  8. poj 3253 Fence Repair(模拟huffman树 + 优先队列)
  9. ZOJ 2967 Colorful Rainbows 【Stack】
  10. xCAT在多卡的物理机上装rhel6当需要人工选择网卡
  11. Node.js 逐行读取
  12. 12、Grafan 4.3升级到Grafana 5.0
  13. Linux_系统管理_网络配置_命令行配置网络
  14. Ajax学习笔记——基本原理
  15. apply、call、bind的区别
  16. Java&amp;Android TimeUtil ~ A Good Util!
  17. 一维码EAN 13简介及其解码实现(zxing-cpp)
  18. andriod 获得MP4时长
  19. cocos-lua基础学习(八)Layer类学习笔记
  20. 为什么重写equals方法时,要求必须重写hashCode方法?

热门文章

  1. Python 中练习题涉及到的无穷大和无穷小问题。
  2. fiddler抓包工具详解
  3. Linux两台机器简历信任
  4. vscode eslint插件对vue文件无效
  5. 并发基础篇(六):线程Thread类的start()方法和run()方法【转载】
  6. NIO 源码分析(01) NIO 最简用法
  7. 第一周 Largest Rectangle in a Histogram
  8. k小子串 SPOJ - SUBLEX 2
  9. tomcat启动内存修改
  10. centos7.x部署php7.0、mysql