[Tutor] Doubt in Python

Alan Gauld alan.gauld at yahoo.co.uk
Wed Jan 16 19:39:21 EST 2019


On 16/01/2019 18:26, Maninath sahoo wrote:
> Using of relational operator i Python?
> How to use relational operator in List.
> Please answer me..

Relational operators are more commonly called
comparisons (or predicates). You use them with
a List much as you use any other object.

if list1 == list2...
if list2 != list3...
if list4 is list1...
if listX < listY

Your best bet it just to try them at the
interactive >>> prompt. For example:

>>> x = [1,2,3,4]
>>> y = [2,5,8]
>>> x == y
False
>>> x < y
True
>>> y > x
True
>>> x <= y
True
>>> z = [1,2,3]
>>> x < z
False
>>> z < x
True

and so on...

The other important operator with regard
to lists is 'in'

>>> 3 in x
True
>>> 3 in y
False

You can also see which operators are supported by looking at the dir()
function output:

>>> dir([])
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
'__dir__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__',
'__imul__', '__init__', '__init_subclass__', '__iter__', '__le__',
'__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__',
'__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append',
'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove',
'reverse', 'sort']

Notice __eq__, __ne__, __gt__, __contains__, etc

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list