tuples Flashcards
Explain what tuples are?
The last collection type in our survey is the Python tuple.
Tuples construct simple
groups of objects. They work exactly like lists, except that tuples can’t be changed inplace
(they’re immutable) and are usually written as a series of items in parentheses,
not square brackets.
Although they don’t support as many methods, tuples share most
of their properties with lists. Here’s a quick look at the basics.
Tuples are:
Ordered collections of arbitrary objects
Like strings and lists, tuples are positionally ordered collections of objects (i.e.,
they maintain a left-to-right order among their contents); like lists, they can embed
any kind of object.
Accessed by offset
Like strings and lists, items in a tuple are accessed by offset (not by key); they
support all the offset-based access operations, such as indexing and slicing.
open a text file for writing to.
write a lines to file ‘goodbye text file’
‘hello text file’
then read each line 1 and then line 2 from the file
then read out each consecutive line by 1st calling a function then using a for loop to print out file.
> > > myfile = open(‘myfile.txt’, ‘w’) # Open for text output: create/empty
myfile.write(‘hello text file\n’) # Write a line of text: string
16
myfile.write(‘goodbye text file\n’)
18
myfile.close() # Flush output buffers to disk
myfile = open(‘myfile.txt’) # Open for text input: ‘r’ is default
myfile.readline() # Read the lines back
‘hello text file\n’
myfile.readline()
‘goodbye text file\n’
myfile.readline() # Empty string: end of file
‘’
open(‘myfile.txt’).read() # Read all at once into string
‘hello text file\ngoodbye text file\n’
print(open(‘myfile.txt’).read()) # User-friendly display
hello text file
goodbye text file
>>> for line in open('myfile'): # Use file iterators, not reads ... print(line, end='') ... hello text file goodbye text file
create an object such as a dictionary with numbers in. open a pickle file to be used for writing to. place this dictionary in the file. close the file.
open the file again, load the pickle and then print of the E.
>>> D = {'a': 1, 'b': 2} >>> F = open('datafile.pkl', 'wb') >>> import pickle >>> pickle.dump(D, F) # Pickle any object to file >>> F.close()
> > > F = open(‘datafile.pkl’, ‘rb’)
E = pickle.load(F) # Load any object from file
E
{‘a’: 1, ‘b’: 2}
> > > L = [‘abc’, [(1, 2), ([3], 4)], 5]
L[1]
> > > L[1][1]
> > > L[1][1][0]
> > > L[1][1][0][0]
>>> 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'] # Embed references to X's object >>> D = {'x':X, 'y':2} >>> X[1] = 'surprise' >>> D >>> L = [1,2,3] >>> D = {'a':1, 'b':2} >>> A = L[:] # Instead of A = L (or list(L)) >>> B = D.copy() # Instead of B = D (ditto for sets) >>> A[1] = 'Ni' >>> B['c'] = 'spam' >>> >>> L, D
> > > A, B
>>> X = [1, 2, 3] >>> L = ['a', X, 'b'] # Embed references to X's object >>> D = {'x':X, 'y':2} >>> X[1] = 'surprise' # Changes all three references! >>> L ['a', [1, 'surprise', 3], 'b'] >>> D {'x': [1, 'surprise', 3], 'y': 2}
> > > L = [1,2,3]
D = {‘a’:1, ‘b’:2}
> > > A = L[:] # Instead of A = L (or list(L))
B = D.copy() # Instead of B = D (ditto for sets)
>>> 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, Y, Z = 43, 44, 45 # Native Python objects
S = ‘Spam’ # Must be strings to store in file
D = {‘a’: 1, ‘b’: 2}
L = [1, 2, 3]
> > > X, Y, Z = 43, 44, 45 # Native Python objects
S = ‘Spam’ # Must be strings to store in file
D = {‘a’: 1, ‘b’: 2}
L = [1, 2, 3]F = open(‘datafile.txt’, ‘w’) # Create output file
F.write(S + ‘\n’) # Terminate lines with \n
F.write(‘%s,%s,%s\n’ % (X, Y, Z)) # Convert numbers to strings
F.write(str(L) + ‘$’ + str(D) + ‘\n’) # Convert and separate with $
F.close()