Python数据类型:数值、字符串、列表、元组、集合、字典。
【数值类型】
数值类型有:整型(int)、浮点型(float)、复数(complex)
a = 4
b =3.14159
c = 1 + 2j
d = complex(1,2)
【字符串】
字符串是以单引号或双引号括起来的文本,如‘abc’、"xyz"等。
a = "hello"
b = 'world'
- 注意包含单引号的字符串,如I’m a student 。
str1 = 'I'm a student.' # 错误,会将I后面的单引号看作返回单引号
# 在字符串里面存在单引号时,使用转义字符\,表示将\后的字符看着普通字符。
str2 = 'I\'m a student.'
- 包含双引号的字符串,如Never forget to say “thanks”。
str1 = "Never forget to say "thanks"" # 错误,会将thanks前面的双引号看作返回双引号
# 在字符串里面存在双引号时,使用转义字符\,表示将\后的字符看着普通字符。
str2 = "Never forget to say \"thanks\""
如果不使用转义字符""会出现如下错误。
- 字符串的访问,使用索引,索引从0开始,0代表第一个, -1代表尾部最后一个
>>> a = "hello"
>>> a[0]
'h'
>>> a[-1]
'o'
>>> a[5] #索引越界了
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>>
- 字符串的运算:连接、重复、长度计算、截取部分字符串等。
>>> a = "hello"
>>> b = " world"
# 字符串的连接,使用“+”
>>> c = a+b
>>> print(c)
hello world
# 字符串的重复
>>> d = a*2
>>> d
'hellohello'
# 计算字符串的长度
>>> len(a)
5
>>> len(d)
10
# 截取字符串的一部分
>>> e = d[3:9]
>>> e
'lohell'
【列表】
写在“[ ]”之间、用逗号隔开的元素列表,列表的数据项不需要具有相同的类型,与字符串的索引一样,列表索引从0开始。
>>> list1 = [90,79,80,88,82,77,85,69,75,98]
>>> list2 = ['hello','world',100,'yes']
>>> list1
[90, 79, 80, 88, 82, 77, 85, 69, 75, 98]
>>> list2
['hello', 'world', 100, 'yes']
# 访问数据时与字符串类似,使用索引,从0开始,可以截取部分
>>> list1[0]
90
# 截取结果为左闭右开,如下包括索引2,不包括索引5
>>> list1[2:5]
[80, 88, 82]
- 列表运算:索引、连接、重复、修改、新增、删除等操作。
>>> a = [1,2,3,4,5,6]
>>> b = ['a','b','c','d','e','f']
# 访问索引,从0开始
>>> a[3]
4
# 连接,用“+”实现,注意顺序
>>> c = a + b
>>> c
[1, 2, 3, 4, 5, 6, 'a', 'b', 'c', 'd', 'e', 'f']
>>> d = b+a
>>> d
['a', 'b', 'c', 'd', 'e', 'f', 1, 2, 3, 4, 5, 6]
# 重复,重复n次,用“*”n
>>> e =a*2
>>> e
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
>>> a
[1, 2, 3, 4, 5, 6]
# 修改,利用索引修改,如修改a中的第一个元素。
>>> a[0] = 'start'
>>> a
['start', 2, 3, 4, 5, 6]
# 新增
'''
新增有两种方式:
(1)使用append(obj)方式:在列表末尾新增元素obj,每次只能新增一个元素;
(2)使用insert(index,obj)方式:在列表索引为index的位置新增元素obj,
每次只能增加一个元素;
(3)使用extend(seq)方式:在列表末尾新增元素序列seq,这里可以新增多个元素
'''
#append()新增,当增加两个元素报错,
>>> a.append(7,8)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list.append() takes exactly one argument (2 given)
>>> a.append(7)
>>> a
['start', 2, 3, 4, 5, 6, 7]
# insert(index,j),当索引为 1 的位置增加元素"learning"
>>> a.insert(1,"learning")
>>> a
['start', 'learning', 2, 3, 4, 5, 6, 7]
# extend(seq),新增一个列表在里面
>>> a.extend([8,9])
>>> a
['start', 'learning', 2, 3, 4, 5, 6, 7, 8, 9]
# * 注意区分append(obj)一个序列时的结果
>>> a.append([8,9])
>>> a
['start', 'learning', 2, 3, 4, 5, 6, 7, 8, 9, [8, 9]]
'''
删除有两个方式:
(1)pop([index]):删除索引为index的列表元素,
如果没有index,index=-1,即pop()是默认删除列表的最后一个元素;
(2)remove(obj):删除列表中的某个值
'''
>>> a
['start', 'learning', 2, 3, 4, 5, 6, 7, 8, 9, [8, 9]]
# index没有,默认为-1,删除最后一个元素
>>> a.pop()
[8, 9]
>>> a
['start', 'learning', 2, 3, 4, 5, 6, 7, 8, 9]
#pop(index),index=2,表示删除索引为2的列表元素2
>>> a.pop(2)
2
>>> a
['start', 'learning', 3, 4, 5, 6, 7, 8, 9]
# remove(obj)删除列表中值为obj的元素
>>> a.remove(4)
>>> a
['start', 'learning', 3, 5, 6, 7, 8, 9]
'''
值得注意的是:remove(obj)删除时是从左到右,删除列表中的第一个obj元素
如下面我先给列表添加 4,5,2,3 四个元素,然后列表中有两个3,
这时我删除 3 ,可发现只删除了前面的元素 3.
'''
>>> a.extend([4,5,2,3])
>>> a
['start', 'learning', 3, 5, 6, 7, 8, 9, 4, 5, 2, 3]
>>> a.remove(3)
>>> a
['start', 'learning', 5, 6, 7, 8, 9, 4, 5, 2, 3]
【元组】
元组与列表相似,不同之处在于元组列表不能修改。元组使用小括号( ),元素之间用逗号隔开。
>>> tup1 = ("good","happy",1997,2022)
>>> tup2 = (1,2,3,4,5,6)
>>> tup1
('good', 'happy', 1997, 2022)
>>> tup2
(1, 2, 3, 4, 5, 6)
- 元组运算:索引、连接、元素个数
>>> tup1
('good', 'happy', 1997, 2022)
>>> tup2
(1, 2, 3, 4, 5, 6)
# 索引访问元组元素,与列表相似,索引从0开始。
>>> tup1[1]
'happy'
# 类似,左闭右开区间
>>> tup2[1:4]
(2, 3, 4)
# 元组连接,用“+”,注意顺序
>>> tup3 = tup1 + tup2
>>> tup3
('good', 'happy', 1997, 2022, 1, 2, 3, 4, 5, 6)
>>> tup4 = tup2 + tup1
>>> tup4
(1, 2, 3, 4, 5, 6, 'good', 'happy', 1997, 2022)
# 元素个数用len()函数
>>> len(tup4)
10
【集合】
集合是一个无序不重复序列。基本功能是进行成员关系测试和删除重复元素。使用大括号或者set()函数创建集合。
创建一个空集合必须用 set() 而不是 { }。因为 { } 是用来创建一个空字典。
# 集合是无序的
>>> x = {10,20,30,40,50,60,70,80}
>>> x
{70, 40, 10, 80, 50, 20, 60, 30}
# 集合是不重复的
>>> z = {1,1,1,1,1,2,3}
>>> z
{1, 2, 3}
- 集合的运算:集合间的运算、增加、删除
>>> a = {10,20,'hello','world'}
>>> b = {40,20,50,'h','e'}
# 集合a和b中都包含了的元素
>>> a & b
{20}
# 不同时包含于a和b的元素
>>> a ^ b
{50, 'e', 'world', 40, 10, 'hello', 'h'}
# 集合a或者b包含的元素
>>> a | b
{'world', 40, 10, 50, 'e', 20, 'hello', 'h'}
# 集合a中包含而在集合b中包含的元素
>>> a - b
{10, 'hello', 'world'}
# 集合b中包含而在集合a中包含的元素
>>> b - a
{40, 50, 'e', 'h'}
>>> a
{10, 'hello', 20, 'world'}
# 增加,使用add()函数
>>> a.add(30)
>>> a
{'world', 10, 20, 'hello', 30}
'''
删除,使用remove(x)函数,
x 如果在集合中,则从集合中删除;
x 如果不在集合中,会发生错误。
'''
>>> a.remove(10)
>>> a
{'world', 20, 'hello', 30}
#
>>> a.remove(10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 10
注意:集合的取值是不能通过下标索引来取值和赋值的。
【字典】
字典是无序的对象集合,建立一个空字典,{ }。
键值对的方式{ key:value }
>>> dict0 = {}
>>> dict0
{}
>>> dict1 = {"name":"xiaoming","age":16,"addr":"shenzhen","grade":"高一某班"}
>>> dict1
{'name': 'xiaoming', 'age': 16, 'addr': 'shenzhen', 'grade': '高一某班'}
>>> dict1["name"]
'xiaoming'
- 字典运算:访问、修改、删除、
>>> dict1
{'name': 'xiaoming', 'age': 16, 'addr': 'shenzhen', 'grade': '高一某班'}
# 访问元素
>>> dict1['name']
'xiaoming'
'''
修改元素:访问的键值
如果键值没有,即添加内容
'''
# 修改键值
>>> dict1['name'] = 'xiaohong'
>>> dict1
{'name': 'xiaohong', 'age': 16, 'addr': 'shenzhen', 'grade': '高一某班'}
# 添加,班级class在dict1中不存在,即为添加
>>> dict1['class']=10
>>> dict1
{'name': 'xiaohong', 'age': 16, 'addr': 'shenzhen', 'grade': '高一某班', 'class': 10}
'''
删除元素:使用del关键字或者使用pop(key)函数
'''
# 删除某一个key:value
>>> del dict1['addr']
>>> dict1
{'name': 'xiaohong', 'age': 16, 'grade': '高一某班', 'class': 10}
# 删除pop()函数
>>> dict1.pop('grade')
'高一某班'
>>> dict1
{'name': 'xiaohong', 'age': 16, 'class': 10}
# 清空字典内容
>>> dict1.clear()
>>> dict1
{}
# 删除字典
>>> del dict1
>>> dict1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'dict1' is not defined