Numeric Expressions and math module
If the variable num contains a positive integer, what are the only two possible results of the expression num % 2?
0 and 1
Operator precedence
parenthesis, exponentiation, multiplication, true division, floor division, and the modulus operator, addition, subtraction
What is the result of the floor division operator? (//) 'integer division operator'
quotient after dividing one number into another so, 25 // 8 is 3
What does the exponentiation operator (**) do?
raises a number to a power. math.pow does essentially the same thing
How would you express 2,172 seconds in minutes and seconds?
seconds = 2172 mins = seconds // 60 secs = seconds % 60
circumference = 2 * math.pi * radius
since math.pi is a constant, not a function, it does not have parentheses following it where arguments would otherwise be specified.
What is the result of the division operator? (/)
always a floating point, regardless of the types of the operands. This operation is sometimes called 'true division'
math.pi
constant in the math module that represents the value of pi to 15 significant digits
What is the key difference between true division and floor division?
for floor division, the type of the result depends on the types of the operators. If either, or both, of the operands are floating-point, the result will be floating-point. If both operands are integer, the result will be integer.
Suppose the integer variable hours contains a value that represents a number of hours. Compute how many 24-hour periods are represented by that value, without worrying about any leftover hours.
hours // 24
To determine if a number is even or odd:
if num % 2 == 0: print(num, 'is even.') else: print(num, 'is odd.')
What is the modulus operator? (%)
The remainder operator. Produces the amount remaining after division.
math module constants:
math.pi = 3.141592653589793 math.e = 2.718281828459045
If both operands to the remainder operator (%) are positive, a divisor of n will produce a result in the range 0 to n-1.
True
If either or both operands to the division operator (//) are floating-point values, then the result will be a floating-point value.
True
Difference between ** and pow or math.pow?
math.pow function 'always' returns a floating-point value, whereas the ** operator and built in pow function will return an integer if both operands are an integer and same for floating-point values