CSCE 110 1nd EditionLecture 23Outline of Last Lecture:I. ImagesA. PixelsB. DrawingC. ColorsII. Drawing programsA. Simple drawingB. Drawing rectanglesC. Fun with drawingOutline of Current Lecture:I. Returning to binaryA. Built-in number converterB. Adding in binaryII. Writing filesCurrent Lecture:I. BinaryA. Built-in number converterIn a previous set of lecture notes, we went through how to convert numbers between base 10 and base 2. While it was useful to learn how manually, Python saves us a lot of trouble by having built-in functions that can convert between bases. To convert an integer to base 2, we write "bin()" with the integer in the parentheses:>>> bin(10)1010To convert from a non-base 10 number to an integer, we write "int()" with the series of 0's and 1's in quotation marks (it is a string), followed by a comma and the base we are converting from:>>> int("1010", 2)10We can have bases other than 2 and 10, using the same concept (using 7n):>>> int("11", 7)8These notes represent a detailed interpretation of the professor’s lecture. GradeBuddy is best used as a supplement to your own notes, not as a substitute.B. Adding in binaryWe will use the following four binary addition properties:0 + 0 = 00 + 1 = 11 + 0 = 11 + 1 = 10We will add binary vertically, and carry over values that exceed 1 + 1. For example, 1 + 1 + 1 just rolls back to as if we had 1 + 0 , but we would carry a 1 as well to the next values. We also carry the 1 of the 10 of 1 + 1.Here are some examples:If we wanted to avoid having to add in binary by hand, we could convert two binary numbers back to base 10 (using the built-in function), add the two integers, and convert the result back to binary.II. Writing filesIf we had a program that created output that we wanted to write as a new .txt file, we would type the following lines:output_file = open('output.txt', 'w')output_file.write(put whatever you want to add to the file here)In the first line, we initialize a variable (can have any name) that indicates we are writing a file. The "open()" is required, the first argument within the parentheses being the name of the new file and the second being the 'w', indicating we are writing a file. Each time this line is read, the file will be automatically overwritten. In the second line, we call our variable and use ".write()", with the text we want to print in the new file within the parentheses. If we want to add multiple lines, we can use "\n" at the end of each output line. Without this, each time we have "output_file.write()", the text in the parentheses will be joined with the text we last left off
View Full Document