(Python) Arithmetic expressions


Recall the assignment operator. General view can be written in this way
variable = expression
The expression on the right side of the assignment operator allows you to calculate the values of variables using various formulas.

What expression can contain
• integers and real numbers (in real numbers, the integer and fractional parts are separated by a dot, not a comma, as is customary in mathematics)
• signs of arithmetic operations:  
+ Addition
- subtraction
* Multiplication
/ Division
** exponentiation

•calls to standard functions (we give only part of a large set. All mathematical functions are described in the math library, which must be connected using the import math  string)
 abs(n) module of integer n
 math.fabs(x) modulus of a real number x
 math.sqrt(x) the square root of the real number x
 math.pow(x,y) computes x to the power of y

• parentheses to reorder

When changing the values of variables, it is convenient to use a shorthand notation
Full string Short string
a = a + b a +=  b
a = a - b a -=  b
a = a * b a *=  b
a = a / b a /=  b

 

Any programming language includes many built-in functions that can be used in arithmetic expressions.
To use additional functions, you often have to connect additional libraries.

For example, the most commonly used standard mathematical functions and their writing in Python

 abs(i) module of integer n
 math.fabs(x) modulus of a real number x
 math.sqrt(x) the square root of the real number x
 math.pow(x,y) computes x to the power of y

Remember that the function argument is always written in brackets.
For these functions to work, you need to connect an additional mathematical library (module).
You can do this by adding a line at the beginning of the program
import math
A detailed description of the functions that this module contains can be found on official site with documentation on the Python language

Rules for writing arithmetic expressions in a programming language

Suppose we need to calculate an expression written in mathematical form in this way:
\({2 * 17,56 ^2}\over {7 * 2,47 * 0,43}\)
 
Before writing a program that will calculate the result for us, we formulate the RULES for writing algebraic expressions in a programming language:
REMEMBER!
1. Expressions contain numbers, names of other variables, operation signs, parentheses, function names
2. Arithmetic operations and their signs (+, -, *, /, //,%,)
3. The separator of the integer and fractional parts is the point.
4. An expression is written in one in a line (linear record of expressions), characters are sequentially arranged one after another, ALL operation signs are affixed; parentheses are used

Thus, following the rules for writing arithmetic expressions, we must translate this (mathematical notation) fraction into a linear record, that is, write the fraction on one line.
Because both the numerator and the denominator are complex (that is, they contain two or more factors), then when writing to a linear form, it is necessary to take the expressions in the numerator and denominator in brackets.
Thus, a linear record of such an expression will look like this:
(2*17.56*17.56)/(7*2.47*0.43)
We will write a program to calculate this expression:
To do this, determine the input and output data

Input:  because we know all the values, then you don’t need to enter anything from the keyboard, therefore there will be no input data
Output: the program should display the result of this arithmetic expression (you can enter it into some variable, or you can immediately display the value on the screen).

We will immediately display the result of the expression without saving it in any variable.
Because if we have a fraction, then the result will be a real number
print((2 * 17.56 * 17.56) / (7 * 2.47 * 0.43))
Run the program on the computer and make sure that it produces a result equal to  82.94984330235246

After that, complete the task.
 
 

Result The division operation ("/") in Python can be a non-integer number or, as they say in programming, a real one. Often we need to get the integer and remainder of division. For example, we know the length of an object in centimeters and we need to find out how many whole meters it is and how many centimeters remain.
For instance:
435 centimeters = 4 meters 35 centimeters
This can be achieved if the number 435 is divided entirely by 100, that is, the integer part of the division by 100
35 centimeters can be obtained if we take the remainder of dividing 435 by 100.
In such cases, use the following operations
// - division completely
% - remainder of the division

The program can be written as follows:
length = 435
m = length // 100
cm = length % 100
REMEMBER
Integer division operations
1
   // - division completely
  % - remainder of the division