字符串
"hello world"
- 萬惡的字符串拼接
python中的字符串在C語言中體現(xiàn)為是一個(gè)字符數(shù)組韧掩,每次創(chuàng)建字符串時(shí)候需要在內(nèi)存中開辟一塊連續(xù)的空間酪劫,
并且一旦需要修改字符串的話旱捧,就需要再次開辟空間愕够,萬惡的+號每出現(xiàn)一次就會在內(nèi)從中重新開辟一塊空間过咬。
- 字符串格式化
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() # 通過指定分隔符對字符串進(jìn)行切片,得到一個(gè)列表
names = "zhangsan, lisi, wangwu"
name2 = names.split(",")
print(name2)
# 輸出
['zhangsan', ' lisi', ' wangwu']
- center() # 返回一個(gè)原字符串居中,并使用空格填充至長度 width 的新字符串酷窥。默認(rèn)填充字符為空格咽安。
例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"是否存在,如果存在返回其下標(biāo)值
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() # 判斷字符串是否是數(shù)字類型 返回True 或 False
例1:
value = "my name is {}"
print(value.isdigit())
# 輸出
False
-
例2:
num_str = "1000"
print(num_str.isdigit())
# 輸出
True
- isalnum() # 判斷字符是否只含有字母和數(shù)字組成 返回True 或 False
例1:
value = "43fd"
print(value.isalnum())
# 輸出
True
-
例2:
# value中包含一個(gè)空格
value = "43 fd"
print(value.isalnum())
# 輸出
False
- endswith() # 匹配字符串的結(jié)束位置蓬推,返回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
列表
# 創(chuàng)建列表
name_list = ["zhangsan", "lisi", "wangwu"]
或
name_list = list(["zhangsan", "lisi", "wangwu"])
# 通過列表的下標(biāo)來取對應(yīng)的值
print(name_list[1])
# 輸出
lisi
- 列表常用功能
- 修改列表中的值
# 將name_list中的 "lisi" 修改為 "liwu"
name_list = ["zhangsan", "lisi", "wangwu"]
name_list[1] = "liwu" # 指定下標(biāo)修改列表的值
print(name_list)
# 輸出
['zhangsan', 'liwu', 'wangwu']
- 切片
# 輸出name_list中的第一個(gè)值和第二個(gè)值
name_list = ["zhangsan", "lisi", "wangwu", 1, 2, 3, 4, 5]
print(name_list[1:3]) # 顧頭不顧尾
# 輸出
['lisi', 'wangwu']
- 步長
# 首先創(chuàng)建一個(gè)從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]
# 注:通過步長可以取奇偶數(shù)
- insert() # 指定坐標(biāo)插入值
# 在列表name_list的值"lisi"前面插入一個(gè)值"xiaogou"
name_list = ["zhangsan", "lisi", "wangwu"]
name_list.insert(1, "xiaogou")
print(name_list)
# 輸出
['zhangsan', 'xiaogou', 'lisi', 'wangwu']
- append() # 在列表的末尾追加一個(gè)值
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']
# 如果刪除的值不在列表中,則會報(bào)錯(cuò)
name_list = ["zhangsan", "lisi", "wangwu"]
name_list.remove("lisfdi")
print(name_list)
# 輸出報(bào)錯(cuò)信息
ValueError: list.remove(x): x not in list
- count() # 查找在列表中出現(xiàn)的次數(shù)
name_list = ["zhangsan", "lisi", "wangwu", "lisi", 1, 2, 3, 4, 5]
print(name_list.count("lisi")) # 查找"lisi"在列表中出現(xiàn)的次數(shù)
# 輸出
2
- index() # 查找在列表中的下標(biāo)
name_list = ["zhangsan", "lisi", "wangwu", "lisi", 1, 2, 3, 4, 5]
print(name_list.index("lisi"))
# 輸出
1
-
# 如果查找的值在列表中不存在募逞,則會報(bào)錯(cuò)
name_list = ["zhangsan", "lisi", "wangwu", "lisi", 1, 2, 3, 4, 5]
print(name_list.index("lisi2"))
# 輸出報(bào)錯(cuò)信息
ValueError: 'lisi2' is not in list
- extend() # 在原先列表的基礎(chǔ)之上擴(kuò)展一個(gè)新的列表
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() # 將列表的值進(jìn)行反轉(zhuǎn)
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()
# 輸錯(cuò)報(bào)錯(cuò)信息
TypeError: unorderable types: int() < str()
-
# 對字母進(jìn)行排序
name_list = ['zhangsan', 'lisi', 'wangwu', 'lisi']
name_list.sort()
print(name_list)
# 輸出
['lisi', 'lisi', 'wangwu', 'zhangsan']
-
# 對數(shù)字進(jìn)行排序
num_list = [3, 4, 1, 100, 3, 5, 6]
num_list.sort()
print(num_list)
# 輸出
[1, 3, 3, 4, 5, 6, 100]
- pop() # 默認(rèn)刪除列表最后的一個(gè)值蛋铆,并返回刪掉的那個(gè)值
name_list = ['zhangsan', 'lisi', 'wangwu', 'lisi', '44']
pop_name = name_list.pop()
print(name_list)
print(pop_name)
# 輸出
['zhangsan', 'lisi', 'wangwu', 'lisi']
44
-
# 指定下標(biāo)刪除列表元素
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()查看內(nèi)存地址看區(qū)別
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中嵌套一個(gè)列表,然后拷貝一份放接,然后在修改嵌套列表中的值
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]]
# 淺拷貝與=之間的區(qū)別纠脾,等于號相當(dāng)于軟連接玛瘸,淺拷貝類似于上面
字典
- 創(chuàng)建字典
創(chuàng)建字典
# 字典是以key和value的形式出現(xiàn)的
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進(jìn)行刪除,并返回其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() # 對已經(jīng)存在的key進(jìn)行更新,沒有的key進(jìn)行添加
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放到一個(gè)元組中苟蹈,多組key和value放入到到一個(gè)列表
中糊渊,轉(zhuǎn)成這種格式: [(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() # 指定一個(gè)key,如果字典存在這個(gè)key慧脱,則返回該key的值渺绒,否則創(chuàng)建這個(gè)key,如果setdefault()括號內(nèi)為空菱鸥,則創(chuàng)建的key的value 為None宗兼,否則為該括號內(nèi)填寫的值
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() # 隨機(jī)刪除字典中的指定的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'}}
數(shù)據(jù)類型
-
算數(shù)運(yùn)算
-
比較運(yùn)算
-
賦值運(yùn)算
-
邏輯運(yùn)算
-
成員運(yùn)算
-
身份運(yùn)算
-
位運(yùn)算
-
運(yùn)算符優(yōu)先級