LoginSignup
1
2

More than 3 years have passed since last update.

VBAユーザがPython・Rを使ってみた:論理演算と比較演算

Last updated at Posted at 2021-01-12

はじめに

機械学習の勉強を始めたVBAユーザです。
備忘録としてPython・Rの文法をVBAと比較しながらまとめていきたいと思います。

目次

論理演算と比較演算

論理値

まず、論理値についてです。

論理値

真・偽を表す論理値は、Pythonでは TrueFalse、Rでは TRUEFALSE、VBAでは TrueFalse です。

Python

Python3
print( True )
print( False )

R

R
print( TRUE )
print( FALSE )
print( T )     # TRUE
print( F )     # FALSE

Rでは TRUEFALSETF と省略して書くことができます。

VBA

VBA
Debug.Print True
Debug.Print False

論理値型

各言語での論理値のデータ型を確認しておきます。Pythonではbool型、Rではlogical型、VBAではBoolean型です。

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

比較演算

比較演算子

次は、比較演算子です。数値にも文字列にも使えます。不等号はどの言語でも同じですが、「=」と「≠」を表す演算子が言語によって違っています。

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

等号はイコール2つです。イコール1つは代入です。

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

等号はイコール2つです。イコール1つは代入です。

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

等号はイコール1つです。イコール1つで等号も代入も表します。

2つの不等式のまとめ書き

2つの不等式 $0 < x$ と $x < 9$ を合わせて $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

Pythonでは 0 < x < 9 が有効です。

R

R
x <- 5
print( x > 0 )     # TRUE
print( x < 9 )     # TRUE   
print( 0 < x < 9 ) # エラー
print( 0 < x && x < 9 ) # TRUE

Rでは 0 < x < 9 と書くとエラーになります。

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

VBAでは 0 < x < 9 が有効です。

論理演算

論理演算子

論理演算子(ブール演算子)の否定(NOT)、論理積(AND)、論理和(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にはXOR演算子がないようです。

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

論理演算の確認

論理演算(ブール演算)の結果はどの言語でも当然同じですが、念のため確認します。

Python

Python3
# 否定(NOT)
for P in [True, False]:
    print(P, not P)
# True False
# False True

# 論理積(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
# 否定(NOT)
for (P in c(TRUE, FALSE)) {
  print(c(P, (!P)))
}
# [1]  TRUE FALSE
# [1] FALSE  TRUE

# 論理積(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

# 排他的論理和(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)

' 否定(NOT)
For i = 0 To 1
  Debug.Print P(i), Not P(i)
Next i
' True          False
' False         True

' 論理積(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

' 排他的論理和(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

条件式の中での使用例

If文の条件式の中で実際に論理演算を使ってみた例です。

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

論理値ベクトル

Rでは論理演算子に &| もあり、論理値ベクトルのベクトル演算に使います。
演算子 &&|| が演算結果を TRUE または FALSE で返すのに対して、演算子 &| は演算結果をベクトルで返します。

R
x <- c(TRUE, TRUE, FALSE, FALSE)   # 論理値ベクトル
y <- c(TRUE, FALSE, TRUE, FALSE)   # 論理値ベクトル
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

まとめ

一覧

各言語で使用する演算子等を一覧にまとめます。比較のために、EXCELでの計算も示しました。

論理値

論理値 Python R VBA EXCEL
True TRUE
T
True TRUE
False False
F
False FALSE
データ型 bool logical Boolean

比較演算子

演算子 Python R VBA EXCEL
== == = =
< < < <
> > > >
>= >= >= >=
<= <= <= >=
!= != <> <>

論理演算子

演算子 Python R VBA EXCEL
否定
NOT
not P !P NOT P =NOT(P)
論理積
AND
P and Q P && Q
P & Q
P And Q =AND(P,Q)
論理和
OR
P or Q P || Q
P | Q
P Or Q =OR(P,Q)
排他的論理和
XOR
xor(P, Q) P Xor Q =XOR(P,Q)

論理演算の確認

演算 演算子 結果
否定 NOT NOT T
NOT F
F
T
論理積 AND T AND T
T AND F
F AND T
F AND T
T
F
F
F
論理和 OR T OR T
T OR F
F OR T
F OR T
T
T
T
F
排他的論理和 XOR T NOR T
T XOR F
F XOR T
F XOR T
F
T
T
F

注意)T:真(true)、 F:偽(false) と表記。

プログラム全体

参考までに使ったプログラムの全体を示します。

Python

Python3
# 論理値
print( True )
print( False )

print( type(True) )  # <class 'bool'>
print( type(False) ) # <class 'bool'>

# 比較演算子
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

# 論理演算子(ブール演算子)
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

# 否定(NOT)
for P in [True, False]:
    print(P, not P)
# True False
# False True

# 論理積(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
# 論理値
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"

# 比較演算子
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 ) # エラー
print( 0 < x && x < 9 ) # TRUE

# 論理演算子(ブール演算子)
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

# 否定(NOT)
for (P in c(TRUE, FALSE)) {
  print(c(P, (!P)))
}
# [1]  TRUE FALSE
# [1] FALSE  TRUE

# 論理積(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

# 排他的論理和(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"

# 論理値ベクトル
x <- c(TRUE, TRUE, FALSE, FALSE)   # 論理値ベクトル
y <- c(TRUE, FALSE, TRUE, FALSE)   # 論理値ベクトル
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

' 論理値
Debug.Print True
Debug.Print False

Debug.Print TypeName(True)   ' Boolean
Debug.Print TypeName(False)  ' Boolean

' 比較演算子
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

' 論理演算子(ブール演算子)
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)

' 否定(NOT)
For i = 0 To 1
  Debug.Print P(i), Not P(i)
Next i
' True          False
' False         True

' 論理積(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

' 排他的論理和(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

参考

1
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2