dictionary ----- python

Learn of dictionary,simple example of dictionary in  “Simple Python tutorial"---------------------------------------------------------

#!/usr/bin/python
# Filename:dictionary.py

# ‘ab‘ is short for ‘a‘ddress‘b‘ook

ab = { ‘Swaroop‘ : ‘[email protected]‘,
        ‘Larry‘  : ‘[email protected]‘,
        ‘Matsumoto‘ : ‘[email protected]‘,
        ‘Spammer‘ : ‘[email protected]‘  }

print "Swaroop‘s address is %s" %ab[‘Swaroop‘]

# delete a key->value pair

del ab[‘Spammer‘] 

print ‘\nThere are %d contacts in the address-book\n‘ %len(ab)
for name,address in ab.items():
        print ‘Contact %s at %s‘ %(name,address)

if ‘Guido‘ in ab:
    print "\nGuido‘s address is %s" %ab[‘Guido‘]

if ab.has_key(‘Larry‘):
    print "\nLarry‘s address is %s" %ab[‘Larry‘]

dictionary ----- python

时间: 2024-11-06 03:31:24

dictionary ----- python的相关文章

Python学习总结

Python 基础教程 Python 基础教程 Python 简介 Python 环境搭建 Python 中文编码 Python 基础语法 Python 变量类型 Python 运算符 Python 条件语句 Python 循环语句 Python While 循环语句 Python for 循环语句 Python 循环嵌套 Python break 语句 Python continue 语句 Python pass 语句 Python Number(数字) Python 字符串 Python 列

python入门(1)

Python是通过python解释器执行的. 1.第一个python程序 print "hello world" # 存为hello.py文件 如何运行?在命令行下执行python hello.py执行即可. 2.变量和算数表达式 principal = 1000                         # python是弱类型语言,变量声明之前不用指定类型 rate = 0.5 numyears = 5 year = 1 while year <= numyears: 

Python 基础教程

Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.Python由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年.像Perl语言一样, Python 源代码同样遵循 GPL(GNU General Public License)协议.现在开始学习 Python! 谁适合阅读本教程?本教程适合想从零开始学习Python编程语言的开发人员.当然本教程也会对一些模块进行深入,让你更好的了解Python的应用. 学习本教程前你需要了解在继续本教程之前

Python基础语法&mdash;字符串&amp;语句&amp;集合

Python字符串 Python中不支持char单字符类型,单字符在Python中也是一个字符串 Python字符串更新 更新Python字符串方法 1234 var1 = 'Hello World!'print "Updated String :- ", var1[:6] + 'Python' 实际执行效果为 Updated String :- Hello Python Python转义字符 Python字符串运算符 Python字符串格式化 Python三引号(triple quo

Instant Django 1.5 Application Development Starter

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Model: The application data View: which data is presented Template: How the data is presented A project is just a collection of settings organi

python之最强王者(8)——字典(dictionary)

1.Python 字典(Dictionary) 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示: d = {key1 : value1, key2 : value2 } 键必须是唯一的,但值则不必. 值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组. 1.1创建字典 示例1 d = {"德玛西亚之力": "盖伦"

Python dictionary implementation

Python dictionary implementation http://www.laurentluce.com/posts/python-dictionary-implementation/ August 29, 2011 This post describes how dictionaries are implemented in the Python language. Dictionaries are indexed by keys and they can be seen as

Python 字典(Dictionary) get()方法

描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 key -- 字典中要查找的键. default -- 如果指定键的值不存在时,返回该默认值值. 返回值 返回指定键的值,如果值不在字典中返回默认值None. 实例 以下实例展示了 get()函数的使用方法: #!/usr/bin/python dict = {'Name': 'Zara', 'A

sort a Python dictionary by value

首先要明确一点,Python的dict本身是不能被sort的,更明确地表达应该是"将一个dict通过操作转化为value有序的列表" 有以下几种方法: 1. import operator x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0} sorted_x = sorted(x.items(), key=operator.itemgetter(1)) #sorted by value sorted_x = sorted(x.items(), key=operator