DOC PREVIEW
TAMU CSCE 110 - Returning to binary
Type Lecture Note
Pages 3

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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

TAMU CSCE 110 - Returning to binary

Type: Lecture Note
Pages: 3
Documents in this Course
06-IO

06-IO

29 pages

21-OOP

21-OOP

8 pages

key

key

6 pages

21-OOP

21-OOP

8 pages

Load more
Download Returning to binary
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Returning to binary and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Returning to binary 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?