파이썬 Django 논리 연산자(Operator) : and
둘 이상의 조건이 참인지 확인한다. 2개의 논리값이 모두 참일 때 참 결과값을 반환한다. 둘 중에 하나의 논리값이 거짓일 경우에는 거짓을 반환한다.
template.html <!DOCTYPE html> <html> <body> {% if greeting == 1 and day == "Friday" %} <h1>Hello Weekend!</h1> {% endif %} <p>In views.py you can see what the variables look like.</p> </body> </html> |
views.py from django.http import HttpResponse from django.template import loader def testing(request): template = loader.get_template('template.html') context = { 'greeting': 1, 'day': 'Friday', } return HttpResponse(template.render(context, request)) |
greeting이 1이고 day가 Friday로 모두 참값이므로 and 연산자 반환값은 참이므로 h1태그에 'Hello Weekend' 문구를 화면에 아래 그림과 같이 출력한다.
파이썬 Django 논리 연산자(Operator) : or
2개의 논리값 중에 하나라도 참값이면 참값을 반환한다. 2개의 논리값이 모두 거짓이면 거짓을 반환한다.
template.html <!DOCTYPE html> <html> <body> {% if greeting == 1 or greeting == 5 %} <h1>Hello Weekend!</h1> {% endif %} <p>In views.py you can see what the variables look like.</p> </body> </html> |
views.py from django.http import HttpResponse from django.template import loader def testing(request): template = loader.get_template('template.html') context = { 'greeting': 1, } return HttpResponse(template.render(context, request)) |
greeting이 1이거나 5이면 참값이 반환이 된다. 현재 greeting 값이 1이기 때문에 or 연산자 반환값은 참이므로 h1태그에 'Hello Weekend' 문구를 화면에 아래 그림과 같이 출력한다.
파이썬 Django 논리 연산자(Operator) : and, or 조합
and와 or 연산자 조합
template.html <!DOCTYPE html> <html> <body> {% if greeting == 1 and day == "Friday" or greeting == 5 %} <h1>Hello Weekend!</h1> {% endif %} <p>In views.py you can see what the variables look like.</p> </body> </html> |
views.py from django.http import HttpResponse from django.template import loader def testing(request): template = loader.get_template('template.html') context = { 'greeting': 5, 'day': 'Friday', } return HttpResponse(template.render(context, request)) |
Django 에 있는 문장의 경우 괄호를 사용할 수 없다. 따라서 및/또는 연산자를 결합할 때 괄호는 에 추가되지만 또는 에 대해서는 추가되지 않는다는 점을 알아야 합니다.
위의 예문을 해석자가 다음과 같이 읽는 것을 의미합니다:
{% if (greeting == 1 and day == "Friday") or greeting == 5 %} |
greetin이 5, day가 'Friday'값이므로 첫번째 and 연산자 문장에서는 거짓값이 반환이 되고, greeting이 5이므로 뒤에 논리식에서는 참값이 반환이 되어 'Hello Weekend'문구가 화면에 아래 그림과 같이 출력이 된다.
파이썬 Django 논리 연산자(Operator) : in
개체에 특정 항목이 있는지 확인한다.
template.html <!DOCTYPE html> <html> <body> {% if 'Banana' in fruits %} <h1>Hello</h1> {% else %} <h1>Goodbye</h1> {% endif %} <p>In views.py you can see what the fruits variable looks like.</p> </body> </html> |
views.py from django.http import HttpResponse from django.template import loader def testing(request): template = loader.get_template('template.html') context = { 'fruits': ['Apple', 'Banana', 'Cherry'], } return HttpResponse(template.render(context, request)) |
fruits 개체에는 Apple, Banana, Cherry 값이 저장되어 있고, if문에서 in 연산자에 fruits 개체에 'Banana'가 포함되어 있는지 확인한다. 포함되어 있으므로 결과값이 참이므로 'Hello'가 아래 그림과 같이 화면에 출력이 된다.
위의 코드를 실행하면 아래 그림과 같다.
파이썬 Django 논리 연산자(Operator) : not in
개체에 특정 항목이 없는지 확인한다.
template.html <!DOCTYPE html> <html> <body> {% if 'Banana' not in fruits %} <h1>Hello</h1> {% else %} <h1>Goodbye</h1> {% endif %} <p>In views.py you can see what the fruits variable looks like.</p> </body> </html> |
views.py from django.http import HttpResponse from django.template import loader def testing(request): template = loader.get_template('template.html') context = { 'fruits': ['Apple', 'Banana', 'Cherry'], } return HttpResponse(template.render(context, request)) |
fruits 개체에는 Apple, Banana, Cherry 값이 저장되어 있고, if문에서 not in 연산자에 fruits 개체에 'Banana'가 포함되어 있는지 확인한다. 포함되어 있지 않으므로 결과값이 거짓이므로 'Goodbye'가 아래 그림과 같이 화면에 출력이 된다.
파이썬 Django 논리 연산자(Operator) : is
두 개체가 동일한지 확인한다.
== 연산자는 두 개체의 값을 확인하지만 is 연산자는 두 개체의 동일성을 확인하기 때문에 이 연산자는 == 연산자와 다르다.
보기에는 x와 y라는 두 개의 객체가 있고 값은 같다:
template.html <!DOCTYPE html> <html> <body> {% if x is y %} <h1>YES</h1> {% else %} <h1>NO</h1> {% endif %} <p>In views.py you can see what the x and y variables look like.</p> </body> </html> |
views.py from django.http import HttpResponse from django.template import loader def testing(request): template = loader.get_template('template.html') context = { 'x': ['Apple', 'Banana', 'Cherry'], 'y': ['Apple', 'Banana', 'Cherry'], } return HttpResponse(template.render(context, request)) |
if x is y 구문에 개체에 값은 같지만 개체 자체는 다르기 때문에 거짓값을 반환하여 'NO'가 화면에 아래 그림과 같이 출력이 된다.
대신 == 연산자를 사용하여 동일한 예를 시도해 보겠다:
{% if x == y %} <h1>YES</h1> {% else %} <h1>NO</h1> {% endif %} |
위의 예제와 달리 ==는 내용의 값이 같기 때문에 참값을 반환하여 'YES'를 아래 그림과 같이 화면에 출력한다.
어떻게 두 개체가 같을 수 있을까? 만약 여러분이 같은 개체를 가리키는 두 개체가 있다면, 그 연산자는 참이라고 평가한다:
템플릿에서 변수를 만들 수 있는 {% with %} 태그를 사용하여 이를 입증한다:
template.html <!DOCTYPE html> <html> <body> {% with var1=x var2=x %} {% if var1 is var2 %} <h1>YES</h1> {% else %} <h1>NO</h1> {% endif %} {% endwith %} <p>The x variable is created in views.py, and both var1 and var2 points to x.</p> </body> </html> |
views.py from django.http import HttpResponse from django.template import loader def testing(request): template = loader.get_template('template.html') context = { 'x': ['Apple', 'Banana', 'Cherry'], 'y': ['Apple', 'Banana', 'Cherry'], } return HttpResponse(template.render(context, request)) |
위의 코드를 실행하면 아래 그림과 같다.
파이썬 Django 논리 연산자(Operator) : is not
두 개체가 동일하지 않은지 확인한다.
template.html <!DOCTYPE html> <html> <body> {% if x is not y %} <h1>YES</h1> {% else %} <h1>NO</h1> {% endif %} <p>In views.py you can see what the x and y variables look like.</p> </body> </html> |
views.py from django.http import HttpResponse from django.template import loader def testing(request): template = loader.get_template('template.html') context = { 'x': ['Apple', 'Banana', 'Cherry'], 'y': ['Apple', 'Banana', 'Cherry'], } return HttpResponse(template.render(context, request)) |
x와 y의 값은 같지만, x와 y는 개체가 다르기 때문에 if is not y는 참값을 반환하므로 'YES'가 아래 그림과 같이 출력이 된다.
이번 글에서는 파이썬 Django 연산자에 두 번째 부분에 대해서 실습하며 살펴보았다.
꼭 손으로 눈으로 머리로 익히며 실습하기를 바란다.
모두 화이팅입니다.!!!
출처 : 이 글의 출처는 w3schools사이트를 참고하였으며 필자가 추가하여 정리한 글입니다.
'파이썬 > 파이썬기본문법' 카테고리의 다른 글
파이썬 Django for loop variables (0) | 2024.03.07 |
---|---|
파이썬 Django for loop (0) | 2024.03.06 |
파이썬 django if tag (0) | 2024.03.04 |
파이썬 Django 템플릿 태그(template tags) (0) | 2024.03.03 |
파이썬 django template variable (0) | 2024.03.02 |
댓글