Python,Tuples, Files
返回
>>> X = 'spam'
>>> Y = 'eggs'
>>> X, Y = Y, X
>>> X
'eggs'
>>> Y
'spam'
>>>
>>> X = 'spam'
>>> Y = 'eggs'
>>> X, Y = Y, X
>>> X
'eggs'
>>> Y
'spam'
>>>
>>> D = {}
>>> D[1] = 'a'
>>> D[2] = 'b'
>>> D[(1,2,3)] = 'c'
>>> D
{1: 'a', 2: 'b', (1, 2, 3): 'c'}
>>>
>>> L = [1, 2, 3]
>>> M = ['X', L, 'Y']
>>> M
['X', [1, 2, 3], 'Y']
>>> L[1] = 0
>>> M
['X', [1, 0, 3], 'Y']
>>> L = [1, 2, 3]
>>> M = ['X', L[:], 'Y']
>>> L[1] = 0
>>> L
[1, 0, 3]
>>> M
['X', [1, 2, 3], 'Y']
>>>
>>> L = [4, 5, 6]
>>> X = L * 4
>>> Y = [L] * 4
>>> X
[4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6]
>>> Y
[[4, 5, 6], [4, 5, 6], [4, 5, 6], [4, 5, 6]]
>>>
>>> L[1] = 0
>>> X
[4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6]
>>> Y
[[4, 0, 6], [4, 0, 6], [4, 0, 6], [4, 0, 6]]
>>>
>>> L = ['grail']
>>> L.append(L)
>>> L
['grail', [...]]
>>>
>>> T = (1, 2, 3)
>>> T[2] = 4
Traceback (most recent call last):
File "", line 1, in ?
TypeError: object doesn't support item assignment
>>> T = T[:2] + (4,)
>>>
>>> L = [1,2,3]
>>> D = {'a':1, 'b',2}
File "", line 1
D = {'a':1, 'b',2}
^
SyntaxError: invalid syntax
>>> D = {'a':1, 'b':2}
>>> A = L[:]
>>> B = D.copy()
>>> A[1] = 'Ni'
>>> B['c'] = 'spam'
>>>
>>> L,D
([1, 2, 3], {'a': 1, 'b': 2})
>>> A,B
([1, 'Ni', 3], {'a': 1, 'c': 'spam', 'b': 2})
>>> X = [1,2,3]
>>> L = ['a', X[:], 'b']
>>> D = {'x':X[:],'y':2}
>>>
>>> L1 = [1, ('a', 3(]
File "", line 1
L1 = [1, ('a', 3(]
^
SyntaxError: invalid syntax
>>> L1 = [1, ('a', 3)]
>>> L2 = [1, ('a', 3)]
>>> L1 == L2, L1 is L2
(True, False)
>>> S1 = 'spam'
>>> S2 = 'spam'
>>> S1 == S2, S1 is S2
(True, True)
>>> S1 = 'a longer string'
>>> S2 = 'a longer string'
>>> S1 == S2, S1 is S2
(True, False)
>>> L1 = [1, ('a', 3)]
>>> L2 = [1, ('a', 3)]
>>> L1 < L2, L1 == L2, L1 > L2
(False, True, False)
>>> L = [None] * 10
>>> L
[None, None, None, None, None, None, None, None, None, None]
>>>
>>> myfile = open('myfile.txt','w')
>>> myfile.write('hello text file\n')
>>> myfile.close()
>>> myfile = open('myfile','r')
>>> myfile.readline()
's4\n'
>>> myfile.readline()
''
>>> myfile = open('myfile.txt','r')
>>> myfile.readline()
'hello text file\n'
>>> myfile.readline()
''
>>>
>>> L = ['abc', [(1,2),([3],4)],5]
>>> L[1]
[(1, 2), ([3], 4)]
>>> L[1][1]
([3], 4)
>>> L[1][1][0]
[3]
>>> L[1][1][0][0]
3
>>> X = [1,2,3]
>>> L = ['a', X, 'b']
>>> D = {'x':X, 'y':2}
>>> X[1] = 'surprise'
>>> L
['a', [1, 'surprise', 3], 'b']
>>> D
{'y': 2, 'x': [1, 'surprise', 3]}
>>>
>>> L = [1,2,3]
>>> D = {'a':1, 'b':2}
>>> A = L[:]
>>> B = D.copy()
>>> A[1] = 'Ni'
>>> B['c'] = 'spam'
>>>
>>> L,D
([1, 2, 3], {'a': 1, 'b': 2})
>>> A,B
([1, 'Ni', 3], {'a': 1, 'c': 'spam', 'b': 2})
>>>
>>> X = [1,2,3]
>>> L = ['a',X[:], 'b']
>>> D = {'x':X[:], 'y':2}
>>>
>>> (1,2) + (3,4)
(1, 2, 3, 4)
>>> (1,2)*4
(1, 2, 1, 2, 1, 2, 1, 2)
>>> T = (1,2,3,4)
>>> T[0],T[1:3]
(1, (2, 3))
>>> x = (40)
>>> x
40
>>> y(40,)
Traceback (most recent call last):
File "", line 1, in ?
NameError: name 'y' is not defined
>>> y
Traceback (most recent call last):
File "", line 1, in ?
NameError: name 'y' is not defined
>>> T = ('cc', 'aa', 'dd', 'bb')
>>> tmp = list(T)
>>> tmp.sort()
>>> tmp
['aa', 'bb', 'cc', 'dd']
>>> T = tuple(tmp)
>>> T
('aa', 'bb', 'cc', 'dd')
>>> T = (1,[2,3],4)
>>> T[1][0] = 'spam'
>>> T
(1, ['spam', 3], 4)
>>> T[1] = 'spam'
Traceback (most recent call last):
File "", line 1, in ?
TypeError: object doesn't support item assignment
返回