
极简生活,极简编程,简到极致,就是完美!
进阶语法部分肝完了,有点麻木了,这是个体力劳动!
同样我的练习资料,放在最后,可直接点击下载。废话不多说,直接开始!
# 1.1 推导式:列表推导式:过滤列表留下奇数部分 scr_list = [1, 2, 3, 4, 5] odd_list = [ele for ele in scr_list if ele % 2 != 0] print(odd_list)
[1, 3, 5]
# 1.2 推导式:元组推导式:过滤元组,留下长度大于2的部分
src_tuple = ("Java", "C++", "AI", "Python")
res_tuple = (ele for ele in src_tuple if len(ele) > 2)
print(tuple(res_tuple))
('Java', 'C++', 'Python')
# 1.3 推导式:字典推导式:过滤字典,留下user1的部分
name_dict = {
"user1": {
"name": "张三",
"age": 18
},
"user2": {
"name": "李四",
"age": 22
}
}
user1_dict = {key: name_dict[key] for key in name_dict if "user1" == key}
print(user1_dict)
{'user1': {'name': '张三', 'age': 18}}
# 2. 三元表达式、三元组:写一个判断,奇数保留
test_list=[1,11,21,10,20]
def isOdd(num):
return True if num %2!=0 else False
for ele in test_list:
if isOdd(ele):
print("{} 是奇数!".format(ele))
1 是奇数! 11 是奇数! 21 是奇数!
# 3. 断言:输入参数校验
def checkInput(number,string):
assert type(number)==int
assert type(string)==str
print("输入参数正常!")
checkInput(100,"aaa")
# checkInput("aaa",110) #断言失败,AssertionError:
输入参数正常!
# 4.with-as语句:简化文件读写操作
import json
file_path = "data.txt"
with open(file_path, mode='r') as file:
data_json = json.loads(file.read())
print(data_json)
{'name': 'zhangsan', 'age': 18, 'hobby': 'baipiao'}
# 5.异常处理:try-execpt
try:
print("start!")
res = 1/0
print("------")
except Exception as e:
print(str(e))
finally:
print("exit!")
start! division by zero exit!常见异常
# 6.字符串方法(在基础语法部分写了)
# 7.lambda函数
add_fun=lambda x:x*10
print(add_fun(99))
#等价于
def add_fun2(x):
return x*10
print(add_fun2(99))
990 990
# 8.文件:简单文件读写
# 1.read
json_path="data.txt"
with open(json_path,mode='r') as file:
data=file.read()
print(data)
# 2.write
output_path="out.txt"
with open(output_path,mode='w') as file2:
file2.write(data)
file2.close()
file.close()
{
"name":"zhangsan",
"age":18,
"hobby":"baipiao"
}
# 9.常用标准库
# 1.json库:dict转成json
import re
import os
import json
src_dict = {
"user1": {
"name": "张三",
"age": 18,
"job": "狂徒"
},
"user2": {
"name": "李四",
"age": 22,
"job": "导演"
}
}
print(src_dict)
print(type(src_dict))
# 1.1 dict->json_str
res_json = json.dumps(src_dict, ensure_ascii=False)
print(res_json)
print(type(res_json))
# 1.2 json_str-> dict
res2_json = json.loads(res_json)
print(res2_json)
print(type(res2_json))
# 2.os库:判断路径是否存在
isExists1 = os.path.exists("./data.txt")
isExists2 = os.path.exists("./data2.txt")
print(isExists1)
print(isExists2)
# 3.re库:正则匹配,切分时间字符串,拿到每一部分
src_str = "1970-01-01 01:02:03"
pattern = r'-| |:'
splits = re.split(pattern, src_str)
for ele in splits:
print(ele)
{'user1': {'name': '张三', 'age': 18, 'job': '狂徒'}, 'user2': {'name': '李四', 'age': 22, 'job': '导演'}}
{"user1": {"name": "张三", "age": 18, "job": "狂徒"}, "user2": {"name": "李四", "age": 22, "job": "导演"}}
{'user1': {'name': '张三', 'age': 18, 'job': '狂徒'}, 'user2': {'name': '李四', 'age': 22, 'job': '导演'}}
True
False
1970
01
01
01
02
03
# 10.字符的编解码
unicode_str = u'你能看懂的!'
print(unicode_str)
utf8_str = unicode_str.encode(encoding='utf-8')
print(utf8_str)
res_str = utf8_str.decode("utf-8")
print(res_str)
你能看懂的! b'xe4xbdxa0xe8x83xbdxe7x9cx8bxe6x87x82xe7x9ax84xefxbcx81' 你能看懂的!
我的代码及资料在:进阶语法.zip,下期见,byebye!