首页 > 编程语言 > 详细

Python 学习4

时间:2016-10-04 07:29:21      阅读:154      评论:0      收藏:0      [点我收藏+]

1. Use set() function to show only unique elements. And we can add() and remove() in a set. Also, it is allowed to use list() to change set into a list

2. User try/except to avoid errors in the program.

3.enumerate() function:

animals = ["Dog", "Tiger", "SuperLion", "Cow", "Panda"]
viciousness = [1, 5, 10, 10, 1]

for i, animal in enumerate(animals):#In the list animals, i is the index of each elements, animal in the element correspond to each element.
    print("Animal")
    print(animal)
    print("Viciousness")
    print(viciousness[i])

 4.List comprehensions

 技术分享

5.The items() method:

plant_types = {"orchid": "flower", "cedar": "tree", "maple": "tree"}
for types,rating in plant_types.items():# here we use items() to avoid access both key and values on dictionaries.
    print (types)
    print (rating)

 

Python 学习4

原文:http://www.cnblogs.com/kingoscar/p/5930016.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!