码迷,mamicode.com
首页 > 编程语言 > 详细

Python 学习4

时间:2016-10-04 07:32:34      阅读:162      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!