整型,浮点型以及字符串数据类型
前⾔:
在python中,数据类型有:整型,浮点型,字符串,列表,元组,字典,集合,布尔型。今天要介绍的是整型,浮点型以及字符串类型。
⼀、整型:
整数类型即是整型,哈哈哈,这不是废话吗?
整型-1, -1, 0, 2, 99
a = 10
b = -10
c = 0
print(a, b, c)
print(type(a))
print(type(b))
print(type(c))
执⾏结果:
10 -10 0
<class'int'>
<class'int'>float型
<class'int'>
Process finished with exit code 0
我们可以看到,10, -10, 0 都是整型,都是 int 类型。
⼆、浮点型:
有⼩数的数据类型,称为浮点型。
浮点型-1.25, 0.0, 1.2, 3.90
a = 10.1
b = -10.0
c = 0.0
print(a, b, c)
print(type(a))
print(type(b))
print(type(c))
执⾏结果:
10.1 -10.0 0.0
<class'float'>
<class'float'>
<class'float'>
Process finished with exit code 0
我们可以看到,10.1 ,0.0, -10.0 这些带⼩数点的,都属于 float 型。
三、字符串类型:
在python中,字符串类型由引号括起来。
字符串类型'hello','python', 'java'
a = 'hello'
b = 'j'
c = 'worl
d '
print(a, b, c)
print(type(a))
print(type(b))
print(type(c))
执⾏结果:
hello j world
<class'str'>
<class'str'>
<class'str'>
Process finished with exit code 0
我们可以看到,只要是杯引号括起来的,都是 str 类型。
四、表达式:
表达式是由值与操作符的组合
五、字符串的连接与复制:
对于字符串, + 号运输符,可以将两个字符串进⾏拼接。
a = 'hello'
b = ' world '
c = a + b
print(c)
执⾏结果:
hello world
我们可以看到 + 号对于数字来说,是数字运算,⽽对于字符串来说,是拼接。
b = ' world '
n = 3
print(b*n)
执⾏结果:
world  world  world
我们可以看到,字符串 * 整数相当于复制了字符串整数次。