07 Sep 2017 » python

2017-09-07-python_1

논리(logic)에 대해 알아보자.

진리 용어

  • and
  • or
  • not
  • !=
  • ==
  • >=
  • <=
  • True
  • False

진리 표(truth table)

아래 표를 읽으면서 이해하자.

진리 True ?
not False True
not True False
True or False True
True or True True
False or True True
False or False False
True and False False
True and True True
False and True False
False and False False
not (True or False) False
not (True or True) False
not (False or True) False
not (False or False) True
not (True and False) True
not (True and True) False
not (False and True) True
not (False and False) True
1 != 0 True
1 != 1 False
0 != 1 True
0 != 0 False
1 == 0 False
1 == 1 True
0 == 1 False
0 == 0 True

Boolean을 연습해보자.

In [1]:
True and True
Out[1]:
True
In [2]:
False and True
Out[2]:
False
In [3]:
1 == 1 and 2 == 1
Out[3]:
False
In [4]:
"test" == "test"
Out[4]:
True
In [5]:
1 == 1 or 2 != 1
Out[5]:
True
In [6]:
True and 1 == 1
Out[6]:
True
In [7]:
False and 0 != 0
Out[7]:
False
In [8]:
True or 1 == 1
Out[8]:
True
In [9]:
"test" == "testing"
Out[9]:
False
In [10]:
1 != 0 and 2 == 1
Out[10]:
False
In [11]:
"test" != "testing"
Out[11]:
True
In [12]:
"test" == 1
Out[12]:
False

아래 두 셀은 같은 논리이다. not을 분배법칙으로 풀어쓰면 같은 논리가 된다.

In [13]:
not (True and False)
Out[13]:
True
In [14]:
False or True
Out[14]:
True
In [15]:
not (1 == 1 and 0 != 1)
Out[15]:
False
In [16]:
not (10 == 1 or 1000 == 1000)
Out[16]:
False
In [17]:
not (1 != 10 or 3 == 4)
Out[17]:
False
In [18]:
not ("testing" == "testing" and "Zed" == "Cool Guy")
Out[18]:
True
In [19]:
1 == 1 and not ("testing" == 1 or 1 == 0)
Out[19]:
True
In [20]:
"chunky" == "bacon" and not (3 == 4 or 3 == 3)
Out[20]:
False
In [23]:
3 != 4 and not ("testing" != "test" or "Python" == "Python")
Out[23]:
False

Reference

  • 깐깐하게 배우는 파이썬


Related Posts