1、简介
1.1、python3 数据类型:
类型 |
含义 |
示例 |
int |
整型 |
1 |
float |
浮点型 |
1.0 |
bool |
布尔值 |
True或False |
complex |
复数 |
a+bj |
string |
字符串 |
‘abc123’ |
list |
列表 |
[a,b,c] |
tuple |
元组 |
(a,b,c) |
set |
集合 |
{a,b,c} |
dictionary |
字典 |
{a:b,c:d} |
1.2、备注说明
类型 |
说明 |
complex |
复数的虚数部分不能省略 |
string |
字符串不能包括有 ‘\’ ,否则输出的不是原来的字符串 |
list和tuple |
list可以修改元素,tuple不能,但是tuple可以包括list等多种数据类型,占用资源多于list |
set |
没有排列的顺序及不会有重复的元素 |
dictionary |
一个键对应多个值,但是不能有相同的键 |
2、示例
2.1、int
A=1
print('A的类型为:',type(A))
>>>A的类型为: <class 'int'>
2.2、float
B=1.0
print('B的类型为:',type(B))
>>>B的类型为: <class 'float'>
2.3、bool
C=True
print('C的类型为:',type(C))
>>>C的类型为: <class 'bool'>
2.4、complex
D=1+1j
print('D的类型为:',type(D))
>>>D的类型为: <class 'complex'>
2.5、string
E='abc123'
F='\abc123'
print('E的类型为:',type(E))
print('E输出为:',E,'\nF输出为:',F)
>>>E的类型为: <class 'str'>
F的类型为: <class 'str'>
E输出为: abc123
F输出为: bc123
2.5、list
G=[A,B,C,D,E]
print('G的类型为:',type(G))
print('列表G在元素更改前为:',G)
print('列表G的第一个元素为:',G[0])
G[0]=100
print('列表G在元素更改后为:',G)
>>>G的类型为: <class 'list'>
列表G在元素更改前为: [1, 1.0, True, (1+1j), 'abc123']
列表G的第一个元素为: 1
列表G在元素更改后为:[100, 1.0, True, (1+1j), 'abc123']
2.6、tuple
H=(A,B,C,D,E,F,G)
print('元组H为:',H)
print('H的类型为:',type(H))
>>>元组H为: (1, 1.0, True, (1+1j), 'abc123', '\x07bc123', [100, 1.0, True, (1+1j), 'abc123'])
H的类型为: <class