Python笔记一
2015-08-18 20:27
501 查看
(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')]
>>>
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')]
>>>
相关文章推荐
- 【Python】[面向对象编程] 访问限制,继承和多态
- Python nltk -- Sinica Treebank
- Python中有关list的增删改查
- python学习--windows下安装Numpy包的错误:Unable to find vcvarsall.bat
- python第三方库系列之二十二--subprocess的用法
- python视频教程大全集下载啦!超级全的教程!
- 我第一个Python教程笔记
- python中的有序字典OrderedDict
- Python3与Python2的区别
- 15 正则表达式 - 《Python 核心编程》
- Python学习 windows下面安装Python和pip(一)
- 14 执行环境 - 《Python 核心编程》
- python之路-----搭建django框架
- python 图片爬虫
- 浅析python 中__name__ = '__main__' 的作用
- vijos - P1494杨辉三角形中的偶数个数 (递归分治 + 记忆化搜索 + python)
- openCV—Python(11)—— 图像边缘检测
- python中MySQLdb模块用法实例
- python list遍历时删除元素的推荐做法
- 从错误中学python(4)——最小公约数与辗转相除法