Logo
/
Programming
/
Python Course
/

Composition of operations

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.

  1. First, we calculate 2 * 4 and get the expression 8 * 5 * 10
  2. Then 8 * 5. The result is 40 * 10
  3. Then, we get the last multiplication, which gives us 400

Operations may contain different operators, which raises the question of their priority.

Instructions

You are going to a store to buy gifts: 3 books at 200 each and 2 pens at 30 each. Calculate and print the total cost of the purchase.

3 * 200  +  2 * 30
    ↓           ↓
   600    +    60    =  660

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.
Found a bug? Have something to add? Pull requests are welcome!