python3中有六大标准的数据类型:
Number,String,List,Tuple,Sets,Dictionary
ps1:用type()函数进行显示变量的数据类型
代码如下:
a=5;
>>> print(type(a));
<class 'int'>
Number数:
python中有四种类型的数:整数,长整数,浮点数,复数
举例:
整数:2
长整数:大一些的整数
浮点数:3.23,52.3E-4 E表示10的幂,则52.3E-4表示52.3*10^-4
复数:(-5+4j)或者(2.3-4.6j)
String字符串:
字符串可以用单引号 or 双引号 or 三引号表示。ps:三引号:‘’‘ or """。同时三引号中可以自由使用单引号或者双引号
注意转义符号的使用
注意行末添加反斜杠表示:下一行继续而不是开始新的一行
举例:
>>> str="woshihaoren"
>>> print(str);#输出字符串
woshihaoren
>>> print(str[1:3]);#输出第二个字符到第三个字符
os
>>> print(str[1]);#输出第二个字符
o
>>> print(str[1:-1]);#输出第二个字符到 倒数第二个字符
oshihaore
>>> print(str+",nushuone~");#连接字符串
woshihaoren,nushuone~
>>>
List列表:
列表本质是数组,不过这个“数组”里面的元素的类型可以是不同的,也可以嵌套使用
举例:
>>> list=[123,"wo",456,"shi",789];
>>> tinyList=["hao",111,"ren"];
>>> print(list);#输出列表
[123, 'wo', 456, 'shi', 789]
>>> print(list[0]);
123
>>> print(list[1:3]);#输出第二个元素到第三个元素
['wo', 456]
>>> print(list[1:]);#输出从第二个元素开始的所有的元素
['wo', 456, 'shi', 789]
>>> print(list*2);#输出两次列表
[123, 'wo', 456, 'shi', 789, 123, 'wo', 456, 'shi', 789]
>>> print(list+tinyList);#两个列表相加
[123, 'wo', 456, 'shi', 789, 'hao', 111, 'ren']
>>>
Tuple元组
元组与列表类似,不同之处是元组的元素是不能修改的,定义用:(),而不是列表的:[]
举例:
>>> tuple=(123,"wo",456,"shi",789);
>>> tinyTuple=("hao",111,"ren");
>>> print(tuple);#输出完整的元组
(123, 'wo', 456, 'shi', 789)
>>> print(tuple[0]);#输出元组的第一个元素
123
>>> print(tuple[1:3]);#输出元组的第二个元素到第三个元素
('wo', 456)
>>> print(tuple[2:]);#输出元组的第二个元素到最后
(456, 'shi', 789)
>>> print(tuple*2);#输出两次元组
(123, 'wo', 456, 'shi', 789, 123, 'wo', 456, 'shi', 789)
>>> print(tuple+tinyTuple);#两个元组相加
(123, 'wo', 456, 'shi', 789, 'hao', 111, 'ren')
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
tuple[0]=1;
TypeError: 'tuple' object does not support item assignment
>>>
元组不能修改
Set集合
集合是一个无序的不重复的元素的序列
使用{} or set()函数创建集合
不过创建一个空的集合必须用set()函数进行创建
举例:
>>> name={"jack","tom","rose","jack","hanmeimei","lilei"};
>>> print(name);
{'jack', 'lilei', 'rose', 'hanmeimei', 'tom'}
if("rose" in name):
print("rose is in the name");
else:
print("rose is not in the name");
Dictionary字典
字典是另一种可变容器模型,且可存储任意类型对象。
字典的每个键值(key=>value)对用冒号(:)进行分割,每个对之间用逗号(,)进行分割,整个字典包含在花括号之间
举例:
>>> d={"001":"wo","002":"shi","003":"hao","004":"ren"};
>>> print(d);
{'004': 'ren', '001': 'wo', '003': 'hao', '002': 'shi'}
>>> print(d["001"])
wo
>>> del d["001"]
>>> print(d["001"])
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
print(d["001"])
KeyError: '001'
>>> print(d["002"])
shi
>>> d.clear();
>>> print(d["002"])
Traceback (most recent call last):
File "<pyshell#35>", line 1, in <module>
print(d["002"])
KeyError: '002'
>>> del d;
>>> print(d)
Traceback (most recent call last):
File "<pyshell#37>", line 1, in <module>
print(d)
NameError: name 'd' is not defined
存在内置函数,用符号“.”即可弹出,不过速度稍微慢点~