Python: Composition of operations
Let's look at an example:
print(2 * 4 * 5 * 10)The operations can be combined with each other to calculate increasingly complex compound expressions. To give you an idea of how calculations are done inside the interpreter, let's look at an example: 2 * 4 * 5 * 10.
- First, we calculate
2 * 4and get the expression8 * 5 * 10 - Then
8 * 5. The result is40 * 10 - Then, we get the last multiplication, which gives us
400
Operations may contain different operators, which raises the question of their priority.
Instructions
Write a program that calculates and prints the value of this expression:
8 / 2 + 5 - -3 / 2Don't calculate anything manually, your program should do all the calculations on its own.
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: Composition of operations
Let's look at an example:
print(2 * 4 * 5 * 10)The operations can be combined with each other to calculate increasingly complex compound expressions. To give you an idea of how calculations are done inside the interpreter, let's look at an example: 2 * 4 * 5 * 10.
- First, we calculate
2 * 4and get the expression8 * 5 * 10 - Then
8 * 5. The result is40 * 10 - Then, we get the last multiplication, which gives us
400
Operations may contain different operators, which raises the question of their priority.
Instructions
Write a program that calculates and prints the value of this expression:
8 / 2 + 5 - -3 / 2Don't calculate anything manually, your program should do all the calculations on its own.
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 = "660"
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
