一、Python基础数据类型

1.序列通用操作

a.索引
1
2
3
4
5
6
7
a = [1,2,3,4]
print(a[0])
print(a[-1])#倒数第一个元素

b = 'slfjsld'
print(b[0])
print(b[-1])

​ 运行结果:

1
2
3
4
1
4
s
d
b.分片
1
2
3
4
5
6
7
print(a[1:3])

print(b[3:-1]) #前闭后开

print(a[::2]) #第二个:后表示步长

print(b[6:2:-2]) #步长可以为负,表示从右往左取

​ 运行结果:

1
2
3
4
[2, 3]
jsl
[1, 3]
ds
c.列表相加
1
2
3
4
5
c = [5,6,7]
print(a+c)

d = 'hello'
print(d+b) #注意同一类型的数据才可以相加

​ 运行结果:

1
2
[1, 2, 3, 4, 5, 6, 7]
helloslfjsld
d.乘
1
print(3*a)

​ 运行结果:

1
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
e. in
1
print(4 in a)

​ 运行结果:

1
True

2.列表

a.index(返回元素第一次出现的索引)
1
2
3
4
5
6
7
8
nums = [1,3,5,7,9,3,5]

print(nums.index(3))


words = ["hello","world","nice","hello"]

print(words.index("hello"))

​ 运行结果:

1
2
1
0
b. count(统计元素出现次数)
1
2
3
4
5
print(nums.count(3))
print(nums.count(5))

print(words.count("nice"))
print(words.count("nihao"))

​ 运行结果:

1
2
3
4
2
2
1
0
c. append(添加新元素)
1
2
3
4
nums.append(7)
print(nums)
nums.append([0,0])
print(nums)

​ 运行结果:

1
2
[1, 3, 5, 7, 9, 3, 5, 7]
[1, 3, 5, 7, 9, 3, 5, 7, [0, 0]]
d. extend(将另一个列表的元素添加到该列表内)
1
2
words.extend(["meet"])
print(words)

​ 运行结果:

1
['hello', 'world', 'nice', 'hello', 'meet']
e. insert(指定的索引位置插入元素)
1
2
nums.insert(1,2)
print(nums)

​ 运行结果:

1
[1, 2, 3, 5, 7, 9, 3, 5, 7, [0, 0]]
f. pop(根据索引位置删除元素)
1
2
3
4
5
nums.pop()	#默认删除最后一个元素
print(nums)

nums.pop(1) #指定删除索引位置的元素
print(nums)

​ 运行结果:

1
2
[1, 2, 3, 5, 7, 9, 3, 5, 7]
[1, 3, 5, 7, 9, 3, 5, 7]
g. remove(删除第一次出现的这个元素)
1
2
nums.remove(3)
print(nums)

​ 运行结果:

1
[1, 5, 7, 9, 3, 5, 7]
h. reverse(列表反序)
1
2
nums.reverse()
print(nums)

​ 运行结果:

1
[7, 5, 3, 9, 7, 5, 1]
i. sort()与sorted(list)
1
2
3
4
5
6
7
8
nums.sort()
print(nums)

b = nums.sort()
print(b)

b = sorted(nums)
print(b)

​ 运行结果:

1
2
3
[1, 3, 5, 5, 7, 7, 9]
None
[1, 3, 5, 5, 7, 7, 9]

3.元组

a.元组是一种不可变的序列
1
2
3
4
5
a = (1,2,3)
print(a[0])

a[0] = 3

​ 运行结果:

1
2
3
4
5
6
7
8
9
1
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_191/828505032.py in <module>
2 print(a[0])
3
----> 4 a[0] = 3

TypeError: 'tuple' object does not support item assignment
b. 元组单一值初始化
1
2
3
4
5
6
a = (12,)
print(a)
print(type(a))

b = (12)
print(type(b))

​ 运行结果:

1
2
3
(12,)
<class 'tuple'>
<class 'int'>

4.字符串

a. find
1
2
3
4
5
str1 = "How are you. I am fine,and you?"

print(str1.find('you')) #普通查找
print(str1.find('you',15)) #从指定位置开始查找
print(str1.find('you',6,10)) #从指定区间查找

​ 运行结果:

1
2
3
8
27
-1
b. split
1
2
3
a = "www.baidu.com"
b = a.split(".")
print(b)

​ 运行结果:

1
['www', 'baidu', 'com']
c. join
1
2
3
a = ['www', 'baidu', 'com']
b = ".".join(a)
print(b)

​ 运行结果:

1
www.baidu.com
d. strip
1
2
3
a = "53156131546@163.com"
b = a.strip("@163.com")
print(b)

​ 运行结果:

1
5315613154
e. replace
1
2
3
a = "hello world"
b = a.replace("hello","Hello") #替换所有
print(b)

​ 运行结果:

1
Hello world

5.字典

a.字典初始化
1
2
3
4
5
6
7
8
9
10
11
12
a = {'name':"李华",'age':34}
print(type(a))
print(a['name'])

b = dict(name="小明",age=30)
print(type(b))
print(a['name'])

item = [('name',"张明"),('age',45)]
c= dict(item)
print(type(c))
print(c['name'])

​ 运行结果:

1
2
3
4
5
6
<class 'dict'>
李华
<class 'dict'>
李华
<class 'dict'>
张明
b. pop
1
2
3
4
5
a = {'name':"李华",'age':34}
for key in a:
print(key,a[key])
a.pop('name')
print(a)

​ 运行结果:

1
2
3
name 李华
age 34
{'age': 34}
c.字典排序
1
2
3
4
5
6
7
8
9
10
students = [
{'name': 'john', 'score': 'B', 'age': 15},
{'name': 'jane', 'score': 'A', 'age': 12},
{'name': 'dave', 'score': 'B', 'age': 10},
{'name': 'ethan', 'score': 'C', 'age': 20},
{'name': 'peter', 'score': 'B', 'age': 20},
{'name': 'mike', 'score': 'C', 'age': 16}
]

sorted(students,key = lambda stu:stu['age'])

​ 运行结果:

1
2
3
4
5
6
[{'name': 'dave', 'score': 'B', 'age': 10},
{'name': 'jane', 'score': 'A', 'age': 12},
{'name': 'john', 'score': 'B', 'age': 15},
{'name': 'mike', 'score': 'C', 'age': 16},
{'name': 'ethan', 'score': 'C', 'age': 20},
{'name': 'peter', 'score': 'B', 'age': 20}]

6.集合

a. 定义以及初始化
1
2
3
s1 = {'a', 'b', 'c', 'a', 'd', 'b'} 

set(s1)

​ 运行结果:

1
{'a', 'b', 'c', 'd'}
b.遍历
1
2
3
s2 = set('helloworld')
for a in s2:
print(a)

​ 运行结果:

1
2
3
4
5
6
7
o
w
h
l
e
r
d
c. add 、remove
1
2
3
4
5
s1 = {'a','b','c'}
s1.add("d")
print(s1)
s1.remove('d')
print(s1)

​ 运行结果:

1
2
{'a', 'd', 'b', 'c'}
{'a', 'b', 'c'}
d. 并集交集差集
1
2
3
4
5
6
7
8
9
10
s1 = {1,2,3,4}
s2 = {3,4,5}
s3 = s1 & s2
print(s3)

s4 = s1 | s2
print(s4)

s5 = s1 - s2
print(s5)

​ 运行结果:

1
2
3
{3, 4}
{1, 2, 3, 4, 5}
{1, 2}