Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Python基本类型

Python是一门动态语言,解释执行,所有错误都是运行时产生的,即使有错误和异常,只要没有被执行到也不会有错,比如调用不存在的方法;类型是隐式的,也即无需变量类型声明;类型是动态,运行时根据变量指向的内容来决定类型,但是Python是强类型语言,即每个变量都是有类型的。Python 基本built-in类型主要有numerics,sequences, mapping, files, classes, instances, exceptions,类型上都会存在的操作有比较、是否为真、转换为字符串toString,Python中使用str/repr(object)可转换为字符串, print(object)时会隐式调用str()。numerics:整形 int,用c语言中的long实现, 取值范围-sys.maxint-1~sys.maxin, 无sys.minint长整形 long, 带有L/l的integer或超出integer范围的,print时会带后缀L,无精度限制,无限大,因此Python中都是有符号数,没有unsigned类型浮点型 float,用c中的double实现,sys.float_info,  因此Python中无单双精度区分复数 complex, z.real, z.imag
OperationResultNotes
x + ysum of x and y 
x - ydifference of x and y 
x * yproduct of x and y 
x / yquotient of x and y(1)
x // y(floored) quotient of x and y(4)(5)
x % yremainder of x / y(4)
-xx negated 
+xx unchanged 
abs(x)absolute value or magnitude of x(3)
int(x)x converted to integer(2)
long(x)x converted to long integer(2)
float(x)x converted to floating point(6)
complex(re,im)a complex number with real part re, imaginary part imim defaults to zero. 
c.conjugate()conjugate of the complex number c. (Identity on real numbers) 
divmod(x, y)the pair (x // y, x % y)(3)(4)
pow(x, y)x to the power y(3)(7)
x ** yx to the power y(7)

不同类型的numerics可以混合运算,遵循规则类似c,也即小范围向大范围转型,int<long<float<complex整除/ :  结果总是整数,哪怕除数、被除数不是整数,而且结果总是趋向负无穷大,-1/2=-10的0次幂:pow(0,0) =1, 0**0=1NaN: not a number  ,  INF:无穷大,-inf +inf  ,  float("nan") float("+inf") float("-inf")int(), long() 都是向下转型,对应实数int long float还可以用以下方式取舍:
OperationResultNotes
math.trunc(x)x truncated to Integral 
round(x[, n])x rounded to n digits, rounding ties away from zero. If n is omitted, it defaults to 0. 四舍五入
math.floor(x)the greatest integral float <= x 
math.ceil(x)the least integral float >= x 
bool布尔:用于if/while后做条件判断True:非False即为TrueFalse: None, False, 数字类型0,空容器,包括空字符串‘’, class的__nonzero__() 或__len__返回0或False的实例bool运算符:or and not, 遵循类似java/c的short-circuit, not比non-Boolean operator优先级低,not a==b 等价于not (a==b)比较运算符: 也用于所有类型的比较,优先级比Boolean operator高,且支持x<y<z这样的写法,x<y<z 等价x<y and y < z 且前者y仅计算一次,都遵循短路原则;不同类型的对象比较结果都是False,除非是不同类型数字或字符串比较,比如0==0L, ‘abc’==u"abc"返回True
OperationMeaningNotes
<strictly less than 
<=less than or equal 
>strictly greater than 
>=greater than or equal 
==equal 
!= 或 <>not equal(1)
isobject identity 
is notnegated object identity 
bitwise operation: 位运算只对整数操作有意义,位运算优先级比数字运算符低,但比比较运算符高; ~与其他的一元运算符优先级(+,-)相同,以下表格中优先级从低到高, 负数移位会抛出ValueError异常
OperationResultNotes
x | ybitwise or of x and y 
x ^ ybitwise exclusive or of x andy 
x & ybitwise and of x and y 
x << nx shifted left by n bits(1)(2)
x >> nx shifted right by n bits(1)(3)
~xthe bits of x inverted 
int.bit_length():获取int bit表示长度long.bit_length():获取long bit表示长度字符:长度为1的字符串,也即没有单个字符字符串: 单引号"abc" 或双引号""abc" 或三个连续单/双引号"""表示多行字符串,字符串可理解为常量字节数组或字节容器,类似Java中String,也不能通过变量改变指向的字符串, s="abc"; id(s) == id("abc")。字符串上常用操作:长度:容器统一用len(),子串:容器分片操作符[]    "abcd"[1:3]="bc"分隔:split/rsplit查找/替换:find/rfind 没找到返回-1; index/rindex没找到抛ValueError, replacetrim:  strip/lstrip/rstrip编/解码:只能对str解码 str("汉").decode("UTF-8"), 只能对Unicode编码 u("汉").encode("UTF-8")大小写转换: lower/uper判断:isalnum/isalpha/isdigit/islower/isspace/isupper/startwith/endwith格式化: %+tuple/dict,类似c语言sprintf,一个参数"%d" % 1 = "1"  ; 两个参数"%s, %s" %("a","b") = "a,b"; 指定占位符%(mapping key)+类型字符,mapping key需加括号"%(key1)s, %(key2)d" %{"key1":"a", "key2":1}="a,1"Python中很容易获取帮助:help(object):显示帮助信息dir(object) :显示所有方法object.__doc__ :显示文档《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htmPython脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm在Ubuntu下用Python搭建桌面算法交易研究环境 http://www.linuxidc.com/Linux/2013-11/92534.htmPython 的详细介绍:请点这里
Python 的下载地址:请点这里本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-09/106759.htm