前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python基本类型

Python基本类型

作者头像
py3study
发布2020-01-06 23:00:01
8660
发布2020-01-06 23:00:01
举报
文章被收录于专栏:python3python3

     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

Operation

Result

Notes

x + y

sum of x and y

x - y

difference of x and y

x * y

product of x and y

x / y

quotient of x and y

(1)

x // y

(floored) quotient of x and y

(4)(5)

x % y

remainder of x / y

(4)

-x

x negated

+x

x 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 im. im 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 ** y

x to the power y

(7)

       整除/ :  结果总是整数,哪怕除数、被除数不是整数,而且结果总是趋向负无穷大,-1/2=-1

       0的0次幂:pow(0,0) =1, 0**0=1

       NaN: not a number  ,  INF:无穷大,-inf +inf  ,  float('nan') float('+inf') float('-inf')

       int(), long() 都是向下转型,对应实数int long float还可以用以下方式取舍:

Operation

Result

Notes

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即为True

      False: 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

Operation

Meaning

Notes

<

strictly less than

<=

less than or equal

>

strictly greater than

>=

greater than or equal

==

equal

!= 或 <>

not equal

(1)

is

object identity

is not

negated object identity

    bitwise operation: 位运算只对整数操作有意义,位运算优先级比数字运算符低,但比比较运算符高; ~与其他的一元运算符优先级(+,-)相同,以下表格中优先级从低到高, 负数移位会抛出ValueError异常

Operation

Result

Notes

x | y

bitwise or of x and y

x ^ y

bitwise exclusive or of x andy

x & y

bitwise and of x and y

x << n

x shifted left by n bits

(1)(2)

x >> n

x shifted right by n bits

(1)(3)

~x

the 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, replace

        trim:  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__   :显示文档

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档