整型,浮点型以及字符串数据类型


前言:

在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'>
<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 = 'world '

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 

我们可以看到,字符串 * 整数 相当于复制了字符串整数次。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM