Python笔记一

来源:互联网 发布:淘宝美式家具品牌 编辑:程序博客网 时间:2024/06/05 07:50
(1)builtin_function_or_method' object has no attribute '__getitem__
elements = []
# then use the range function to do 0 to 5 counts
for i in range(0, 6) :
    print "Adding %d to the list : " % i
    # append is a function that lists understand
    elements.append[i]

for i in elements : 
    print "Element was: %d " % i

【报错】
Traceback (most recent call last):
  File "ex32.py", line 29, in <module>
    elements.append[i]
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
【修改】
    elements.append[i] ===》    elements.append(i)

(2)函数注解
extest.py
def cthulhu_room() :
    """ This is cthulhu_room function"""

使用方法:

[Harvey@/Users/Harvey/mystuff]cat extest.py
def cthulhu_room() :
    """ This is cthulhu_room function"""
[Harvey@/Users/Harvey/mystuff]python
Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import extest
>>> help(extest)
>>> help(extest.cthulhu_room)

(3)dict的 items(), keys(), values()
Python的字典的items(), keys(), values()都返回一个list

[python] view plaincopy
>>> dict = { 1 : 2, 'a' : 'b', 'hello' : 'world' }  
>>> dict.values()  
['b', 2, 'world']  
>>> dict.keys()  
['a', 1, 'hello']  
>>> dict.items()  
[('a', 'b'), (1, 2), ('hello', 'world')]  
>>> 
0 0
原创粉丝点击