



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This is introductory course for computer science. Its about basic concepts involving in computer programming, structure and working. Key points in this lecture handout are: Tuples, Truncate, Erase Previous Contents, Immutable, Parenthesis in Tuples, Python
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Today
Reading
Use write() to add text to a file
def main():
outfile = open("test.txt", "w") outfile.write("spam\n") outfile.write("ham\n") outfile.write("eggs\n") outfile.close()
infile = open("test.txt", "r") for line in infile: print(line[:-1]) infile.close()
Can only write strings
What you can’t do with a tuple:
>>> t[0] = 15 ... TypeError: ’tuple’ object does not support item assignment
You can do tuple assignment ...
>>> (a, b) = (10, 20)
>>> a + b 30
Tuples often useful data structures ...
>>> z = zip([10, 20], [’a’, ’b’])
>>> list(z) [(10, ’a’), (20, ’b’)]
You can create tuples of length 1
>>> t = (10,) >>> len(t) 1
The parenthesis in tuples are optional (but standard to use)
>>> t = 10, 20
>>> type(t)
>>> a, b = t
>>> a + b 30
x, y = 10, 20
Q: What about this?
>>> my_list = [20, 10]
>>> my_list[0], my_list[1] = my_list[1], my_list[0]
>>> my_list [10, 20]
Q: How does this work?