Python: Commutative operation
"An operation will not change if the operands are moved” is one of the basic laws of arithmetic, also called the commutative law. A binary operation is considered commutative if you get the same result even if you swap the operands. In this case, addition is a commutative operation: `3 + 2 = 2 + 3'.
But subtraction is not a commutative operation: `2 - 3 ≠ 3 - 2'. In programming, this law applies just like it does in arithmetic. Moreover, most of the operations we encounter in real life are not commutative. Here's the takeaway: always pay attention to the order of things you work with.
Instructions
Write a program that calculates and displays sequentially (one value per line) the values of the following mathematical expressions: "3 to the power of 5" and "-8 divided by -4".
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Python: Commutative operation
"An operation will not change if the operands are moved” is one of the basic laws of arithmetic, also called the commutative law. A binary operation is considered commutative if you get the same result even if you swap the operands. In this case, addition is a commutative operation: `3 + 2 = 2 + 3'.
But subtraction is not a commutative operation: `2 - 3 ≠ 3 - 2'. In programming, this law applies just like it does in arithmetic. Moreover, most of the operations we encounter in real life are not commutative. Here's the takeaway: always pay attention to the order of things you work with.
Instructions
Write a program that calculates and displays sequentially (one value per line) the values of the following mathematical expressions: "3 to the power of 5" and "-8 divided by -4".
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Your exercise will be checked with these tests:
import importlib
def test(capsys):
expected = "256\n3.0"
expect_output(capsys, expected)
def expect_output(capsys, expected):
importlib.import_module('solution')
out, _err = capsys.readouterr()
actual = out.strip('\n')
with capsys.disabled():
print('\n')
print(out)
assert actual == expectedTeacher's solution will be available in:
20:00
