dict0 = {"python" : "Python由荷兰国家数学与计算机科学研究中心的吉多·范罗苏姆于1990年代初设计,作为一门叫做ABC语言的替代品。", "java" : "Java是一门面向对象的编程语言,不仅吸收了C 语言的各种优点,还摒弃了C 里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进"};
info = {'name':'python', 'id':1, 'age':34, 'address':'荷兰国家数学与计算机科学研究中心'}info = {'name':'python', 'id':1, 'age':34, 'address':'荷兰国家数学与计算机科学研究中心'}
print(info['name'])
print(info['address'])>>> info['sex'] Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'sex'
info = {'name':'python', 'id':1, 'age':34, 'address':'荷兰国家数学与计算机科学研究中心'}
print(info.get("sex"))
print(info.get("sex",2))结果: None 2
info = {'name':'python', 'id':1, 'age':34, 'address':'荷兰国家数学与计算机科学研究中心'}
print(len(info))
结果:
4
info = {'name':'python', 'id':1, 'age':34, 'address':'荷兰国家数学与计算机科学研究中心'}
print(info.keys())
结果:
dict_keys(['name', 'id', 'age', 'address'])info = {'name':'python', 'id':1, 'age':34, 'address':'荷兰国家数学与计算机科学研究中心'}
print(info.values())
结果:
dict_values(['python', 1, 34, '荷兰国家数学与计算机科学研究中心'])info = {'name':'python', 'id':1, 'age':34, 'address':'荷兰国家数学与计算机科学研究中心'}
print(info.items())
结果:
dict_items([('name', 'python'), ('id', 1), ('age', 34), ('address', '荷兰国家数学与计算机科学研究中心')])mystr="Python才是世界上最好的语言,世界最好的语言python"
for char in mystr:
print(char,end=" ")
结果:
P y t h o n 才 是 世 界 上 最 好 的 语 言 , 世 界 最 好 的 语 言 p y t h o nmyList=["Python","C","Java","C ","C#"]
for item in myList:
print(item,end=" ")
结果:
Python C Java C C#myTurple = ("Python", "C", "Java", "C ", "C#")
for t in myTurple:
print(t,end=" ")
结果:
Python C Java C C#info = {'name':'python', 'id':1, 'age':34, 'address':'荷兰国家数学与计算机科学研究中心'}
for key in info.keys():
print(key,end=",")
结果:
name,id,age,address,info = {'name':'python', 'id':1, 'age':34, 'address':'荷兰国家数学与计算机科学研究中心'}
for key in info.values():
print(key,end=",")
结果:
python,1,34,荷兰国家数学与计算机科学研究中心,
info = {'name':'python', 'id':1, 'age':34, 'address':'荷兰国家数学与计算机科学研究中心'}
for item in info.items():
print(item,end=",")
结果:
('name', 'python'),('id', 1),('age', 34),('address', '荷兰国家数学与计算机科学研究中心'),info = {'name':'python', 'id':1, 'age':34, 'address':'荷兰国家数学与计算机科学研究中心'}
for key,value in info.items():
print("key=%s value=%s"%(key,value))
结果:
key=name value=python
key=id value=1
key=age value=34
key=address value=荷兰国家数学与计算机科学研究中心|
运算符 |
Python 表达式 |
结果 |
描述 | 支持的数据类型 |
|
|
[1, 2] [3, 4] |
[1, 2, 3, 4] |
合并 | 字符串、列表、元组 |
| * | 'Hi!' * 4 | ['Hi!', 'Hi!', 'Hi!', 'Hi!'] | 复制 | 字符串、列表、元组 |
| in |
3 in (1, 2, 3) |
True |
元素是否存在 |
字符串、列表、元组、字典 |
| not in | 4 not in (1, 2, 3) | True | 元素是否不存在 | 字符串、列表、元组、字典 |
a=[1,2,3] b=[4,5,6] print(a b) str1="Best" str2="Python" print(str1 str2) 结果: [1, 2, 3, 4, 5, 6] BestPython
str2="Python " print(str2 * 4) 结果: Python Python Python Python
a=[1,2,3] b=[4,5,6] print( 3 in a) print(7 in b) 结果: True False
| 序号 | 方法 | 描述 |
| 1 |
len(item) | 计算容器中元素个数 |
| 2 |
max(item) | 返回容器中元素最大值 |
| 3 | min(item) | 返回容器中元素最小值 |
| 4 | del(item) | 删除变量 |
>>> len("python java")
11
>>> len([1,2,3,4])
4
>>> len({"python":"langeu","java":""})
2
>>>
>>> max([1,2,3,4])
4
>>> max("python java")
'y'
>>> max({"a":1,"b":2})
'b'
>>>
>>> a=1 >>> a 1 >>> del a >>> a Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined >>> a=['python','java','c','c '] >>> a ['python', 'java', 'c', 'c '] >>> del a[0] >>> a ['java', 'c', 'c '] >>> del(a) >>> a Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined
>> tuple1=[(2,3),(4,5)] >>> tuple1[0] (2, 3) >>> tuple1[0][0] 2 >>> tuple1[0][2] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: tuple index out of range >>> tuple1[2][2] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range >>> tuple2=tuple1 [3] >>> tuple2 [(2, 3), (4, 5), 3] >>> tupl2[2] Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'tupl2' is not defined >>> tuple2[2] 3 >>> tuple2[2][0] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not subscriptable
info = {'name':'python', 'id':1, 'age':34, 'address':'荷兰国家数学与计算机科学研究中心'}
newId=input("请输入编号:")
info['id']=int(newId)
print("修改后的id为%d"%info['id'])
结果:
2info = {'name':'python', 'id':1, 'age':34, 'address':'荷兰国家数学与计算机科学研究中心'}
info['author']="吉多·范罗苏姆"
print(info)
结果:
{'name': 'python', 'id': 1, 'age': 34, 'address': '荷兰国家数学与计算机科学研究中心', 'author': '吉多·范罗苏姆'}info = {'name':'python', 'id':1, 'age':34, 'address':'荷兰国家数学与计算机科学研究中心'}
info['author']="吉多·范罗苏姆"
del info['author']
print(info)
结果:
{'name': 'python', 'id': 1, 'age': 34, 'address': '荷兰国家数学与计算机科学研究中心'}info = {'name':'python', 'id':1, 'age':34, 'address':'荷兰国家数学与计算机科学研究中心'}
info['author']="吉多·范罗苏姆"
#删除前
print(info)
del info
#删除后
print(info)
结果:
print(info)
NameError: name 'info' is not definedinfo = {'name':'python', 'id':1, 'age':34, 'address':'荷兰国家数学与计算机科学研究中心'}
info['author']="吉多·范罗苏姆"
#清空前
print(info)
info.clear()
#清空后
print(info)
结果:
{}