本篇文章正文内容,下面案例可供参考`
一、字典
01字典的定义格式以及键值对

02变量的定义
字典是一个无序的数据集合,使用打印函数输出字典时,通常始输出的顺序和定义的顺序是不一致的!
huanzi={"name":"欢子",
"age":18,
"weight":65,
"agender": "男",
"height": 1.75
}
print(huanzi)
03字典的增删查改
huanzi_dict={"name":"欢子"
}
huanzi_dict["age"]=18
huanzi_dict.pop("name")
print(huanzi_dict)

04字典的常用操作
 
05字典的循环遍历
huanzi_dict={"name":"欢子",
"phone":18135124850,
"qq":2012122}
for k in huanzi_dict:
print("%s - %s" %(k,huanzi_dict[k]))

06字典和列表组合的应用场景
card_list=[
{"name":"欢子",
"phone":18135124850,
"qq":2980053030},
{"name":"张三",
"phone":12345678969,
"qq":123456152}
]
for card_list in card_list:
print(card_list)

二、字符串
01字符串的定义

02字符串的常用操作

 
03判断数字的三个方法

num_str="一千零一"
print(num_str)
print(num_str.isdecimal())
print(num_str.isdigit())
print(num_str.isnumeric())
04字符串的查找和替换

hello_str="hello world"
print(hello_str.startswith("hel"))
print(hello_str.endswith("world"))
print(hello_str.index("llo"))
print(hello_str.find("llo"))
print(hello_str.replace("hello","python"))
print(hello_str)

05文本对齐方法

poem=["邓焕缺漏",
"王之涣",
"白日依山尽",
"黄河入海流",
"欲穷千里目",
"跟上一层楼",
]
for poem_str in poem:
print("|%s|" % poem_str.center(10," "))
 (不知道为啥右边没对齐呜呜呜)
06去除空白字符

poem=["\t\r邓焕缺漏",
"王之涣\t\r",
"白日依山尽",
"黄河入海流",
"\t欲穷千里目",
"跟上一层楼",
]
for poem_str in poem:
print("|%s|" % poem_str.strip().center(10," "))

07拆分和拼接字符串

poem_str="邓焕缺漏\t王之涣\t白日依山尽\t黄河入海流\n欲穷千里目\r\r更上有一层楼"
print(poem_str)
poem_list = poem_str.split()
print(poem_list)
result=" ".join(poem_list)
print(result)

08切片概念和语法以及倒序索引加演练
 
num="0123456789"
print(num[2:6])
print(num[2:])
print(num[0:6])
print(num[:6])
print(num[:])
print(num[::2])
print(num[1::2])
print(num[2:-1])
print(num[-2:])
print(num[-1::-1])

三、公共方法
01python内置函数
 
02切片

03运算符
 
list="a" not in "abcde"
print(list)
list=1 not in [1,2,3]
print(list)
print("a" in {"a":"jsuhid"})
list_="jsuhid" in {"a":"jsuhid"}
print(list)

04完整的for循环

for num in [1,2,3]:
print(num)
if num==2:
break
else:
print("会执行吗")
print("循环结束")

05利用for else搜索字典列表

students= [
{"name":"喜羊羊"},
{"name":"美羊羊"}
]
find_name="懒羊羊"
for stu_dict in students:
print(stu_dict)
if stu_dict["name"] == find_name:
print(" 找到了 %s" % find_name)
break
else:
print("抱歉,没有找到 %s" % find_name)
print("循环结束")
 
|