Alterna · учебный материал

Arrhymetic expressions

34.3: Arifmetic expressions: Short theory, one example and a step-by-step Python.

Что в этом материале

Содержание и основные понятия

Python has regular operations, a whole division //, the remainder of % and the degree **.
substance and practice
Grade 8
OGE No. 6
01
How it works.
The arithmetic operator chooses not only the action but also the type of result. For the tasks on numbers and time, the whole part and the rest are particularly important.
/.
The normal division returns the flot even for two whole divided numbers.
//.
The whole division rounds down; for negative numbers, it's not zero.
%.
The balance is additional to /: a = (a / b) * b + a % b.
Priority.
The brackets, degree, multiplication/sharing, then addition/reading. ** is calculated from right to left.
02
One example in execution
Start the short program and go over the lines. The visualiser only shows the code, current line, variables and output.
For a non-negative whole
n
expression
n % 10
Selects the last digit, and
n // 10
It removes it. Instead of a long chain of calculations, it is better to enter intermediate names: then the path shows the meaning of each step. You cannot divide it by zero:
/
,
//
and
%
in this case give
ZeroDivisionError
.
Algorithm map
Read from left to right, then run the code.
1
Start
2
Segregate 29 by 6
3
Get private and residual
4
Remove q and r
5
End
03
Remember
XOR is a beaten XOR, not a degree.
The brackets make the calculation order verifiable.
For negative numbers, check separately the behaviour / and % on a specific example.
Syntax of this page
+ - *
Adding, subtracting and multiplying numbers.
/
Returns a fractional result like float.
//
Returns private with rounding down.
%
Returns the rest; the divisor shouldn't be zero.
**
Accuracy; 2 ** 3 ** 2 equals 2 ** 9.
abs(x)
Number x module.
round(x, n)
Rounds x to n; for financial purposes, specify the rounding rule.
divmod(a, b)
Returns the pair (a // b, a % b).
print(value)
Prints the value; print should not add extra invitations to the examination conclusion.
int(input())
Reads the line and converts it into a whole before calculating it.

Structural map of textbooks