python数字1 3怎么表示_python3基础01数值和字符串(一)

本篇系统梳理python3数值类型,数值计算;

字符串类型,字符串操作。

本文将了解什么?

1、数值数值数据类型

数值运算

数值运算举例

2、字符串单引号双引号区别

反斜杠 \ 转义符

输出原始字符串

三引号

操作字符串字符串修改

索引

乘法

拼接

in成员资格检查

遍历

求长度

1、数值python支持的数值数据类型。

数值类型 实例

整数(int) 0, -3

浮点数(float) 3.1415926, 0.3E+1, 3E-1

十六进制(hexadecimal) 0xAF(以0x开头,其后都是数字或者大小写字母)

八进制(octal) 0o10(以0o或0O开头,其后其后都是0-7之间的整数)

二进制(binary) 0b1011010010(以0b或0B开头,其后都是0、1)数值运算

常见数字运算类型及内置函数如下。

运算符 含义

+ (加)

- (减)

* (乘)

/ (除)

// (整除运算)

% (求余数)

** (幂)

int(x) (取x整数部分)

float(x) (将x转化为浮点数)

pow(x,y) (求x的y次幂)

abs(x) (求x的绝对值)

round(x) (x四舍五入)

bin(x) (将x转化为二进制)

oct(x) (将x转化为八进制)

hex(x) (将x转化为十六进制)

math.floor(x) (math模块中向下取整,floor中文有地板的意思)

math.ceil(x) (math模块中向上取整,ceil中文天花板的意思)数值运算举例

In [1]: #加

...: print(2 + 3)

5

In [2]: #减

...: print(2 - 3)

-1

In [3]: #乘

...: print(2 * 3)

6

In [4]: #除

...: print(2 / 3)

0.6666666666666666

In [5]: #整除运算,向下取整

...: print(1 // 3)

...: print(5.0 // 2.0)#输出结果类型与除数和被除数一致

...: print(5.0 // 2)

...: print(5 // 2.0)

...: print(-5 // 2)#向下(向负3)取整

0

2.0

2.0

2.0

-3

In [6]: #求余运算

...: print(5 % 2)

...: print(5.0 % 2.0)

...: print(5.0 % 2)

...: print(5 % 2.0)

1

1.0

1.0

1.0

In [7]: #求幂

...: print(2 ** 3)

...: print(-2 ** 2)#注意比较区别

...: print((-2) ** 2)

8

-4

4

In [8]: #取浮点数

...: print(float(2))

...:

...:

2.0

In [9]: #取整数

...: print(int(2.3))

2

In [10]: #取浮点数

...: print(float(2))

2.0

In [11]: #pow函数求幂

...: print(pow(2,3))

...: print(2 ** 3)#与上行等价

8

8

In [12]: #取绝对值

...: print(abs(-2))

2

In [13]: #四舍五入

...: print(round(2.3))

...: print(round(2.6))

2

3

In [14]: #转二进制

...: print(bin(2))

...:

...: #转八进制

...: print(oct(2))

...:

...: #转十六进制

...: print(hex(2))

0b10

0o2

0x2

In [15]: #向下取整和向上取整

...: import math

...: print(math.floor(3.5))#向下取整

...: print(math.ceil(3.5))#向上取整

3

4

2、字符串

字符串使用单引号,双引号或者三引号包围起来,例如,"hello, boy!",'hello, boy!','''hello,boy!'''。单引号双引号区别

In [16]: #该场景单引号和双引号作用一致

...: print("hello boy!")

...: print('hello girl!')

hello boy!

hello girl!

#以下场景双引号和单引号作用不一致

In [13]: print('hello,boy! Let's do it')

File "", line 1

print('hello,boy! Let's do it')

^

SyntaxError: invalid syntax反斜杠 \ 转义符

可添加转义字符使以上场景发挥相同作用。

In [15]: print('hello,boy! Let\'s do it')

hello,boy! Let's do it输出原始字符串

输出字符串例如,换行符(\n),tab分隔符(\t)等的原始字符串,而不是换行或者tab分割。

In [16]: #方法一,反斜线转义

...: #分别在特殊字符钱加反斜线转义

...: print("Hi\\tHello\\nboy!")

...:

...: #方法二,使用r

...: print(repr("Hi\tHello\nboy!"))

...: print(r"Hi\tHello\nboy!")

Hi\tHello\nboy!

Hi\tHello\nboy!三引号

三引号内的字符可自由换行。

In [26]: print("""Hello ...: , ...: boy!""")

Hello , boy!

三引号内可以随意使用双引号和单引号而不需要转义。

In [22]: print("""hello,boy! Let's d"o it""")

hello,boy! Let's d"o it

操作字符串

索引、切片、乘法、成员资格检查、长度、最小值和最大值都适用于字符串。字符串修改

字符串是不可变的,所有的元素赋值和切片赋值都是非法的;

强行修改字符串的值会报错。

In [128]: "Hello"[1:]="o"

---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

in

----> 1 "Hello"[1:]="o"

TypeError: 'str' object does not support item assignment索引

索引每次取出一个元素,python索引从0开始,可以为负数,最后一位索引为-1,倒数第二位索引为-2,依次类推。

In [6]: "Keepstudying"[0:3]#取出索引位为0到2的元素

Out[6]: 'Kee'

In [7]: "Keepstudying"[0:-2]#取出索引位为0到倒数第三个元素

Out[7]: 'Keepstudyi'

In [8]: "Keepstudying"[-4:-2]#取出索引位为-4到-3的元素

Out[8]: 'yi'

In [9]: "Keepstudying"[:3]#第一个参数可以省略

Out[9]: 'Kee'

In [10]: "Keepstudying"[:]#取出所有元素

Out[10]: 'Keepstudying'

In [11]: "Keepstudying"[1:]#第二个参数也可以省略

Out[11]: 'eepstudying'

In [13]: "Keepstudying"[1:5:2]#步长为2

Out[13]: 'ep'

In [14]: "Keepstudying"[::-1]#将字符颠倒

Out[14]: 'gniydutspeeK'乘法

str*n,重复str n次。

In [27]: print("Keepstudying\t" * 10)#重复10次

Keepstudying Keepstudying Keepstudying Keepstudying Keepstudying Keepstudying Keepstudying Keepstudying Keepstudying Keepstudying拼接

使用加号拼接。

In [27]: print("Hello,"+"world!")

Hello,world!in成员资格检查

使用in判断字符串是否包含某个子字符串,包含返回True,不包含返回False。

In [15]: "stu" in "Keepstudying"

Out[15]: True

In [16]: "stv" in "Keepstudying"

Out[16]: False遍历

依次输出字符串中每个元素。

In [18]: for i in "Keepstudying":

...: print(i)

K

e

e

p

s

t

u

d

y

i

n

g求长度

使用len函数,计算字符串个数,\t,\n算一个字符。

In [23]: len("Keepstudyin\t\ng")

Out[23]: 16

3、参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值