In [2]:
from operator import *
a = -1
b = 5
print('not_(a)', not_(a))
print('truth(a)', truth(a))
print('is_(a, b)', is_(a,b))
print('is_not(a, b)', is_not(a,b))
In [3]:
from operator import *
a = 1
b = 5.0
print('a =', a)
print('b =', b)
for func in (lt, le, eq, ne, ge, gt):
print('{}(a, b): {}'.format(func.__name__, func(a, b)))
In [4]:
from operator import *
a = -1
b = 5.0
c = 2
d = 6
print('a =', a)
print('b =', b)
print('c =', c)
print('d =', d)
print('\nPositive/Negative:')
print('abs(a):', abs(a))
print('neg(a):', neg(a))
print('neg(b):', neg(b))
print('pos(a):', pos(a))
print('pos(b):', pos(b))
print('\nArithmetic:')
print('add(a, b) :', add(a, b))
print('floordiv(a, b):', floordiv(a, b))
print('floordiv(d, c):', floordiv(d, c))
print('mod(a, b) :', mod(a, b))
print('mul(a, b) :', mul(a, b))
print('pow(c, d) :', pow(c, d))
print('sub(b, a) :', sub(b, a))
print('truediv(a, b) :', truediv(a, b))
print('truediv(d, c) :', truediv(d, c))
print('\nBitwise:')
print('and_(c, d) :', and_(c, d))
print('invert(c) :', invert(c))
print('lshift(c, d):', lshift(c, d))
print('or_(c, d) :', or_(c, d))
print('rshift(d, c):', rshift(d, c))
print('xor(c, d) :', xor(c, d))
In [5]:
from operator import *
a = [1, 2, 3]
b = ['a', 'b', 'c']
print('a =', a)
print('b =', b)
print('\nConstructive:')
print(' concat(a, b):', concat(a, b))
print('\nSearching:')
print(' contains(a, 1) :', contains(a, 1))
print(' contains(b, "d"):', contains(b, "d"))
print(' countOf(a, 1) :', countOf(a, 1))
print(' countOf(b, "d") :', countOf(b, "d"))
print(' indexOf(a, 5) :', indexOf(a, 1))
print('\nAccess Items:')
print(' getitem(b, 1) :',
getitem(b, 1))
print(' getitem(b, slice(1, 3)) :',
getitem(b, slice(1, 3)))
print(' setitem(b, 1, "d") :', end=' ')
setitem(b, 1, "d")
print(b)
print(' setitem(a, slice(1, 3), [4, 5]):', end=' ')
setitem(a, slice(1, 3), [4, 5])
print(a)
print('\nDestructive:')
print(' delitem(b, 1) :', end=' ')
delitem(b, 1)
print(b)
print(' delitem(a, slice(1, 3)):', end=' ')
delitem(a, slice(1, 3))
print(a)
In [6]:
from operator import *
class MyObj:
"""Example for operator overloading"""
def __init__(self, val):
super(MyObj, self).__init__()
self.val = val
def __str__(self):
return 'MyObj({})'.format(self.val)
def __lt__(self, other):
"""compare for less-than"""
print('Testing {} < {}'.format(self, other))
return self.val < other.val
def __add__(self, other):
"""add values"""
print('Adding {} + {}'.format(self, other))
return MyObj(self.val + other.val)
a = MyObj(1)
b = MyObj(2)
print('Comparison:')
print(a<b)
print('\nArithmetic:')
print(a+b)