Python tutorial #4 : arithmetic operations
Start IDLE(interactive mode)
For Terminal
python
↓
Arithmetic operations cheat sheet
x + y addition
x  y subtraction
x * y multiplication
x / y division
x // y Division (rounded down to the nearest whole number)
x % y Surplus
x Sign inversion
+x As it is
x ** y Exponentiation
↓
python arithmetic operations priority
↑ High priority
(), [], {}, list / tuple / dict
await x Await
** Exponentiation
+x, x, ~x Positive / Negative / Bitwise
*, @, /, //, % Multiplication / matrix multiplication / division / truncation division / remainder
+,  Addition and subtraction
<<, >> Shift operation
& Bitwise AND
^ Bitwise XOR
 Bitwise OR
in, not in, is, is not, Comparison including affiliation and identity tests
<, <=, >, >=, !=, == Comparison including affiliation and identity tests
not x Boolean operation NOT
and Boolean operation AND
or Boolean operation OR
if  else Conditional expression
lambda Lambda expression
↓ Low priority
1 + 2 * 3
= 7
1 + 3 / 7
= 0.4285714285714286
1 + 8 // 2
= 5
↓
Addition Addition + Operator
1 + 2
= 3
1.23 + 2.6
= 3.83
↓
Subtraction Operator
Unable to do subtraction in python
2  5
= 3
This is the code 2 + 5, so if you want to do a subtraction, you have to write it at the end of the column.
1  6 / 2
= 2.0
error
6 / 2  1
= 2.0
correct
↓
Multiplication *operator
2 * 3
= 6
6.5 * 20.5
= 133.25
↓
Division /Operator
6 / 2
= 3.0
3 / 7
= 0.42857142857142855
↓
25 / 4
= 6.25
Handling of decimal point
3 / 7
=
0.42857142857142855
Output in python
0.428571428571428571
Original answer
↓
3 / 11
=
0.2727272727272727
Output in python
0.27272727272727272
Original answer
↓
Division displays 17 digits after the decimal point. Numbers longer than 17 digits are not displayed even if they exist
↓
arithmetic operations Rounding
1 / 9 = 0.1111111111111111
2 / 9 = 0.2222222222222222
3 / 9 = 0.3333333333333333
4 / 9 = 0.4444444444444444
5 / 9 = 0.5555555555555556
6 / 9 = 0.6666666666666666
7 / 9 = 0.7777777777777778
8 / 9 = 0.8888888888888888
9 / 9 = 1.0
・For some reason, indivisible numbers between 5 and 7 are rounded up to the nearest whole number.
・Indivisible numbers in 0.999 ... are displayed as approximately 1.0
↓
Integer part of division Integer division //Operator omitted
Truncate
When performing division, it is recommended to use //
, which is rounded down to the nearest whole number, unless accurate calculation is required.
Reason
・Numbers (ints) that have a decimal point are called float types, which complicates the code. ・Execution speed takes about 10 times (slows down) ・Increased probability of human error
25 // 4
= 6
Handling of decimal points in //
1 // 9 = 0
2 // 9 = 0
3 // 9 = 0
4 // 9 = 0
5 // 9 = 0
6 // 9 = 0
7 // 9 = 0
8 // 9 = 0
9 // 9 = 1
↓
Division remainder remainder mod %operator Percentage
% Is often used mainly when using machine learning. Let's take the gross profit amount and the gross profit rate as an example.
・Cost
cost = 14
・Selling price
sales = 85
・Gross profit
gp = (sales  cost)
↓
print('{:,}'.format(gp))
↓
71
↓
・Gross profit margin
gpr = gp / sales
print('{:%}'.format(gpr))
↓
83.529412%
You can see that the gross margin is about 83%
↓
Exponentiation **Operator
4 ** 4
= 4 * 4 * 4 * 4
= 256
5 ** 3
= 5 * 5 * 5
= 125
Cubic centimeters Useful when calculating square centimeters

Recommended
 Windows 10 : wsl2 install & uninstall
 Python tutorial #1 : Install & Uninstall python3 with windows10
 Python tutorial #2 : Development environment construction with VScode
 Python tutorial #5 : Basic writing, sequential progress
 Python tutorial #3 : IDLE
 Python : Reserved words, identifiers, usable characters, invalid characters
↓
Thank you for watching until the end. I'm glad if you can use it as a reference.
↓
Thank you for Helping
Give money to us for Amazon gift
We will continue to update useful information. I am delighted to jump to your help. Thank you very much.