The Nuances of Grouping in Python
You can put operations that have priority in brackets - this will not be an error. For example, let's put the product of numbers in brackets:
res = (2 * 2) + 3
print(res) # 7 (result 4 + 3)
In this case, the brackets are redundant (multiplication has priority anyway), but the code is acceptable. Sometimes such grouping is used in places where the priority of operations is not obvious. For example, consider the following code:
res = 8 / 2 * 4
print(res) # 16.0 (result 4 * 3)
As you already know, it will perform division first, then multiplication. But this may not be obvious at first glance. Here you can use grouping parentheses to make the priority explicit:
res = (8 / 2) * 4
print(res)
The following code is given:
res = 2 * 3 / 2
print(res)
Tell me what will be output to the console.
The following code is given:
res = (6 / 2) + 5
print(res)
Tell me what will be output to the console.