1.Python三引号是为了解决特殊字符的,它允许一个字符串跨多行,字符串中可以包含换行符,制表符以及其他特殊字符。
2.使用extend()方法比连接操作的一个优点是它实际上是把新列表添加到了原有的列表里面,而不是像连接操作那样新建一个列表。
使用连接操作符不能实现向列表中添加新元素的操作。是因为我们在连接操作符的左右两边使用了不同类型的值。
3.无论list()还是tuple()都可能做完全的转换,也就是说,你传给tuple()的一个列表对象不可能变成一个元组。虽然前后两个对象(原来的和新的对象)有着相同的数据集合(所以相等==),但是变量指向的却不是同一个对象。
4.那些可以改变对象值的可变对象的方法是没有返回值的。
5.
stack = [];
def pushit():
stack.append(raw_input('Enter new string: ').strip());
def popit():
if len(stack) == 0:
print 'Cannot pop from an empty stack';
else:
print 'Removed[',stack.pop(),']';
def viewstack():
print stack;
CMDs = {'u':pushit, 'o':popit, 'v':viewstack};
def showmenu():
pr = """
P(U)sh
P(O)p
(V)iew
(Q)uit
Enter choice:""";
while True:
while True:
try:
choice = raw_input(pr).strip()[0].lower();
except (EOFError, KeyboardInterrupt, IndexError):
choice = 'q';
print '\nYour picked: [%s]' % choice;
if choice not in 'uovq':
print 'Invalid option, try again';
else:
break;
if choice == 'q':
break;
CMDs[choice]();
if __name__ == '__main__':
showmenu();
queue = [];
def enQ():
queue.append(raw_input('Enter new string: ').strip());
def deQ():
if len(queue) == 0:
print 'Cannot pop from an empty queue!';
else:
print 'Removed [', queue.pop(), ']';
def viewQ():
print queue;
CMDs = {'e':enQ, 'd':deQ, 'v':viewQ};
def showmenu():
pr = """
(E)nqueue
(D)equeue
(V)iew
(Q)uit
Enter choice: """;
while True:
while True:
try:
choice = raw_input(pr).strip()[0].lower();
except (EOFError, KeyboardInterrupt, IndexError):
choice = 'q';
print '\nYou picked: [%s]' % choice;
if choice not in 'devq':
print 'Invalid option, try again';
else:
break;
if choice == 'q':
break;
CMDs[choice]();
if __name__ == '__main__':
showmenu();
6.
虽然元组对象本身是 不可改变的,但这并不意味着元组包含的可变对象也不可变。
>>> t=(['xyz',123],23,-103.4)
>>> t
(['xyz', 123], 23, -103.4)
>>> t[0][1]
123
>>> t[0][1]=['abc','def']
>>> t
(['xyz', ['abc', 'def']], 23, -103.4)
7.所有函数返回的多对象(不包括有符号封装的)都是元组类型。有符号封装的多对象集合其实是返回的一个单一的容器对象。
8.
>>> person=['name',['savings', 100.00]]
>>> hubby=person[:]
>>> wifey=list(person)
>>> [id(x) for x in person, hubby, wifey]
[23585096, 23586136, 23594368]
>>> hubby[0]='Joe'
>>> wifey[0]='Jane'
>>> hubby,wifey
(['Joe', ['savings', 100.0]], ['Jane', ['savings', 100.0]])
>>> hubby[1][1]=50.0
>>> hubby,wifey
(['Joe', ['savings', 50.0]], ['Jane', ['savings', 50.0]])
>>>
在这两个列表的对象中,第一个对象是不可变的(是字符串类型),而第二个是可变的(一个列表)。当进行浅拷贝时,字符串被显示的拷贝,并创建了一个字符串对象,而列表元素只是把它的引用复制了一下,并不是它的成员。
>>> [id(x) for x in hubby]
[23590264, 23586056]
>>> [id(x) for x in wifey]
[23575200, 23586056]
非容器类型(比如数字,字符串和其他“原子”类型对象)没有拷贝一说,浅拷贝是用完全切片操作来完成的。
如果元组变量只包含原子类型对象,对它的深拷贝将不会进行。