(Control Structures 04) 불리언 로직
SoloLearn Python 번역
Boolean logicis used to make more complicated conditions forifstatements that rely on more than one condition.불리언 로직은 둘 이상의 조건에 의존하는if문에 대해 보다 복잡한 조건을 생성하는 데 사용된다.
- Python’s Boolean operators are
and,or, andnot.- Python의 불리언 연산자는
and,or,not이다.
- Python의 불리언 연산자는
- The
andoperator takes two arguments, and evaluates asTrueif, and only if, both of its arguments areTrue.and연산자는 두 개의 인수를 사용한다.- 두 인수 모두
True인 경우에만True로 평가한다.
- Otherwise, it evaluates to
False.- 그렇지 않으면
False로 평가한다.
- 그렇지 않으면
>>> 1 == 1 and 2 == 2
# True
>>> 1 == 1 and 2 == 3
# False
>>> 1 != 1 and 2 == 2
# False
>>> 2 < 1 and 3 > 6
# False
Python uses words for its Boolean operators, whereas most other languages use symbols such as \&\&, || and !.
Python은 불리언 연산자에 단어를 사용한다.
대부분의 다른 언어는 \&\&, ||, !를 사용한다.
- The
oroperator also takes two arguments.or연산자도 두 가지 인수를 사용한다.
- It evaluates to
Trueif either (or both) of its arguments areTrue, andFalseif both arguments areFalse.- 인수 중 하나(또는 둘 모두)가
True로 평가되면True이다. - 두 인수 모두
False로 평가되면False이다.
- 인수 중 하나(또는 둘 모두)가
>>> 1 == 1 or 2 == 2
# True
>>> 1 == 1 or 2 == 3
# True
>>> 1 != 1 or 2 == 2
# True
>>> 2 < 1 or 3 > 6
# False
- Unlike other operators we’ve seen so far,
notonly takes one argument, and inverts it.- 지금까지 본 연산자들과는 달리,
not은 하나의 인수를 사용하고, 뒤집는다.
- 지금까지 본 연산자들과는 달리,
- The result of
not TrueisFalse, andnot Falsegoes toTrue.not True는 결과가False이고,not False는 결과가True이다.
>>> not 1 == 1
# False
>>> not 1 > 7
# True
You can chain multiple conditional statements in an
ifstatement using the Boolean operators.불리언 연산자를 사용해서
if문에 여러 조건문을 연결할 수 있다.
QUIZ
- What is the result of this code?
- 이 코드의 결과는 무엇인가?
if (1 == 1) and (2 + 2 > 3):
print("true")
else:
print("false")
true
- What is the result of this code?
- 이 코드의 결과는 무엇인가?
if not True:
print("1")
elif not (1 + 1 == 3):
print("2")
else:
print("3")
2