Exam2SheetCSE231 - `%' is the remainder (i.e....

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
‘%’ is the remainder (i.e. 12%6,%4,%3,%2,=0 because it is evenly divisible by those numbers) #purpose-function takes in a single integer argument outputs tuple of the number and the sum of its factors: #returns tuple, (original number, sumOfFactors) print "%s is %d years old" %("Bill", 25) #(prints) Bill is 25 years old #s stands for sting #d stands for digits %s string %d decimal %e floating point exponent %f floating point decimal %u unsigned integer and others -- fileObject.read() Reads the entire contents of the file as a string and returns it. It can take an optional argument integer to limit the read to N bytes, that is fileObject.read(N) -- fileObject.readline() Returns the next line as a string. fileObject.readLines() Returns a single list of all the lines from the file. for line in fileObject: iterator to go through the lines of a file fileObject.write(s) Writes the string s to the file. fileObject.writelines(list) Writes a list of strings (one at a time) to the file. fileObject.close() One way is a “list comprehension” [ n**2 for n in range(1,6)] returns [1,4,9,16,25] [c for c in “Hi There Mom” if c.isupper()] The “if” part of the comprehensive controls which of the iterated values is collected at the end. Only those values which make the if part true will be collected. returns [‘H’,’T’,’M’] mySet=set(“abc”); newSet=set(“abcdef”) mySet.issubset(newSet) returns True newSet.issuperset(mySet) returns True mySet=set(“abcd”); newSet=set(“cdef”) mySet.symmetric_difference(newSet)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/19/2008 for the course CSE 231 taught by Professor Punch during the Fall '07 term at Michigan State University.

Page1 / 3

Exam2SheetCSE231 - `%' is the remainder (i.e....

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online