字典
'a = {'name':'gaoqi','age':18,'job':'programmer'}
b = dict(name='gaoqi',age=18,job='programmer')
字典元素的访问
a['name']
a.get('name')
a.items()
a.keys()
a.values()
字典元素添加、修改、删除
a[""]=""
a.update(b)
del(a[""])
a.pop("")
序列解包
s = {"name":"yangwenjie","age":25,"hobby":"chess"}
a, b, c = s
d, e, f =s.values()
g, h, l = s.items()
r1 = {"name":"高小一","age":18, "salary":3000, "city":"beijing"}
r2 = {"name":"高小二", "age":19, "salary":2000, "city":"shanghai"}
r3 = {"name":"高小三", "age":20, "salary":1000, "city":"shenzheng"}
tb = [r1,r2,r3]
print(tb)
print(tb[1])
print(tb[1].get("name"))
for i in range(len(tb)):
print(tb[i].get("name"))
for i in range(len(tb)):
print(tb[i].get("name"),tb[i].get("age"),tb[i].get("salary"),tb[i].get("city"))
集合相关操作:a|b a.union(b); a&b a.intersection(b); a-b a.difference(b) 三元条件运算符
s = input("number:")
if int(s)<20:
print(s)
else:
print("out of range")
print(s if int(s)<10 else "out of range")
x = int(input("x:"))
y = int(input("y:"))
result = ""
if x==0 and y==0:
result = "原点"
elif x==0:
result = y
elif y==0:
result = x
elif x>0 and y>0:
result = "第一"
elif x>0 and y<0:
result = "第二"
elif x<0 and y>0:
result = "第三"
else:
result = "第四"
print("坐标:({0},{1}),象限:{2}".format(x,y,result))
|