字符串
"hello world"
- 万恶的字符串拼接
python中的字符串在C语言中体现为是一个字符数组,每次创建字符串时候需要在内存中开辟一块连续的空间,
并且一旦需要修改字符串的话,就需要再次开辟空间,万恶的+号每出现一次就会在内从中重新开辟一块空间。
- 字符串格式化
name = "bear"
print "i am %s " % name
# **输出**
i am bear
- 字符串常用功能
- strip() # 去掉字符串首尾空格、换行符、tab
value = " my name is bear "
print(value.strip()) # 会将首尾的空格去掉
# 输出
my name is bear
- split() # 通过指定分隔符对字符串进行切片,得到一个列表
names = "zhangsan, lisi, wangwu"
name2 = names.split(",")
print(name2)
# 输出
['zhangsan', ' lisi', ' wangwu']
- center() # 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串。默认填充字符为空格。
例1:
value = "my name is bear"
print(value.center(50))
# 输出
my name is bear
例2:
# 使用*填充空格
value = "my name is bear"
print(value.center(50, '*'))
# 输出
- find() # 字符串查找
例1:
# 在字符串value中查找字符串"zhangcong"是否存在,如果存在返回其下标值
value = "my name is zhangbear"
print(value.find("zhang"))
# 输出
11
-
例2:
# 指定开始的位置查找
value = "my name is zhangbear, my name is zhangbear"
print(value.find("zhang", 12))
# 输出
33
- capitalize() # 首字母大写
value = "my name is bear, my name is bear"
print(value.capitalize())
# 输出
My name is bear, my name is bear
- upper() # 将字符串里面的所有字母都改为大写
value = "43 fd"
print(value.upper())
# 输出
43 FD
- format() 格式化字符串
value = "my name is {}"
print(value.format("bear"))
# 输出
my name is bear
- isdigit() # 判断字符串是否是数字类型 返回True 或 False
例1:
value = "my name is {}"
print(value.isdigit())
# 输出
False
-
例2:
num_str = "1000"
print(num_str.isdigit())
# 输出
True
- isalnum() # 判断字符是否只含有字母和数字组成 返回True 或 False
例1:
value = "43fd"
print(value.isalnum())
# 输出
True
-
例2:
# value中包含一个空格
value = "43 fd"
print(value.isalnum())
# 输出
False
- endswith() # 匹配字符串的结束位置,返回True 或 False
value = "43 fd"
print(value.endswith("fd"))
# 输出
True
-
print(value.endswith("c"))
# 输出
False
- startswith() # 匹配字符串的开始位置,返回True 或 False
value = "43 fd"
print(value.startswith("4"))
# 输出
True
-
print(value.startswith("fda"))
# 输出
False
列表
# 创建列表
name_list = ["zhangsan", "lisi", "wangwu"]
或
name_list = list(["zhangsan", "lisi", "wangwu"])
# 通过列表的下标来取对应的值
print(name_list[1])
# 输出
lisi
- 列表常用功能
- 修改列表中的值
# 将name_list中的 "lisi" 修改为 "liwu"
name_list = ["zhangsan", "lisi", "wangwu"]
name_list[1] = "liwu" # 指定下标修改列表的值
print(name_list)
# 输出
['zhangsan', 'liwu', 'wangwu']
- 切片
# 输出name_list中的第一个值和第二个值
name_list = ["zhangsan", "lisi", "wangwu", 1, 2, 3, 4, 5]
print(name_list[1:3]) # 顾头不顾尾
# 输出
['lisi', 'wangwu']
- 步长
# 首先创建一个从0到100的列表,不包含100
num_list = list(range(100))
print(num_list[::2])
# 输出
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22,
24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44,
46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66,
68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88,
90, 92, 94, 96, 98]
# 注:通过步长可以取奇偶数
- insert() # 指定坐标插入值
# 在列表name_list的值"lisi"前面插入一个值"xiaogou"
name_list = ["zhangsan", "lisi", "wangwu"]
name_list.insert(1, "xiaogou")
print(name_list)
# 输出
['zhangsan', 'xiaogou', 'lisi', 'wangwu']
- append() # 在列表的末尾追加一个值
name_list = ["zhangsan", "lisi", "wangwu"]
name_list.append("xiaogou")
print(name_list)
# 输出
['zhangsan', 'lisi', 'wangwu', 'xiaogou']
- remove() # 删除列表中的指定值
name_list = ["zhangsan", "lisi", "wangwu"]
name_list.remove("lisi")
print(name_list)
# 输出
['zhangsan', 'wangwu']
# 如果删除的值不在列表中,则会报错
name_list = ["zhangsan", "lisi", "wangwu"]
name_list.remove("lisfdi")
print(name_list)
# 输出报错信息
ValueError: list.remove(x): x not in list
- count() # 查找在列表中出现的次数
name_list = ["zhangsan", "lisi", "wangwu", "lisi", 1, 2, 3, 4, 5]
print(name_list.count("lisi")) # 查找"lisi"在列表中出现的次数
# 输出
2
- index() # 查找在列表中的下标
name_list = ["zhangsan", "lisi", "wangwu", "lisi", 1, 2, 3, 4, 5]
print(name_list.index("lisi"))
# 输出
1
-
# 如果查找的值在列表中不存在,则会报错
name_list = ["zhangsan", "lisi", "wangwu", "lisi", 1, 2, 3, 4, 5]
print(name_list.index("lisi2"))
# 输出报错信息
ValueError: 'lisi2' is not in list
- extend() # 在原先列表的基础之上扩展一个新的列表
name_list = ["zhangsan", "lisi", "wangwu", "lisi"]
name_list2 = [1, 2, 3, 4, 5]
name_list.extend(name_list2)
print(name_list)
# 输出
['zhangsan', 'lisi', 'wangwu', 'lisi', 1, 2, 3, 4, 5]
- reverse() # 将列表的值进行反转
name_list = ['zhangsan', 'lisi', 'wangwu', 'lisi', 1, 2, 3, 4, 5]
name_list.reverse()
print(name_list)
# 输出
[5, 4, 3, 2, 1, 'lisi', 'wangwu', 'lisi', 'zhangsan']
- sort() # 排序
name_list = ['zhangsan', 'lisi', 'wangwu', 'lisi', 1, 2, 3, 4, 5]
name_list.sort()
# 输错报错信息
TypeError: unorderable types: int() < str()
-
# 对字母进行排序
name_list = ['zhangsan', 'lisi', 'wangwu', 'lisi']
name_list.sort()
print(name_list)
# 输出
['lisi', 'lisi', 'wangwu', 'zhangsan']
-
# 对数字进行排序
num_list = [3, 4, 1, 100, 3, 5, 6]
num_list.sort()
print(num_list)
# 输出
[1, 3, 3, 4, 5, 6, 100]
- pop() # 默认删除列表最后的一个值,并返回删掉的那个值
name_list = ['zhangsan', 'lisi', 'wangwu', 'lisi', '44']
pop_name = name_list.pop()
print(name_list)
print(pop_name)
# 输出
['zhangsan', 'lisi', 'wangwu', 'lisi']
44
-
# 指定下标删除列表元素
name_list = ['zhangsan', 'lisi', 'wangwu', 'lisi', '44']
pop_name = name_list.pop(2)
print(name_list)
print(pop_name)
# 输出
['zhangsan', 'lisi', 'lisi', '44']
wangwu
- copy() # 浅拷贝 可以通过id()查看内存地址看区别
name_list = ['zhangsan', 'lisi', 'wangwu', 'lisi', '44']
name_list2 = name_list.copy()
print("name_list: %s" % name_list)
print("name_list2: %s" % name_list2)
# 将name_list中的'zhangsan'改为'liusan'
name_list[0] = 'liusan'
print("修改完之后")
print("name_list: %s" % name_list)
print("name_list2: %s" % name_list2)
# 输出
name_list: ['zhangsan', 'lisi', 'wangwu', 'lisi', '44']
name_list2: ['zhangsan', 'lisi', 'wangwu', 'lisi', '44']
修改完之后
name_list: ['liusan', 'lisi', 'wangwu', 'lisi', '44']
name_list2: ['zhangsan', 'lisi', 'wangwu', 'lisi', '44']
-
# 在name_list中嵌套一个列表,然后拷贝一份,然后在修改嵌套列表中的值
name_list = ['zhangsan', 'lisi', 'wangwu', 'lisi', '44', [3, 4, 1, 100, 3, 5, 6]]
name_list2 = name_list.copy()
print("name_list: %s" % name_list)
print("name_list2: %s" % name_list2)
# 将name_list中的'zhangsan'改为'liusan'
name_list[-1][0] = 'liusan'
print("修改完之后")
print("name_list: %s" % name_list)
print("name_list2: %s" % name_list2)
# 输出
# 可以看到name_list中嵌套列表中的值被修改,name_list2也随着变化,浅拷贝只能操作第一层
name_list: ['zhangsan', 'lisi', 'wangwu', 'lisi', '44', [3, 4, 1, 100, 3, 5, 6]]
name_list2: ['zhangsan', 'lisi', 'wangwu', 'lisi', '44', [3, 4, 1, 100, 3, 5, 6]]
修改完之后
name_list: ['zhangsan', 'lisi', 'wangwu', 'lisi', '44', ['liusan', 4, 1, 100, 3, 5, 6]]
name_list2: ['zhangsan', 'lisi', 'wangwu', 'lisi', '44', ['liusan', 4, 1, 100, 3, 5, 6]]
# 浅拷贝与=之间的区别,等于号相当于软连接,浅拷贝类似于上面
字典
- 创建字典
创建字典
# 字典是以key和value的形式出现的
id_db = {
22323232: {
"name": "ShanPao",
"age": 22,
"addr": "ShanDong"
},
43267432:{
"name": "DaShanPao",
"age": 24,
"addr": "DongBei"
}
}
# 输出字典中key为id_db[22323232]的值
print(id_db[22323232])
# 修改字典中key为22323232的key "name"的值
id_db[22323232]["name"] = "Wangwu"
print(id_db)
# 输出
{'age': 22, 'addr': 'ShanDong', 'name': 'ShanPao'}
{22323232: {'age': 22, 'addr': 'ShanDong', 'name': 'Wangwu'},
43267432: {'age': 24, 'addr': 'DongBei', 'name': 'DaShanPao'}}
- 字典常用功能
- pop() # 对指定key进行删除,并返回其value
id_db = {
22323232: {
"name": "ShanPao",
"age": 22,
"addr": "ShanDong"
},
43267432:{
"name": "DaShanPao",
"age": 24,
"addr": "DongBei"
}
}
print(id_db)
v = id_db.pop(43267432)
print(v)
print(id_db)
# 输出
# 字典id_db的值
{22323232: {'age': 22, 'addr': 'ShanDong', 'name': 'ShanPao'},
43267432: {'age': 24, 'addr': 'DongBei', 'name': 'DaShanPao'}}
# 通过pop()删除的值
{'age': 24, 'addr': 'DongBei', 'name': 'DaShanPao'}
# 通过pop()删除之后的值
{22323232: {'age': 22, 'addr': 'ShanDong', 'name': 'ShanPao'}}
- get() # 通过key获取值
id_db = {
22323232: {
"name": "ShanPao",
"age": 22,
"addr": "ShanDong"
},
43267432:{
"name": "DaShanPao",
"age": 24,
"addr": "DongBei"
}
}
print(id_db.get(43267432))
# 输出
{'age': 24, 'name': 'DaShanPao', 'addr': 'DongBei'}
- update() # 对已经存在的key进行更新,没有的key进行添加
id_db = {
22323232: {
"name": "ShanPao",
"age": 22,
"addr": "ShanDong"
},
43267432:{
"name": "DaShanPao",
"age": 24,
"addr": "DongBei"
}
}
dic2 = {
22323232: {
"name": "DaShaXXXXXXX",
"age": 22,
"addr": "ShanDong"
},
"name": "acong",
43267432: "xxxxxx"
}
print(id_db)
id_db.update(dic2)
print(id_db)
# 输出
{22323232: {'addr': 'ShanDong', 'name': 'ShanPao', 'age': 22}, 43267432: {'addr': 'DongBei', 'name': 'DaShanPao', 'age': 24}}
{22323232: {'addr': 'ShanDong', 'name': 'DaShaXXXXXXX', 'age': 22}, 43267432: 'xxxxxx', 'name': 'acong'}
- items() # 将字典的key和value放到一个元组中,多组key和value放入到到一个列表
中,转成这种格式: [(key, value), (key, value)]
id_db = {
22323232: {
"name": "ShanPao",
"age": 22,
"addr": "ShanDong"
},
43267432:{
"name": "DaShanPao",
"age": 24,
"addr": "DongBei"
}
}
print(id_db.items())
# 输出
dict_items([(22323232, {'age': 22, 'addr': 'ShanDong', 'name': 'ShanPao'}), (43267432, {'age': 24, 'addr': 'DongBei', 'name': 'DaShanPao'})])
- keys() # 输出字典所有的key
id_db = {
22323232: {
"name": "ShanPao",
"age": 22,
"addr": "ShanDong"
},
43267432:{
"name": "DaShanPao",
"age": 24,
"addr": "DongBei"
}
}
print(id_db.keys())
# 输出
dict_keys([22323232, 43267432])
- values() # 输出字典所有的value
id_db = {
22323232: {
"name": "ShanPao",
"age": 22,
"addr": "ShanDong"
},
43267432:{
"name": "DaShanPao",
"age": 24,
"addr": "DongBei"
}
}
print(id_db.values())
# 输出
dict_values([{'name': 'ShanPao', 'addr': 'ShanDong', 'age': 22}, {'name': 'DaShanPao', 'addr': 'DongBei', 'age': 24}])
has_key() # python3使用in判断
setdefault() # 指定一个key,如果字典存在这个key,则返回该key的值,否则创建这个key,如果setdefault()括号内为空,则创建的key的value 为None,否则为该括号内填写的值
id_db = {
22323232: {
"name": "ShanPao",
"age": 22,
"addr": "ShanDong"
},
43267432:{
"name": "DaShanPao",
"age": 24,
"addr": "DongBei"
}
}
value = id_db.setdefault(43267432)
print(value)
value2 = id_db.setdefault(432674323232)
print(value2)
print(id_db)
# 输出
{'addr': 'DongBei', 'name': 'DaShanPao', 'age': 24}
None
{22323232: {'addr': 'ShanDong', 'name': 'ShanPao', 'age': 22}, 43267432: {'addr': 'DongBei', 'name': 'DaShanPao', 'age': 24}, 432674323232: None}
- popitem() # 随机删除字典中的指定的key
id_db = {
22323232: {
"name": "ShanPao",
"age": 22,
"addr": "ShanDong"
},
43267432:{
"name": "DaShanPao",
"age": 24,
"addr": "DongBei"
}
}
print(id_db.popitem())
print(id_db)
# 输出
(22323232, {'age': 22, 'addr': 'ShanDong', 'name': 'ShanPao'})
{43267432: {'age': 24, 'addr': 'DongBei', 'name': 'DaShanPao'}}
数据类型
-
算数运算
-
比较运算
-
赋值运算
-
逻辑运算
-
成员运算
-
身份运算
-
位运算
-
运算符优先级