DOC PREVIEW
TAMU CSCE 110 - Exam 1 Study Guide
Type Study Guide
Pages 10

This preview shows page 1-2-3 out of 10 pages.

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

Unformatted text preview:

CSCE 110 1nd EditionExam # 1 Study Guide Lectures: 1 - 9Lecture 1 (August 29)Introduction to Python and OperatorsClassify the following Python expressions as string, integer, float, or Boolean, and then determine the value of the output. a) 3 + 34This is an integer, as no decimal points are used in this mathematical operation. The returned output will be "37"b) 3 / 4This is an integer, as no decimal points are used. Even though we know the answer should be a decimal, Python is looking at a mathematical operation of integers, so its output will be an integer. The output here will be "0" because 4 does not go into 3 at all. These types of computations will always round down to the nearest integer. c) 3 / 4.0It doesn't matter which number has the decimal, but because one of these values is a float (has a decimal), the output will be a float as well. The output here will be "0.75".d) 14 % 11The modulus operator is used here. It represents the remainder of when we compute 14 / 11. And, since the two numbers are integers, the output "3" will be as well.e) 2 % 0This will receive an error because we cannot divide any number by 0.f) 4 + 5 * 6 / 3This is an example where we must use the order of operations. First we compute 5 * 6 = 30, then 30 / 3 = 10, then finally we have 4 + 10 = 14. The output "14" is an integer.g) (3.5 + 2.1) * 2Another example of using order of operations. We have 3.5 + 2.1 = 5.6, and 5.6 * 2 = 11.2, so our output "11.2" is a float.h) 3 != 4This is equivalent to saying 3 is not equal to four, which is correct. This is a Boolean expression and the output will be "True".i) 3=! 4This will receive an error because the exclamation point must come before the equal sign.j) 2**3This is equivalent to 2 to the 3rd power, and its output "8" is an integer.k) 3 == 3.0000This is a correct Boolean expression even though the number type is different for both, and the output will be "True".l) 'pig' > 'latin'Because "pig" comes after "latin" alphabetically, this is a correct Boolean expression and its output will be "True".m) 4 >= 2.12n) / 3 + 8This will receive an error because we cannot begin an operation with an operator.o) 3 > 5 and 3 < 4Because an "and" is used, both statements must be correct for the Boolean expression to have an output of True. But, since 3 is not greater than 5, the output will be "False".p) 3 > 5 or 3 < 4Because an "or" is used, we only need one of the of the statements to be correct for the Boolean expression to get our output of "True".q) () is an example of a tuple, which is a collection of items in a parentheses. This is NOT a list, which uses brackets [].Lecture 2 (September 3) Strings and VariablesDetermine the output of each scenario.>>> apple = 12345>>> print appleThe output here will be:>>> 12345We simply gave apple a value and then asked for that value.>>> x = '56' + 'string'>>> print xThe output here will be:>>> 56stringNotice that we can have numbers as strings if we place them in quotations. There will not be a space between the two values, as we just merged the two values into one.>>> x = 'yes' * 4>>> print xThe output here will be:>>> yesyesyesyesUsing the multiplication operator, we can repeat an entire string however many times we desire. Notice again how there are no spaces. Also, when we print a string, there will be no quotations marks, as opposed to when we simply call the variable.>>> 45 + TrueThe output here will be:>>> 46If we added a string to an integer or float, we would get an error back. This is not what happenswhen we add a number to a Boolean. With Booleans, a True is equivalent to 1, and False is equivalent to 0. >>> print "\" apple\""The output here will be:>>> "apple"We can put forward slashes in front of the quotations we wish to keep when printed. Lecture 3 (September 5) Indexes and Range Slices of StringsDetermine the output of each scenario, when x = "python">>> x[0]The index zero is the first character of a string.>>> 'p'>>> x[3+1]Notice how we can use mathematical expressions to signify a particular index. This is asking for the fourth index, or the third character, of the string.>>> 'o'>>> print x[-1]This is asking for the very last character of the string. Remember that we can have negative indexes, starting with -1 at the far right and decreasing by one all the way to the left. >>> n>>> print x[0:4]0 is the starting index, and 4 is the ending index. The command will return the characters from (and including) index 0, stopping at the character that is before index 4. >>> pyth>>> print x[0:]If we add a colon but do not enter an index number after it, the range slice will include every character from the starting index to the end of the string. >>> python>>> print x[:4]Similarly, if we do not add a starting index before the colon, the range will automatically start at the beginning of the string. >>> pyth>>> print x[:]If we do not add a starting or ending index, it is equivalent to asking for the full string.>>> python>>> print x[-4:]Do not confuse the negative sign with the direction the characters are called. Starting with index-4, the characters to the right of the character at x[-4] will be read. >>> thon>>> print x[2:2]This will give no output, since there is no range. It can't print x[2] because the ending index itself is never printed.>>> print x[0:6:2]The third number signifies the "step size." Starting with index 0 and ending with index 6, we will print every other character.>>> ptoWe would get the same output if we had:>>> print x[::2]>>> print x[::-1]If we have a step size that is negative, the string will be read from right to left. This is the only way we can print a string backwards. >>> nohtypLecture 4 (September 10)if/elif/else1 >>> x = 102 >>> if x == 5:3 >>> print 'A'4 >>> elif x ==50:5 >>> print 'B'6 >>> elif x > 4:7 >>> print 'C'8 >>> else:9 >>> print 'D'We can write as many "elif" functions as we want. With elif's, we can check multiple Boolean expressions, and the "else:" at the end still acts as a catch-all. What will the output of the program be?CThe program read line 1, read line 2, skipped line 3 because line 2 is a False expression, read line4, skipped line 5 because line 4 is a False expression, read line 6, read line 7, and stopped there.If we changed line 1 to “x = 5” in our program, what would the new output would be?AThe program would have read line 1, line 2, line 3, and would have stopped there because the rest of the program would no longer be relevant. Lecture 5


View Full Document

TAMU CSCE 110 - Exam 1 Study Guide

Type: Study Guide
Pages: 10
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 Exam 1 Study Guide
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 Exam 1 Study Guide 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 Exam 1 Study Guide 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?