首页 > 编程语言 >python30

python30

时间:2022-12-16 12:45:18浏览次数:41  
标签:输出 code python30 dict print tinydict 字典

Python 字典

字典(dictionary)是除列表以外python之中最灵活的内置数据结构类型。列表是有序的对象集合,字典是无序的对象集合。

两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。

字典用"{ }"标识。字典由索引(key)和它对应的值value组成。

实例(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- dict = {} dict['one'] = "This is one" dict[2] = "This is two" tinydict = {'name': 'runoob','code':6734, 'dept': 'sales'} print dict['one'] # 输出键为'one' 的值 print dict[2] # 输出键为 2 的值 print tinydict # 输出完整的字典 print tinydict.keys() # 输出所有键 print tinydict.values() # 输出所有值

输出结果为:

This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'runoob'}
['dept', 'code', 'name']
['sales', 6734, 'runoob']

标签:输出,code,python30,dict,print,tinydict,字典
From: https://www.cnblogs.com/chenyutong0321/p/16987067.html

相关文章

  • 我做过的Python30道练习题
    练习题1成绩等级要求输出成绩等级A、B、C、D、E,其中90-100分为A,80-89分为B,70-79分为C,60-69分为D,60分以下为E。要求:-用If语句实现;-输入百分制成绩后要判断该成绩......