python基础数据结构

python基础复习

chapter1 变量和数据类型

变量

变量命名:只能包含字母,数字,下划线,开头不能使用下划线。
注解:不要用python的关键字和函数命名变量名

数据类型

1. 字符串

在python中,所有单引号’ ‘或者" "括起来的都是字符串
例如:"This is a python review blog"或者’This is a python review blog’

操作字符串
  1. 大小写
name = "ljh love wyq"
print(name)
结果:ljh love wyq
title() 函数:以首字母大写展示每个词
print(name.title())
结果:Ljh Love Wyq
upper()函数:大写,lower()函数:小写
print(name.upper())
print(name.lower())
结果:
LJH LOVE WYQ
ljh love wyq

2.合并(拼接)字符串

name1 = ljh
name2 = wyq
v = love
final = name1 + v + name2
final2 = name1 + " " + " " + name2
print(final)
结果:
ljhlovewyq
ljh love wyq
  1. 制表符和换行符
    制表符号 \t 相当于四个空格
print("ljh")
print("\tljh")
结果:
ljh
	ljh

换行符号 \n

print("ljh\nlove\nwyq")
结果:
ljh
love
wyq
  1. 删除空白部分
在python中
'ljh ''ljh' 看起来没有什么区别,但是这是两个完全不一样的字符串
strip()函数可以把字符串里面的空格去掉
lstrip()去除字符串左边的空格
rstrip()去除字符串右边的空格
s = 'ljh '
print(s.rstrip())
结果:
'ljh'
  1. 注意事项
    如果字符串中有 ’ 那么字符串要用 " "括起来
2. 数字

int 型
float 型

chapter2 python数据结构

1.集合(set)

集合定义:
python集合是多个元素的无序组合
集合之间元素无序,每个元素唯一,不存在相同元素
集合元素不可更改,不能是可变数据类型
集合用大括号表示 {},元素之间用逗号隔开
建立集合类型用{}或者set()
建立空集合类型,必须使用set()

使用{}建立集合
ljh = {"smart","handsome","good at basketball"}
print(ljh)
结果:
{'smart','handsome','good at baskteball'}
使用set()建立集合
wyq = set("I love you")
print(wyq)
结果:
{'l', 'u', 'v', 'o', 'y', ' ', 'I', 'e'}
注:集合出现了两次o和两次空格,但是在结果中只有一次o和一次空格,这与集合内元素不同有关
集合的操作和应用
集合操作

A = set("12345")
B = set("45678")


求并集
C = A|B
或者 
C = A.union(B)
print(C) # 把A集合和B集合中的元素全部取出来
结果: {'2', '6', '4', '7', '5', '8', '1', '3'}


求交集
D = A&B
或者
D = A.intersection(B)
print(D)  # 把A集合和B集合共有的元素提取出来
结果:{'5', '4'}


求差集
E = A - B
或者
E = A.difference(B)
print(E) # 把存在集合A,但是不存在集合B的元素提取出来
结果: {'2', '3', '1'}


求补集
F = A ^ B
print(F)  # 把A集合中
结果: 
{'2', '6', '1', '7', '8', '3'}
处理集合
add()操作
A = set("123")
A.add("4") # 把"4"加入到集合中
print(A)
结果:{'2', '3', '4', '1'}

pop()操作
print(A)
A.pop() # 默认删除第一个元素,并且返回出来 
print(A.pop()) 
print(A)
结果:
{'1', '2', '4', '3'}
1
{'2', '4', '3'}

remove()操作
A.remove(2) # 从集合里面删除指定元素,元素不存在会报错
discard()操作
A.discard(2) # 从集合里删除指定元素,元素不存在不会报错

2.元组(tuple)

1. 元组的定义和性质

元组本质上是一种有序的集合,和列表非常的相似,列表使用 [ ] 表示,元组使用 () 表示
特点:一旦初始化,就不能发生改变

2. 元组的创建

格式:
元组名 = (元素1, 元素2, 元素3, …)

# 创建元组
tuple1 = ("handsome","cool",2000,2002)
tuple2 = (1,2,3,4,5)
tuple3 = "a","b","c","d","e"    # 不用括号也可以创建元组
tuple4 = ()    # 空元组 
tuple = (2002,)   # 元组只有1个元素的时候后面需要加逗号
3. 元组的修改和运算

元组的元素值是不允许修改的,但是可以对元组进行拼接组合。

tuple1 = ("handsome","cool",2000,2002)
tuple2 = (1,2,3,4,5)
tuple3 = tuple1 + tuple2
print(tuple3)
output : ('handsome', 'cool', 2000, 2002, 1, 2, 3, 4, 5)

# 获取元组长度
print(len(tuple3))
output : 9

# 元组的复制
tuple4 = tuple1*2
print(tuple4)
output: ('handsome', 'cool', 2000, 2002, 'handsome', 'cool', 2000, 2002)

# 元组的遍历
for i in tuple1:
	print(i)

output:
handsome
cool
2000
2002
4.元组的删除

元组中的元素不允许直接删除,我们只能使用del 语句来删除整个元组

tuple1 = ("handsome","cool",2000,2002)
del tuple1
5.元组的内置函数
tuple1 = ("handsome","cool",2000,2002)
tuple2 = (1,2,3,4,5)
# 元组长度
len(tuple1)
# 元组最大/最小值
max(tuple2)
min(tuple2)
# 将列表转换为元组
list1 = ['q','w','e','r']
tuple3 = tuple(list1)
print(tuple3)
output:  ('q', 'w', 'e', 'r')

3.列表(list)

1.列表定义

列表是由一系列按照特定顺序排列的元素的组成。元素可以是字符串,字母,数字,等等。
列表用 [ ]来表示,用逗号分隔其中的元素。

如:
list1 = [1,2,3,4]
list2 = ['ljhsg','wyqmn','sbwbh','sbgpx']
2.访问列表
# 访问列表的第i个元素
list2[i-1] # 从0开始的

# 修改列表元素
# 直接修改
list1 = [1,2,3,4,5]
list1[2] = 5
print(list1)
output: [1,2,5,4,5]
# 添加列表元素
# 1,在列表尾部增加元素 append
list1.append(6)
print(list1)
output: [1,2,5,4,5,6]
# 在列表中出入一个元素
list1.insert(3,5) # 在列表的第4个位置插入5
print(list1)
output:[1,2,5,5,4,5,6]


# 删除列表元素
# 1.del 语句删除
del list1[0]   # 删除list1里面的第一个元素
# 2.pop()语句删除
list1.pop()  #删除list1最后面的元素并且返回出来
list1.pop(i) #删除list1第i+1个元素并且返回出来 
# 3.删除值remove()
list1.remove(1)
删除list1中所有值为1的元素,不需要值的位置。

3. 组织列表
1. sort()函数

sort(reverse = False) 表示不用逆序
sort(reverse = True) 表示按字母表逆序
sort()函数不保留原来的排序,永久的修改了元素的排列顺序,对列表的排序是永久性的。

names = ['ljh','wyq','sbwbh','sbgpx']
# sort()排序
names.sort(reverse = False)
print(names)
output: ['ljh', 'sbgpx', 'sbwbh', 'wyq']
names.sort(reverse = True)
print(names)
output: ['wyq', 'sbwbh', 'sbgpx', 'ljh']
2. sorted()函数

sorted() 函数和sort函数一样能排序,但是不同的是sorted()函数保留了原来的列表的排列顺序
比如:

cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)
print(sorted(cars))
print(sorted(cars,reverse = True))
output:
['bmw', 'audi', 'toyota', 'subaru']
['audi', 'bmw', 'subaru', 'toyota']
['toyota', 'subaru', 'bmw', 'audi']
3. 倒着打印列表
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars.reverse())
output:
['subaru', 'toyota', 'audi', 'bmw']
4.遍历列表
names = ['ljh','wyq','sbwbh','sbgpx']
# 第一种
for name in names:
    print(name)
  
# 第二种
for i in range(len(names)):
	print(names[i])
output: 
ljh
wyq
sbwbh
sbgpx
5. 列表切片
names = ['ljh','wyq','sbwbh','sbgpx']
# print(names[i:j]) 左闭右开区间,表示从第0+i 个元素到第j个元素,如果i不存在,则默认为0
print(names[0:3])    # 左闭右开区间,表示从第0+1 个元素到第2+1个元素
output:['ljh','wyq','sbwbh']
# print(names[-i:])  # 表示最后i个元素
print(names[-2:])
output : ['sbwbh', 'sbgpx']
# 遍历切片
for name in names[i:j]: # 表示输出从i+1到j个元素
	print(names)  
6. 复制列表
names = ['ljh','wyq','sbwbh','sbgpx']
name1 = names[:]  # 得到两个列表

4.字典(dict)

先建立一个简单的字典

ljh = {
	'name' : 'ljh',
	'sex' : 'male',
	'height' : '175cm',
	'weight' : '75kg',
	'school' : 'BLCU',
	'English' : 'CET6&TEM-4',
	'major' : 'IMS'
}

访问字典中的值:

print(ljh['name'])
output:ljh

往字典中添加键值对

ljh['girlfriend'] = 'wyq'
print(ljh)
output:
{'name': 'ljh', 'sex': 'male', 'height': '175cm', 'weight': '75kg', 'school': 'BLCU', 'English': 'CET6&TEM-4', 'major': 'IMS', 'girlfriend': 'wyq'}

遍历字典

for key, value in ljh.items():
	print("\nKey:",key,"\nValue:",value)
output:

Key: name 
Value: ljh

Key: sex 
Value: male

Key: height 
Value: 175cm

Key: weight 
Value: 75kg

Key: school 
Value: BLCU

Key: English 
Value: CET6&TEM-4

Key: major 
Value: IMS

Key: girlfriend 
Value: wyq
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值