转自 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) 

《转》python数据类型的更多相关文章

  1. python 数据类型---布尔型& 字符串

    python数据类型-----布尔型 真或假=>1或0 >>> 1==True True >>> 0==False True python 数据类型----- ...

  2. Python 数据类型及其用法

    本文总结一下Python中用到的各种数据类型,以及如何使用可以使得我们的代码变得简洁. 基本结构 我们首先要看的是几乎任何语言都具有的数据类型,包括字符串.整型.浮点型以及布尔类型.这些基本数据类型组 ...

  3. day01-day04总结- Python 数据类型及其用法

    Python 数据类型及其用法: 本文总结一下Python中用到的各种数据类型,以及如何使用可以使得我们的代码变得简洁. 基本结构 我们首先要看的是几乎任何语言都具有的数据类型,包括字符串.整型.浮点 ...

  4. Python数据类型及其方法详解

    Python数据类型及其方法详解 我们在学习编程语言的时候,都会遇到数据类型,这种看着很基础也不显眼的东西,却是很重要,本文介绍了python的数据类型,并就每种数据类型的方法作出了详细的描述,可供知 ...

  5. Python学习笔记(五)--Python数据类型-数字及字符串

    Python数据类型:123和'123'一样吗?>>> 123=='123'False>>> type(123)<type 'int'>>> ...

  6. python数据类型之元组、字典、集合

    python数据类型元组.字典.集合 元组 python的元组与列表类似,不同的是元组是不可变的数据类型.元组使用小括号,列表使用方括号.当元组里只有一个元素是必须要加逗号: >>> ...

  7. 1 Python数据类型--

    常见的Python数据类型: (1)数值类型:就是平时处理的数字(整数.浮点数) (2)序列类型:有一系列的对象并排或者排列的情况.如字符串(str),列表(list),元组(tuple)等 (3)集 ...

  8. Python数据类型和数据操作

    python数据类型有:int,float,string,boolean类型.其中string类型是不可变变量,用string定义的变量称为不可变变量,该变量的值不能修改. 下面介绍python中的l ...

  9. Python数据类型(python3)

    Python数据类型(python3) 基础数据类型 整型 <class 'int'> 带符号的,根据机器字长32位和64位表示的范围不相同,分别是: -2^31 - 2^31-1 和 - ...

  10. 二、Python数据类型(一)

    一.Python的基本输入与输出语句 (一)输出语句 print() 示例: print('你好,Python') print(4+5) a = 10 print(a) 输出的内容可以是字符串,变量, ...

随机推荐

  1. 排序算法(一) 插入排序及Java实现

    代码实现: public void insertionSort(List<T> list, Comparator<T> comparator) { for (int i=1; ...

  2. 剑指offer---1、顺时针打印矩阵

    剑指offer---1.顺时针打印矩阵 一.总结 一句话总结: 谋而后动+多做:还是要谋而后动,但是怎么谋而后动,很有学问,做好的方式就是多做 问题就这些问题:解决了就好了,比如php多维数组 面试的 ...

  3. python中的缓存技术

    python缓存技术 def console(a,b): print('进入函数') return (a,b) print(console(3,'a')) print(console(2,'b')) ...

  4. python 一些特殊用法和坑

    https://github.com/leisurelicht/wtfpython-cn

  5. jeecg 实现lhgDialog窗口传值

    需要在jeecg中的dialog弹框往调用的窗口赋值. 定义内容页调用窗体实例对象接口 var windowapi = frameElement.api ; var W = windowapi.ope ...

  6. CSS实现背景图片固定

    body { background-image:url('bg.jpg'); background-repeat: no-repeat; background-attachment: fixed; / ...

  7. 2019 牛客多校第六场 D Move

    题目链接:https://ac.nowcoder.com/acm/contest/886/D 题解摘自官方题解 题目大意 有 K 个体积相同的箱子,有 N 个体积相同或相异的物品,现要按照如下策略装箱 ...

  8. PostgreSQL——启动脚本

    <仅供参考,执行結果受环境影响> 如下: pgpath='/usr/local/pgsql/bin' pgdata='/usr/local/pgsql/data' #以 postgres ...

  9. MySQL 8.0及以上版本设置密码

    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

  10. C# String类常用操作

    1.string.Compare(s1,s2) 比较  按字母顺序后边的大于前边的 s1<s2 retrun -1 ;s1=s2 return 0; s1>s2 return1. Cons ...