Python‎ > ‎keywords‎ > ‎

not

http://www.adilwaseem.com/

Definition
The boolean operation "not" tests whether the equation is not equal which is also can be tested by !=.

operator
!=

Parameters
Requires testing statements on both ends.

Examples
Here, in this example, the IntegerField validated number entered should be in record and before the Field vale is passed on HttpResponseRedirect in views.py and index.html files.
Steps for creating virtual environment, startproject, startapps, & running server:
  • virtualenv myvirtualenv
  • cd  mysite
  • .\Scripts\activate
  • pip install django
  • python .\Scripts\django-admin.py startproject mysite
  • cd mysite
  • python manage.py startapp myapphttp
  • python manage.py runserver 



setting.py

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles','myapphttp',
)

urls.py

from django.conf.urls import include, url

from myapphttp.views import response

urlpatterns = [
      url(r'^', response),
]

myapphttp/forms.py

from django import forms



def Validate(value):
    pin=[1101,2945,3635,5550,3334,5553,2822,4624,3221,6453,2223,4456,5462]
    if value not in pin:
        raise ValidationError('%s : The pin number is not in our record.'% value)

class IntegerField(forms.Form):
    number=forms.IntegerField(label='Number' ,validators=[Validate])

myapphttp/views.py

from django.shortcuts import render

from .forms import IntegerField

def response(request):
    if request.method == 'POST':
        form=IntegerField(request.POST)
        if form.is_valid():
            return HttpResponseRedirect('/Thanks-for-submitting-your-request/')
    else:
        form = IntegerField()

    return render(request, 'myapphttp\index.html', {'form':form})


myapphttp/index.html

<!DOCTYPE html>
<html>
<title> </title>
<body>

<form action="/pin-not-found/" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>

</body>
</html>





In this example, Python tested that y value which is 13 is within the range 5 to 55. If statement was not executed on the because else is true.
Testing the string through built in method operator.is_not().
 >>> len("Python Lake") is not len("Python Lake")
False

str(int()) is notint(str())-> True
The returns are :
 >>> type("Python Research Centre".rfind("Centre"))==type(str("Python Research Centre".rfind("Centre")))
False
>>> type("Python Research Centre".rfind("Centre")) is not type(str("Python Research Centre".rfind("Centre")))
True
>>> "Python Research Centre".rfind("Centre") is not str(int("Python Research Centre".rfind("Centre")))
True
>>> "Python Research Centre".rfind("Centre").__eq__str(int("Python Research Centre".rfind("Centre")))
False
>>> "Python Research Centre".rfind("Centre") .__eq__int(str("Python Research Centre".rfind("Centre")))
True
>>>


if...not
if statement find the number in range().
x= range(5,55)
y=13
if y not in x:
   print("y value is not within x range")
else:
   print("y is within range of x")

Output

>>> 
y is within range of x.
>>> 




Comments