I am a VBA user who started studying machine learning. As a memorandum, I would like to summarize the Python / R grammar while comparing it with VBA.
table of contents
-Basic Arithmetic -Four arithmetic operations -Exponentiation and integer division -Sign Inversion and Absolute Value -Summary -List -Whole program
First, there are four arithmetic operations (addition, subtraction, multiplication, division). The four arithmetic operators +
, -
,*
, and/
are common to all languages.
Python
Python3
print( 1 + 2 ) #Addition
print( 3 - 4 ) #Subtraction
print( 5 * 6 ) #Multiply
print( 7 / 8 ) #division
In Python2, if the divisor (divided number) and the divisor (divided number) are integers, dividing by /
results in rounded division.
R
R
print( 1 + 2 ) #Addition
print( 3 - 4 ) #Subtraction
print( 5 * 6 ) #Multiply
print( 7 / 8 ) #division
VBA
VBA
Debug.Print 1 + 2 'Addition
Debug.Print 3 - 4 'Subtraction
Debug.Print 5 * 6 'Multiply
Debug.Print 7 / 8 'division
Next is the calculation of exponentiation (power) and integer division (quotient (integer quotient) and remainder (remainder)). Operators differ depending on the language.
Python
Python3
print( 2 ** 3 ) #Exponentiation
print( 5 // 3 ) #Integer quotient
print( 5 % 3 ) #Surplus
print( divmod(5, 3) ) #Integer quotient and remainder
The Python divmod
function returns tuples. The result of divmod (5, 3)
is tuple(1, 2)
.
R
R
print( 2 ^ 3 ) #Exponentiation
print( 5 %/% 3 ) #Integer quotient
print( 5 %% 3 ) #Surplus
VBA
VBA
Debug.Print 2 ^ 3 'Exponentiation
Debug.Print 5 \ 3 'Integer quotient
Debug.Print 5 Mod 3 'Surplus
Debug.Print Int(5 / 3) 'Integer quotient (get the integer part with the Int function)
The backslash "" in the VBA integer quotient calculation "5 \ 3" is a half-width yen mark.
Finally, sign inversion and absolute value calculation.
Python
Python3
print( -2 ) #Sign inversion
print( abs(-2) ) #Absolute value
R
R
print( -2 ) #Sign inversion
print( abs(-2) ) #Absolute value
VBA
VBA
Debug.Print -2 'Sign inversion
Debug.Print Abs(-1) 'Absolute value
List the operators and functions used in each language. For comparison, the calculation in EXCEL is also shown.
Calculation | Example | Python | R | VBA | EXCEL | result |
---|---|---|---|---|---|---|
Addition | 1 + 2 | 1 + 2 | 1 + 2 | =1+2 | 3 | |
Subtraction | 3 - 4 | 3 - 4 | 3 - 4 | =3-4 | -1 | |
Multiply | 5 * 6 | 5 * 6 | 5 * 6 | =5*6 | 30 | |
division | 7 / 8 | 7 / 8 | 7 / 8 | =7/8 | 0.875 | |
Exponentiation | 2 ** 3 | 2 ^ 3 | 2 ^ 3 | =2^3 | 8 | |
Integer quotient | 5 // 3 | 5 %/% 3 | 5 ¥ 3 | =QUOTIENT(5,3) | 1 | |
Surplus | 5 % 3 | 5 %% 3 | 5 Mod 3 | =MOD(5,3) | 2 | |
divmod(5, 3) | (1, 2) | |||||
Sign inversion | -2 | -2 | -2 | =-2 | -2 | |
Absolute value | abs(-2) | abs(-2) | Abs(-2) | =ABS(-2) | 2 |
The "¥" in VBA is a circle mark (a full-width circle mark is included, but it is actually a half-width circle mark).
The whole program used for reference is shown.
Python
Python3
#Four arithmetic operations
print( 1 + 2 ) #Addition
print( 3 - 4 ) #Subtraction
print( 5 * 6 ) #Multiply
print( 7 / 8 ) #division
#Exponentiation and integer division
print( 2 ** 3 ) #Exponentiation
print( 5 // 3 ) #Integer quotient
print( 5 % 3 ) #Surplus
print( divmod(5, 3) ) #Integer quotient and remainder
#Sign inversion and absolute value
print( -2 ) #Sign inversion
print( abs(-2) ) #Absolute value
R
R
#Four arithmetic operations
print( 1 + 2 ) #Addition
print( 3 - 4 ) #Subtraction
print( 5 * 6 ) #Multiply
print( 7 / 8 ) #division
#Exponentiation and integer division
print( 2 ^ 3 ) #Exponentiation
print( 5 %/% 3 ) #Integer quotient
print( 5 %% 3 ) #Surplus
#Sign inversion and absolute value
print( -2 ) #Sign inversion
print( abs(-2) ) #Absolute value
VBA
VBA
Sub test()
'Four arithmetic operations
Debug.Print 1 + 2 'Addition
Debug.Print 3 - 4 'Subtraction
Debug.Print 5 * 6 'Multiply
Debug.Print 7 / 8 'division
'Exponentiation and integer division
Debug.Print 2 ^ 3 'Exponentiation
Debug.Print 5 \ 3 'Integer quotient
Debug.Print 5 Mod 3 'Surplus
Debug.Print Int(5 / 3) 'Integer quotient (get the integer part with the Int function)
'Sign inversion and absolute value
Debug.Print -2 'Sign inversion
Debug.Print Abs(-1) 'Absolute value
End Sub
The backslash "" in the VBA integer quotient calculation "5 \ 3" is a half-width yen mark.
Recommended Posts