py_memo1
来源:互联网 发布:洛阳网站建设启辰网络 编辑:程序博客网 时间:2024/06/15 18:31
python_memo1
python编码风格和js相差不大,唯一让人惊讶的是字符串的切片功能.
>>> a = 'it is tetsing';>>> print a[:];it is tetsing
python字符串第二个让人惊讶的是可以使用*操作符
>>> a = '#';>>> print 40*a;########################################
序列类型数据元组
声明的时候,若只有一个元素,则必须在结尾处添加一个,符号
>>> a = (12);>>> type(a);<type 'int'>>>> a = (12,);>>> type(a);<type 'tuple'>
在下标操作的情况下,元组和字符串具有类似的特性,即不可更改性.
>>> a = (1,2,3);>>> a[0]=12;Traceback (most recent call last): File "<pyshell#195>", line 1, in <module> a[0]=12;TypeError: 'tuple' object does not support item assignment>>> b = '123';>>> b[0] = 3;Traceback (most recent call last): File "<pyshell#197>", line 1, in <module> b[0] = 3;TypeError: 'str' object does not support item assignment
而和列表比较起来的话,列表具有可操作性,无论是怎样的组合.
>>> c = (1,3,[12,3]);>>> c[0]=3;Traceback (most recent call last): File "<pyshell#199>", line 1, in <module>c[0]=3;TypeError: 'tuple' object does not support item assignment>>> c[2][1] = 120;>>> print c;(1, 3, [12, 120])