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
-Logical operations and comparison operations -Logical value -Logical value -Logical value type -Comparison operation -Comparison operator -Summary of two inequalities -Logical operation -Logical Operator -Confirm logical operation -Example of use in conditional expression -Logical value vector -Summary -List -Whole program
First, about logical values.
The logical values that represent true / false are True / False in Python, TRUE / FALSE in R, and True / False in VBA.
Python
Python3
print( True )
print( False )
R
R
print( TRUE )
print( FALSE )
print( T )     # TRUE
print( F )     # FALSE
In R, TRUE ・ FALSE can be abbreviated as T ・ F.
VBA
VBA
Debug.Print True
Debug.Print False
Check the data type of the logical value in each language. It is a bool type in Python, a logical type in R, and a Boolean type in VBA.
Python3
print( type(True) )  # <class 'bool'>
print( type(False) ) # <class 'bool'>
R
R
print( typeof(TRUE) ) # "logical"
print( class(TRUE) )  # "logical"
print( typeof(TRUE) ) # "logical"
print( class(TRUE) )  # "logical"
VBA
VBA
Debug.Print TypeName(True)   ' Boolean
Debug.Print TypeName(False)  ' Boolean
Next is the comparison operator. It can be used for both numbers and strings. The inequality sign is the same in all languages, but the operators for "=" and "≠" are different in each language.
Python
Python3
print( 12 == 13 )   # False
print( 12 <  13 )   # True
print( 12 >  13 )   # False 
print( 12 >= 13 )   # False
print( 12 <= 13 )   # True
print( 12 != 13 )   # True
print( "abc" == "def" )   # False
print( "abc" <  "def" )   # True
print( "abc" >  "def" )   # False
print( "abc" >= "def" )   # False
print( "abc" <= "def" )   # True
print( "abc" != "def" )   # True
The equal sign is two equals. One equal is substitution.
R
R
print( 12 == 13 )   # FALSE
print( 12 <  13 )   # TRUE
print( 12 >  13 )   # FALSE 
print( 12 >= 13 )   # FALSE
print( 12 <= 13 )   # TRUE
print( 12 != 13 )   # TRUE
print( "abc" == "def" )   # FALSE
print( "abc" <  "def" )   # TRUE
print( "abc" >  "def" )   # FALSE 
print( "abc" >= "def" )   # FALSE
print( "abc" <= "def" )   # TRUE
print( "abc" != "def" )   # TRUE
The equal sign is two equals. One equal is substitution.
VBA
VBA
Debug.Print (12 = 13)    ' False
Debug.Print (12 < 13)    ' True
Debug.Print (12 > 13)    ' False
Debug.Print (12 >= 13)   ' False
Debug.Print (12 <= 13)   ' True
Debug.Print (12 <> 13)   ' True
Debug.Print ("abc" = "def")    ' False
Debug.Print ("abc" < "def")    ' True
Debug.Print ("abc" > "def")    ' False
Debug.Print ("abc" >= "def")   ' False
Debug.Print ("abc" <= "def")   ' True
Debug.Print ("abc" <> "def")   ' True
The equal sign is one equal. One equal sign represents both the equal sign and the substitution.
It's about whether the two inequalities $ 0 <x $ and $ x <9 $ can be combined and written as $ 0 <x <9 $.
Python
Python3
x = 5
print( x > 0 )     # True
print( x < 9 )     # True   
print( 0 < x < 9 ) # True
print( 0 < x and x < 9 ) # True
0 <x <9 is valid in Python.
R
R
x <- 5
print( x > 0 )     # TRUE
print( x < 9 )     # TRUE   
print( 0 < x < 9 ) #error
print( 0 < x && x < 9 ) # TRUE
In R, writing 0 <x <9 will result in an error.
VBA
VBA
x = 5
Debug.Print (x > 0)     ' True
Debug.Print (x < 9)     ' True
Debug.Print (0 < x < 9) ' True
Debug.Print (0 < x And x < 9) ' True
0 <x <9 is valid in VBA.
About logical operator (Boolean operator) negation (NOT), logical product (AND), OR (OR), exclusive OR (NOR).
Python
Python3
print( not True  )   # False
print( not False )   # True
print( True  and True  )   # True
print( True  and False )   # False
print( False and True  )   # False 
print( False and False )   # False
print( True  or True  )    # True
print( True  or False )    # False
print( False or True  )    # False 
print( False or False )    # False
Python doesn't seem to have an XOR operator.
R
R
print( ! TRUE  )   # FALSE
print( ! FALSE )   # TRUE
print( TRUE  && TRUE  )   # TRUE
print( TRUE  && FALSE )   # FALSE
print( FALSE && TRUE  )   # FALSE 
print( FALSE && FALSE )   # FALSE
print( TRUE  || TRUE  )   # TRUE
print( TRUE  || FALSE )   # TRUE
print( FALSE || TRUE  )   # TRUE 
print( FALSE || FALSE )   # FALSE
print( xor(TRUE,  TRUE ) )  # FALSE
print( xor(TRUE,  FALSE) )  # TRUE
print( xor(FALSE, TRUE ) )  # TRUE
print( xor(FALSE, FALSE) )  # FALSE
VBA
VBA
Debug.Print (Not True)    ' False
Debug.Print (Not False)   ' True
Debug.Print (True And True)     ' True
Debug.Print (True And False)    ' False
Debug.Print (False And True)    ' False
Debug.Print (False And False)   ' False
Debug.Print (True Or True)      ' True
Debug.Print (True Or False)     ' True
Debug.Print (False Or True)     ' True
Debug.Print (False Or False)    ' False
Debug.Print (True Xor True)     ' False
Debug.Print (True Xor False)    ' True
Debug.Print (False Xor True)    ' True
Debug.Print (False Xor False)   ' False
Of course, the result of logical operation (Boolean operation) is the same in all languages, but check it just in case.
Python
Python3
#Negation (NOT)
for P in [True, False]:
    print(P, not P)
# True False
# False True
#Logical product (AND)
for P in [True, False]:
    for Q in [True, False]:
        print(P, Q, (P and Q))
# True True True
# True False False
# False True False
# False False False
#OR
for P in [True, False]:
    for Q in [True, False]:
        print(P, Q, P or Q)
# True True True
# True False True
# False True True
# False False False
R
R
#Negation (NOT)
for (P in c(TRUE, FALSE)) {
  print(c(P, (!P)))
}
# [1]  TRUE FALSE
# [1] FALSE  TRUE
#Logical product (AND)
for (P in c(TRUE, FALSE)) {
  for (Q in c(TRUE, FALSE)) {
    print(c(P, Q, (P && Q)))
  }
}
# [1] TRUE TRUE TRUE
# [1]  TRUE FALSE FALSE
# [1] FALSE  TRUE FALSE
# [1] FALSE FALSE FALSE
#OR
for (P in c(TRUE, FALSE)) {
  for (Q in c(TRUE, FALSE)) {
    print(c(P, Q, (P || Q)))
  }
}
# [1] TRUE TRUE TRUE
# [1]  TRUE FALSE  TRUE
# [1] FALSE  TRUE  TRUE
# [1] FALSE FALSE FALSE
#Exclusive OR (XOR)
for (P in c(TRUE, FALSE)) {
  for (Q in c(TRUE, FALSE)) {
    print(c(P, Q, xor(P, Q)))
  }
}
# [1]  TRUE  TRUE FALSE
# [1]  TRUE FALSE  TRUE
# [1] FALSE  TRUE  TRUE
# [1] FALSE FALSE FALSE
VBA
VBA
P = Array(True, False)
Q = Array(True, False)
'Negation (NOT)
For i = 0 To 1
  Debug.Print P(i), Not P(i)
Next i
' True          False
' False         True
'Logical product (AND)
For i = 0 To 1
    For j = 0 To 1
        Debug.Print P(i), Q(j), P(i) And Q(j)
    Next j
Next i
' True          True          True
' True          False         False
' False         True          False
' False         False         False
'OR
For i = 0 To 1
    For j = 0 To 1
        Debug.Print P(i), Q(j), P(i) Or Q(j)
    Next j
Next i
' True          True          True
' True          False         True
' False         True          True
' False         False         False
'Exclusive OR (XOR)
For i = 0 To 1
    For j = 0 To 1
        Debug.Print P(i), Q(j), P(i) Xor Q(j)
    Next j
Next i
' True          True          False
' True          False         True
' False         True          True
' False         False         False
This is an example of actually using a logical operation in a conditional expression of an If statement.
Python
Python3
x = 5
if (0 < x) and (x < 9):
    print('0 < x < 9')
else:
    print('x < 0, x < 9')
# 0 < x < 9
if (x <= 0) or (9 <= x):
    print('x <= 0, 9 <= x')
else:
    print('0 < x < 9')
# 0 < x < 9
if (0 < x < 9):
    print('0 < x < 9')
else:
    print('x < 0, x < 9')
# 0 < x < 9
R
R
x <- 5
if ((0 < x) && (x < 9)) {
  print("0 < x < 9")
} else {
  print("x <= 0, 9 <= x")
}
# [1] "0 < x < 9"
if ((x <= 0) || (9 <= x)) {
  print("x <= 0, 9 <= x")
} else {
  print("0 < x < 9")
}
# [1] "0 < x < 9"
VBA
VBA
x = 5
If ((0 < x) And (x < 9)) Then
    Debug.Print ("0 < x < 9")
Else
    Debug.Print ("x <= 0, 9 <= x")
End If
' 0 < x < 9
If ((x <= 0) Or (9 <= x)) Then
    Debug.Print ("x <= 0, 9 <= x")
Else
    Debug.Print ("0 < x < 9")
End If
' 0 < x < 9
If (0 < x < 9) Then
    Debug.Print ("0 < x < 9")
Else
    Debug.Print ("x <= 0, 9 <= x")
End If
' 0 < x < 9
In R, there are also logical operators & and |, which are used for vector operations of logical value vectors.
operator&&When||Is the calculation resultTRUEOrFALSE で返すのに対して、operator&When|Returns the operation result as a vector.
R
x <- c(TRUE, TRUE, FALSE, FALSE)   #Logical value vector
y <- c(TRUE, FALSE, TRUE, FALSE)   #Logical value vector
print( !x )
# FALSE FALSE  TRUE  TRUE
print( x & y )
# TRUE FALSE FALSE FALSE
print( x | y )
# TRUE  TRUE  TRUE FALSE
print( xor(x, y) )
# FALSE  TRUE  TRUE FALSE
List the operators used in each language. For comparison, the calculation in EXCEL is also shown.
*** Logical value ***
| Logical value | Python | R | VBA | EXCEL | 
|---|---|---|---|---|
| true | True | TRUE  T  | 
True | TRUE | 
| false | False | False  F  | 
False | FALSE | 
| Data type | bool | logical | Boolean | 
*** Comparison operator ***
| operator | Python | R | VBA | EXCEL | 
|---|---|---|---|---|
| = | == | == | = | = | 
| < | < | < | < | < | 
| > | > | > | > | > | 
| ≦ | >= | >= | >= | >= | 
| ≧ | <= | <= | <= | >= | 
| ≠ | != | != | <> | <> | 
*** Logical Operators ***
| operator | Python | R | VBA | EXCEL | 
|---|---|---|---|---|
| denial NOT  | 
not P | !P | NOT P | =NOT(P) | 
| Logical AND AND  | 
P and Q | P && Q P & Q  | 
P And Q | =AND(P,Q) | 
| Logical sum OR  | 
P or Q | P || Q P | Q  | 
P Or Q | =OR(P,Q) | 
| Exclusive OR XOR  | 
xor(P, Q) | P Xor Q | =XOR(P,Q) | 
*** Confirm logical operation ***
| Calculation | Calculation子 | formula | result | 
|---|---|---|---|
| denial | NOT | NOT T NOT F  | 
F T  | 
| Logical AND | AND | T AND T T AND F F AND T F AND T  | 
T F F F  | 
| Logical sum | OR | T OR T T OR F F OR T F OR T  | 
T T T F  | 
| Exclusive OR | XOR | T NOR T T XOR F F XOR T F XOR T  | 
F T T F  | 
Note) Notated as T: true and F: false.
The whole program used for reference is shown.
Python
Python3
#Logical value
print( True )
print( False )
print( type(True) )  # <class 'bool'>
print( type(False) ) # <class 'bool'>
#Comparison operator
print( 12 == 13 )   # False
print( 12 <  13 )   # True
print( 12 >  13 )   # False 
print( 12 >= 13 )   # False
print( 12 <= 13 )   # True
print( 12 != 13 )   # True
print( "abc" == "def" )   # False
print( "abc" <  "def" )   # True
print( "abc" >  "def" )   # False
print( "abc" >= "def" )   # False
print( "abc" <= "def" )   # True
print( "abc" != "def" )   # True
x = 5
print( x > 0 )     # True
print( x < 9 )     # True   
print( 0 < x < 9 ) # True
print( 0 < x and x < 9 ) # True
#Logical operator (Boolean operator)
print( not True  )   # False
print( not False )   # True
print( True  and True  )   # True
print( True  and False )   # False
print( False and True  )   # False 
print( False and False )   # False
print( True  or True  )    # True
print( True  or False )    # False
print( False or True  )    # False 
print( False or False )    # False
#Negation (NOT)
for P in [True, False]:
    print(P, not P)
# True False
# False True
#Logical product (AND)
for P in [True, False]:
    for Q in [True, False]:
        print(P, Q, (P and Q))
# True True True
# True False False
# False True False
# False False False
#OR
for P in [True, False]:
    for Q in [True, False]:
        print(P, Q, P or Q)
# True True True
# True False True
# False True True
# False False False
x = 5
if (0 < x) and (x < 9):
    print('0 < x < 9')
else:
    print('x < 0, x < 9')
# 0 < x < 9
if (x <= 0) or (9 <= x):
    print('x <= 0, 9 <= x')
else:
    print('0 < x < 9')
# 0 < x < 9
if (0 < x < 9):
    print('0 < x < 9')
else:
    print('x < 0, x < 9')
# 0 < x < 9
R
R
#Logical value
print( TRUE )
print( FALSE )
print( T )     # TRUE
print( F )     # FALSE
print( typeof(TRUE) ) # "logical"
print( class(TRUE) )  # "logical"
print( typeof(TRUE) ) # "logical"
print( class(TRUE) )  # "logical"
#Comparison operator
print( 12 == 13 )   # FALSE
print( 12 <  13 )   # TRUE
print( 12 >  13 )   # FALSE 
print( 12 >= 13 )   # FALSE
print( 12 <= 13 )   # TRUE
print( 12 != 13 )   # TRUE
print( "abc" == "def" )   # FALSE
print( "abc" <  "def" )   # TRUE
print( "abc" >  "def" )   # FALSE 
print( "abc" >= "def" )   # FALSE
print( "abc" <= "def" )   # TRUE
print( "abc" != "def" )   # TRUE
x <- 5
print( x > 0 )     # TRUE
print( x < 9 )     # TRUE   
print( 0 < x < 9 ) #error
print( 0 < x && x < 9 ) # TRUE
#Logical operator (Boolean operator)
print( ! TRUE  )   # FALSE
print( ! FALSE )   # TRUE
print( TRUE  && TRUE  )   # TRUE
print( TRUE  && FALSE )   # FALSE
print( FALSE && TRUE  )   # FALSE 
print( FALSE && FALSE )   # FALSE
print( TRUE  || TRUE  )   # TRUE
print( TRUE  || FALSE )   # TRUE
print( FALSE || TRUE  )   # TRUE 
print( FALSE || FALSE )   # FALSE
print( xor(TRUE,  TRUE ) )  # FALSE
print( xor(TRUE,  FALSE) )  # TRUE
print( xor(FALSE, TRUE ) )  # TRUE
print( xor(FALSE, FALSE) )  # FALSE
#Negation (NOT)
for (P in c(TRUE, FALSE)) {
  print(c(P, (!P)))
}
# [1]  TRUE FALSE
# [1] FALSE  TRUE
#Logical product (AND)
for (P in c(TRUE, FALSE)) {
  for (Q in c(TRUE, FALSE)) {
    print(c(P, Q, (P && Q)))
  }
}
# [1] TRUE TRUE TRUE
# [1]  TRUE FALSE FALSE
# [1] FALSE  TRUE FALSE
# [1] FALSE FALSE FALSE
#OR
for (P in c(TRUE, FALSE)) {
  for (Q in c(TRUE, FALSE)) {
    print(c(P, Q, (P || Q)))
  }
}
# [1] TRUE TRUE TRUE
# [1]  TRUE FALSE  TRUE
# [1] FALSE  TRUE  TRUE
# [1] FALSE FALSE FALSE
#Exclusive OR (XOR)
for (P in c(TRUE, FALSE)) {
  for (Q in c(TRUE, FALSE)) {
    print(c(P, Q, xor(P, Q)))
  }
}
# [1]  TRUE  TRUE FALSE
# [1]  TRUE FALSE  TRUE
# [1] FALSE  TRUE  TRUE
# [1] FALSE FALSE FALSE
x <- 5
if ((0 < x) && (x < 9)) {
  print("0 < x < 9")
} else {
  print("x <= 0, 9 <= x")
}
# [1] "0 < x < 9"
if ((x <= 0) || (9 <= x)) {
  print("x <= 0, 9 <= x")
} else {
  print("0 < x < 9")
}
# [1] "0 < x < 9"
#Logical value vector
x <- c(TRUE, TRUE, FALSE, FALSE)   #Logical value vector
y <- c(TRUE, FALSE, TRUE, FALSE)   #Logical value vector
print( !x )
# FALSE FALSE  TRUE  TRUE
print( x & y )
# TRUE FALSE FALSE FALSE
print( x | y )
# TRUE  TRUE  TRUE FALSE
print( xor(x, y) )
# FALSE  TRUE  TRUE FALSE
VBA
VBA
Sub test_bool()
Dim x As Integer
Dim P As Variant
Dim Q As Variant
Dim i As Integer
Dim j As Integer
'Logical value
Debug.Print True
Debug.Print False
Debug.Print TypeName(True)   ' Boolean
Debug.Print TypeName(False)  ' Boolean
'Comparison operator
Debug.Print (12 = 13)    ' False
Debug.Print (12 < 13)    ' True
Debug.Print (12 > 13)    ' False
Debug.Print (12 >= 13)   ' False
Debug.Print (12 <= 13)   ' True
Debug.Print (12 <> 13)   ' True
Debug.Print ("abc" = "def")    ' False
Debug.Print ("abc" < "def")    ' True
Debug.Print ("abc" > "def")    ' False
Debug.Print ("abc" >= "def")   ' False
Debug.Print ("abc" <= "def")   ' True
Debug.Print ("abc" <> "def")   ' True
x = 5
Debug.Print (x > 0)     ' True
Debug.Print (x < 9)     ' True
Debug.Print (0 < x < 9) ' True
Debug.Print (0 < x And x < 9) ' True
'Logical operator (Boolean operator)
Debug.Print (Not True)    ' False
Debug.Print (Not False)   ' True
Debug.Print (True And True)     ' True
Debug.Print (True And False)    ' False
Debug.Print (False And True)    ' False
Debug.Print (False And False)   ' False
Debug.Print (True Or True)      ' True
Debug.Print (True Or False)     ' True
Debug.Print (False Or True)     ' True
Debug.Print (False Or False)    ' False
Debug.Print (True Xor True)     ' False
Debug.Print (True Xor False)    ' True
Debug.Print (False Xor True)    ' True
Debug.Print (False Xor False)   ' False
P = Array(True, False)
Q = Array(True, False)
'Negation (NOT)
For i = 0 To 1
  Debug.Print P(i), Not P(i)
Next i
' True          False
' False         True
'Logical product (AND)
For i = 0 To 1
    For j = 0 To 1
        Debug.Print P(i), Q(j), P(i) And Q(j)
    Next j
Next i
' True          True          True
' True          False         False
' False         True          False
' False         False         False
'OR
For i = 0 To 1
    For j = 0 To 1
        Debug.Print P(i), Q(j), P(i) Or Q(j)
    Next j
Next i
' True          True          True
' True          False         True
' False         True          True
' False         False         False
'Exclusive OR (XOR)
For i = 0 To 1
    For j = 0 To 1
        Debug.Print P(i), Q(j), P(i) Xor Q(j)
    Next j
Next i
' True          True          False
' True          False         True
' False         True          True
' False         False         False
x = 5
If ((0 < x) And (x < 9)) Then
    Debug.Print ("0 < x < 9")
Else
    Debug.Print ("x <= 0, 9 <= x")
End If
' 0 < x < 9
If ((x <= 0) Or (9 <= x)) Then
    Debug.Print ("x <= 0, 9 <= x")
Else
    Debug.Print ("0 < x < 9")
End If
' 0 < x < 9
If (0 < x < 9) Then
    Debug.Print ("0 < x < 9")
Else
    Debug.Print ("x <= 0, 9 <= x")
End If
' 0 < x < 9
End Sub
Recommended Posts