博客
关于我
Python学习(一)——数据类型、输入输出
阅读量:135 次
发布时间:2019-02-27

本文共 1229 字,大约阅读时间需要 4 分钟。

数据类型

1. 整数和布尔值

整数和长整型在Python中并不严格区分,整型值后加“L”即可表示长整型。大小任意,正负皆可。布尔值有两种:TrueFalse,区分大小写,分别对应1和0。

>>> type(3L)
>>> type(3)
>>> print(0xa)10>>> type(0xa)

布尔运算包括andornot等。

2. 浮点数

浮点数包括普通小数(如12.3、0.123、-1.23)和科学计数法(如1.23e-5)。注意整数相除与浮点数相除的区别。

>>> type(3.0)
>>> print(1.23e-5)1.23e-05>>> print(1.2e3)1200.0

整数相除与浮点数相除的区别:

>>> 9 / 240.375>>> 9 / 2.044.5>>> 9 // 2.044.0

可以通过导入__future__模块改变计算方式:

>>> from __future__ import division>>> 5 / 22.52.0>>> 9 / 42.250.212

3. 复数

复数可以表示为a + bj,其中a为实部,b为虚部。可以通过conjugate获得共轭复数。

>>> 1.2 + 3.4j(1.2+3.4j)>>> 1.2j + 3.4(3.4+1.2j)>>> f = 1.2 + 3.4j>>> f.real1.2>>> f.imag3.4>>> f.conjugate()(1.2-3.4j)

4. 字符串

字符串可以用单引号、双引号或三引号括起来。三引号支持多行字符串。内部单引号和双引号可以使用转义字符\\表示。

>>> print("\t\t\tabc")     abc>>> print(r"\t\t\tabc")\t\t\tabc>>> print('\n\n\\abc')\n\n\\abc>>> print(r'\n\n\\abc')\n\n\\abc

字符串操作符包括u(转换为Unicode字符串)和r(Raw字符串,内部不转义)。

>>> print("inpu string: abcdef")inpu string: abcdef

字符串中的特殊字符如水平制表符\t、垂直制表符\\v等都有对应的转义字符。

>>> print("She is {0} ,class:{1}, age:{2}".format("Marry",3,16))She is Marry ,class:3, age:16

5. 空值

None是一个特殊值,不可用0表示。变量命名规则为大小写英文、数字和_的组合,且不能用数字开头。变量可以反复赋不同类型的值。

n = int(raw_input('n='))if n > 10:    print(n, '>10')else:    print(n, '<10')

转载地址:http://fhnd.baihongyu.com/

你可能感兴趣的文章
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>
NPM 2FA双重认证的设置方法
查看>>
npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
查看>>
npm build报错Cannot find module ‘webpack‘解决方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm ERR! fatal: unable to connect to github.com:
查看>>
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 Failed to connect to github.com port 443 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>