创建两个元组并输出他们的类型。
演示运算:
tup1 = ("lqj_本人") #创建空的元组
print(type(tup1)) #<class 'tuple'>:以元组的类型出现
tup2 = (50)
print(type(tup2)) #<class 'int'>:以int整型的类型出现
运算结果:
<class 'str'>
<class 'int'>
我们可以通过type()方法来查看我们创建的元组中的内容的类型。
演示运算:
tup1 = ["1","2","3","4"]
tup2 = ["a","b"]
tup = tup1 +tup2
print(tup)
运算结果:
['1', '2', '3', '4', 'a', 'b']
演示运算:
tup1 = ["1","2","3","4"]
del tup1[0] #根据下标删除数据
print(tup1)
运算结果:
['2', '3', '4']
演示运算:
tup1 = ["1","2","3","4"]
tup1[0] = "100" #根据下标来修改数据
print(tup1)
运算结果:
['100', '2', '3', '4']
演示运算:
tup1 = ["1","2","3","4"]
print(tup1[0])
运算结果:
1
python中的字典就跟我们JavaScript里面的对象一样,都有一个key对应的一个value值。
演示运算:
lqj = {"name":"彭于晏","age":"18"}
print(lqj["name"])
print(lqj["age"])
运算结果:
彭于晏
18
演示运算:
info = {"name":"彭于晏","age":18}
newID = input("请输入新的学号:")
info["id"] = newID #info["此处是要在info{}添加的键的信息,与“name" “age”一类]
print("添加学号完成")
chazhao = input("请输出要查找的信息:")
print(info[chazhao])
运算结果:
请输入新的学号:18
添加学号完成
请输出要查找的信息:id
18
演示运算:
info = {"name":"吴彦祖","age":18}
print("删除前:%s"%info)
info.clear()
print("删除后:%s"%info)
运算结果:
删除前:{'name': '吴彦祖', 'age': 18}
删除后:{}
演示运算:
info = {"name":"吴彦祖","age":18}
print(info)
info["name"] = "彭于晏"
print(info)
运算结果:
{'name': '吴彦祖', 'age': 18}
{'name': '彭于晏', 'age': 18}
演示运算:
info = {"id":1,"name":"吴彦祖","age":18}
print(info.keys()) #得到所有的键(列表)
print(info.values()) #得到所有的值(列表)
print(info.items()) #得到所有的项(列表)
'''
'''
info = {"id":1,"name":"吴彦祖","age":18}
for keys in info:
print("key=%s"%keys)
print("-"*20)
for values in info:
print("values=%s"%values)
print("-"*20)
for keys,values in info.items():
print("keys=%s\nvalues=%s"%(keys,values))
print("-"*20)
运算结果:
dict_keys(['id', 'name', 'age'])
dict_values([1, '吴彦祖', 18])
dict_items([('id', 1), ('name', '吴彦祖'), ('age', 18)])
key=id
key=name
key=age
--------------------
values=id
values=name
values=age
--------------------
keys=id
values=1
keys=name
values=吴彦祖
keys=age
values=18
--------------------