运算符,列表copy,作业
Python学习日记-day16
数据运算
1. 算数运算
% 取模,可以用来判断奇偶数,比如上面这个表格
2. 比较运算
python2里 != 和 <> 是一样的,python3里没有 <>
3. 赋值运算
4. 逻辑运算
5. 成员运算
6. 身份运算
bool运算中以下几种情况都为False:
[] 空列表
{} 空字典
() 空元组
' ' 空字符串
0
7. 位运算
例子如下:
列表copy
列表copy,如果源列表里有子列表,copy后修改源子列表里的值,目标列表里的子列表值也会变.
例子:
name = [1, 2, 3, ["alex", "jim"]] n2 = name.copy() print("1. 直接copy整个列表 --------------") print(name) print(n2) print("2. 将name[1]修改为-2 --------------") name[1] = -2 print(name) print(n2) print("3. 将name[3][0]修改为 DJ --------------") name[3][0] = "DJ" print(name) print(n2)
输出结果:
1. 直接copy整个列表 --------------
[1, 2, 3, ['alex', 'jim']]
[1, 2, 3, ['alex', 'jim']]
2. 将name[1]修改为-2 --------------
[1, -2, 3, ['alex', 'jim']]
[1, 2, 3, ['alex', 'jim']]
3. 将name[3][0]修改为 DJ --------------
[1, -2, 3, ['DJ', 'jim']]
[1, 2, 3, ['DJ', 'jim']]
作业:
三级菜单
# 需求:三级菜单,b返回上级,q退出 provice = { "四川省": { "成都市": { "金牛区": { "茶店子": {}, "交大路": {} }, "青羊区": { "光华村": {}, "杜甫草堂": {} }, "锦江区": { "春熙路": {} }, "成华区": { "驷马桥": {} }, "武侯区": { "高升桥": {}, "红牌楼": {} }, "高新区": { "天府软件园": {}, "腾讯": {}, "IBM": {} } }, "宜宾市": { "翠屏区": {}, "长宁县": {}, "南溪区": {}, "江安县": {} } }, "北京市": { "东城区": { }, "朝阳区": { } } } while True: for i in provice: print(i) choice = input("please input L1:") if choice == "q": exit() if choice in provice: while True: for j in provice[choice]: print(j) choice2 = input("please input L2:") if choice2 == "b": break if choice2 == "q": exit() if choice2 in provice[choice]: while True: for k in provice[choice][choice2]: print(k) choice3 = input("please input L3:") if choice3 == "b": break if choice3 == "q": exit() if choice3 in provice[choice][choice2]: while True: for m in provice[choice][choice2][choice3]: print(m) ch4 = input("b or q:") if ch4 == "b": break if ch4 == "q": exit()
共有 0 条评论