Operators ========= .. contents:: :local: Arithmetic operators -------------------- There are 7 arithmetic operators ``+``, ``-``, ``*``, ``/``, ``//``, ``%``, ``**``. Use them to complete the lines of code to get the requested results. The precedence of the order of operations can be changed with parentheses ``(``, ``)``. .. interactive_code_block:: :caption: Add the required elementary arithmetic operators +, -, *, / a = 1 b = 2 c = 3 # Complete the next line of code that the result is d = a b c # = 7 print(d) .. SOLUTIONS: d = a + b * c # 7 .. interactive_code_block:: :caption: Add the required arithmetic operators division /, floor division //, and modulo % a = 7 b = 3 # Complete the next 3 lines of code that the results are c = a b # = 1 d = a b # = 2 e = a b # = 2.333333333333333 print(c, d, e, sep='\\n') .. SOLUTIONS: c = a % b # = 1 d = a // b # = 2 e = a / b # = 2.333333333333333 .. interactive_code_block:: :caption: Add the required elementary arithmetic operators +, -, *, /, parentheses (, ) or the exponentiation operator ** a = 1 b = 2 c = 3 # Complete the next 2 lines of code that the result is d = a b c # = 9 e = a b c # = 9 # Find 2 different solutions print(d, e, sep='\\n') .. SOLUTIONS: d = a + b ** c # = 9 e = (a + b) * c # = 9 Compound assignments -------------------- There are 7 compound assignments ``+=``, ``-=``, ``*=``, ``/=``, ``//=``, ``%=``, ``**=``. By writing e.g. ``a += 1`` they are shorter version of ``a = a + 1``. .. interactive_code_block:: :caption: Add the required compound assignments +=, -=, *=, /=, //=, %=, **= a = 1 b = 2 c = 3 # Complete the next 3 lines of code that the results are a b # a = -1 b c # b = 8 c a # c = -3.0 print(a, b, c, sep='\\n') .. SOLUTIONS: a -= b # a -> -1 b **= c # b -> 8 c /= a # c -> -3.0 .. SOLUTION: d = a + b * c # 7 Comparison operators -------------------- There are 6 comparison operators ``<``, ``<=``, ``==``, ``>=``, ``>``, ``!=``. Keep in mind that * the result of an expression with a comparison operator is always boolean, i.e. ``True`` or ``False``, * comparison operators have the lowest precedence, i.e. ``a + b < c`` is the same as ``(a + b) < c``, * and that ``True`` or ``False`` is treated as ``1`` or ``0`` if used in an arithmetic expression, i.e. ``(True + 2) * 3`` is the same as ``(1 + 2) * 3``. .. interactive_code_block:: :caption: Add the required comparison operators <, <=, ==, >=, >, != a = 1 b = 2 c = 3 # Complete the next 3 lines of code that the results are d = a b # = True e = a b # = True f = a b # = True # Find 3 different solutions print(d, e, f, sep='\\n') .. SOLUTION: d = a != b # = True e = a < b # = True f = a <= b # = True .. interactive_code_block:: :caption: Add the required elementary arithmetic +, -, *, / and comparison operators <, <=, ==, >=, >, != a = 1 b = 2 c = 3 # Complete the next 2 lines of code that the results are d = a + b c # = True e = a > b c # = True print(d, e, sep='\\n') .. SOLUTION: d = a + b == c # = True e = a > b - c # = True .. interactive_code_block:: :caption: Add the required elementary arithmetic +, -, *, /, comparison operators <, <=, ==, >=, >, !=, and parentheses (, ) a = 1 b = 2 c = 3 # Complete the next 3 lines of code that the results are d = a (b > c) # = 0 e = a != b * c # = 3 f = a < b + b ** c # 9 print(d, e, f, sep='\\n') .. SOLUTION: e = a * (b > c) # = 0 d = (a != b) * c # = 3 f = (a < b) + b ** c # 9