ch 2.5, 2.6, 2.7, 2.8

Ace your homework & exams now with Quizwiz!

The division operator / performs division and returns a floating-point number. Ex: 20 / 10 is 2.0. 50 / 50 is 1.0. 5 / 10 is 0.5. The floored division operator // can be used to round down the result of a floating-point division to the closest whole number value. The resulting value is an integer type if both operands are integers; if either operand is a float, then a float is returned: 20 // 10 is 2. 50 // 50 is 1. 5 // 10 is 0. (5/10 is 0 and the remainder 5 is thrown away). 5.0 // 2 is 2.0

...

Determine the result. Type "Error" if appropriate. 1) 50 % 2 2) 51 % 2 3) 78 % 10 4) 596 % 10 5) 100 % (1 // 2)

1) 0 2) 1 3) 8 4) 6 5) error

Determine the result. Type "Error" if the program would terminate due to division by 0. If the answer is a floating-point number, answer in the form #.#, even if the answer is a whole number. 1) 12 / 4 2) 5 / 10 3) 5.0 // 2 4) 100 / 0

1) 3.0 2) 0.5 3) 2.0 4) error

1) A file containing Python code that is passed as input to the interpreter 2) A file containing Python code that is imported by a script, module, or the interactive interpreter 3) Used to reference an object in an imported module. 4) Executes the contents of a file containing Python code and makes the definitions from that file available.

1) script 2) module 3) dot notation 4) import

Indicate which are valid expressions. x and y are variables. Valid/invalid? 1) x + 1 2) 2 * (x - y) 3) x 4) 2 5) 2x 6) 2 + (xy) 7) y = x + 1

1) valid 2) valid 3) valid - An expression can just be a variable. 4) valid - An expression can just be a literal. 5) not valid 6) not valid - xy, is not usually allowed, because xy could be the name of another variable. 7) not valid - (x + 1) alone is an expression, but y = x + 1 is an assignment and not itself an expression.

Does the expression correctly capture the intended behavior? 1) 6 plus num_items: 6 + num_items 2) 6 times num_items: 6 x num_items 3) total_days divided by 12: total_days / 12 4) 5 times t: 5t 5) The negative of user_val: -user_val 6) n factorial n!

1) yes 2) no 3) yes 4) no 5) yes 6) no - Python doesn't use the ! symbol for factorial.

Is the following an error? num_years = 1,999,999,999

yes, Commas aren't allowed in an integer literal.

1) Given a non-negative number x, which yields a number in the range 5 - 10? a) x % 5 b) x % 10 c) x % 11 d) (x % 6) + 5 2) Given a non-negative number x, which yields a number in the range -10 to 10? a) x % -10 b) (x % 21) - 10 c) ( x % 20) - 10 3) Which get the tens digit of x. Ex: If x = 693, which yields 9? a) x %10 b) x % 100 c) (x // 10) % 10 4) Given a 16-digit credit card number stored in x, which gets the last (rightmost) four digits? (Assume the fourth digit from the right is non-zero). a) x / 10000 b) x % 10000

1)d) (x % 6) + 5 2)b) (x % 21) -10 3)c) (x // 10) % 10 4)b) x % 10000 x / 10000 shifts right by four (and thus loses the right four digits), rather than getting the right four digits. x % 10000 yields 0 - 9999, being the rightmost four digits. To get other digits like the next four digits, divide first to shift the desired digits to the rightmost digits, then use % to get just those digits.

+ The addition operator is +, as in x + y. - The subtraction operator is -, as in x - y. Also, the - operator is for negation, as in -x + y, or x + -y. * The multiplication operator is *, as in x * y. / The division operator is /, as in x / y. ** The exponent operator is **, as in x ** y (x to the power of y).

Arithmetic operators:

The modulo operator (%) evaluates the remainder of the division of two integer operands. Ex: 23 % 10 is 3. Examples: 24 % 10 is 4. Reason: 24 / 10 is 2 with remainder 4. 50 % 50 is 0. Reason: 50 / 50 is 1 with remainder 0. 1 % 2 is 1. Reason: 1 / 2 is 0 with remainder 1.

Modulo (%) - used to get the remained

An __________ is a combination of items, like variables, literals, operators, and parentheses, that evaluates to a value, like 2 * (x + 1)

expression

A __________ is a specific value in code like 2. An __________ is a symbol that performs a built-in calculation, like +, which performs addition

literal operator

num_atoms is initially 7. What is num_atoms after: num_atoms *= 2?

num_atoms = num_atoms * 2 num_atoms = 7 * 2 = 14

num_atoms is initially 7. What is num_atoms after: num_atoms += 5?

num_atoms = num_atoms + 5 num_atoms = 7 + 5 = 12


Related study sets

Foundations: Health Studies Midterm Review

View Set

Round 4 (Valley Forge and Saratoga)

View Set

MGMT 10, MGMT 13, MGMT 11, MGMT 12

View Set