Operators

Arithmetic


In [22]:
a = 11; b = 3; c = 4; d = 5.0; e=-3
print "a + b = ", a+b    # addition
print "a - d = ", a-d    # subtraction
print "a * e = ", a*e    # multiplication
print "a / b = ", a/b    # division of integers
print "a / d = ", a/d    # division of float
print 'c ** b = ', c**b  # exponentiation
print 'a % c = ', a%c    # remainder
print 'a // b = ', a//b  # floor division
print 'a // e = ', a//e  # floor division


a + b =  14
a / b =  3
a / d =  2.2
c ** b =  64
a % c =  3
a // b =  3
a // e =  -4

Relational


In [24]:
a = 11; b = 3; c = 4; d = 5.0; e=-3
print 'is a == b? ', a == b     # equality
print 'is a != b? ', a != b     # not equal
print 'is a <> b? ', a <> b     # not equal
print 'is a > b? ', a > b       # greater than
print 'is a >= b? ', a >= b     # greater then or equal to
print 'is a < b? ', a < b       # less than
print 'is a <= b? ', a <= b     # less then or equal to


is a == b?  False
is a != b?  True
is a <> b?  True
is a > b?  True
is a >= b?  True
is a < b?  False
is a <= b?  False

Assignment


In [49]:
a = 11; b = 3; c = 4; d = 5.0; e=-3
a+=b
print "a += b = ", a      # same as a = a + b
a-=d
print "a -= d = ", a      # same as a = a - d
a*=e
print "a *= e = ", a      # same as a = a * e
a/=b
print "a /= b = ", a      # same as a = a / b (dividing integers)
a/=d
print "a /= d = ", a      # same as a = a / d
c**=b
print 'c **= b = ', c     # c = c ** b
a%=c
print 'a %= c = ', a      # a = a % c
a//=b
print 'a //= b = ', a     # a = a // b
a//=e
print 'a //= e = ', a     # a = a // e


a += b =  14
a -= d =  9.0
a *= e =  -27.0
a /= b =  -9.0
a /= d =  -1.8
c **= b =  64
a %= c =  62.2
a //= b =  20.0
a //= e =  -7.0

Logical


In [6]:
# and
# or 
# not
c = True
b = False

print b and c
print b or c
print not b


False
True
True

Bitwise


In [16]:
#& -- logical and
# | -- logical or
# ^ -- XOR
# ~ -- One's Complement
# >> -- left shift
# << -- right shift


a = 0b00011100
b = 0b00101101
print 'a =', a, '(', bin(a), ')'
print 'b =', b, '(', bin(b), ')'
print 'a & b =', '(', a & b, bin(a & b), ')'
print 'a | b =', '(', a | b, bin(a | b), ')'
print 'a ^ b =', '(', a ^ b, bin(a ^ b), ')'
print '~a =', ~a, '(', bin(~a), ')'
print 'a << 2 =', a << 2, '(', bin(a << 2), ')'
print 'a >> 2 =', a >> 2, '(', bin(a >> 2), ')'


a = 28 ( 0b11100 )
b = 45 ( 0b101101 )
a & b = ( 12 0b1100 )
a | b = ( 61 0b111101 )
a ^ b = ( 49 0b110001 )
~a = -29 ( -0b11101 )
a << 2 = 112 ( 0b1110000 )
a >> 2 = 7 ( 0b111 )

Membership


In [1]:
# in 
# not in
a = ['1', 2, 'this is an element', 'x']

print 'x' in a
print 'x' not in a
print 5 in a


True
False
False

Identity


In [3]:
# is
# is not

a = 1
b = a

print b is a
print b is not a


True
False

Extra


In [3]:
divmod(20,8)


Out[3]:
(2, 4)

Variable Types

We'll talk about lists, dictionaries, strings, & tuples in the following notebooks


In [7]:
x = 786 # decimal int
m = 060 # octal int
s = 0x69 # hex int
y = 0122L # long
z = -21.9 # float
f = 9.322e-36j # complex

In [8]:
# print the conjugate of the complex number
f.conjugate()


Out[8]:
-9.322e-36j

In [9]:
# print all the example variables
print x
print m
print s
print y
print z
print f


786
48
105
82
-21.9
9.322e-36j

In [6]:
number = 600 
print 'numeber ', number
print "number is: %d" % (number, )     
str1 = "string 1" 
str2 = "string 2" 
print str1 + " is not the same as " + str2    
print "%s is not the same as %s" % (str1, str2)    
print '%s plus %s equals %s' % (1.7, 11.9, 13.6)


numeber  600
number is: 600
string 1 is not the same as string 2
string 1 is not the same as string 2
1.7 plus 11.9 equals 13.6

In [5]:
# write a decimal number in binary
x=10
print "the binary value of", x, "is ", bin(x)
print "Number of bytes : ", x.bit_length()


the binary value of 10 is  0b1010
Number of bytes :  4

In [15]:
# write a float as the ration of two integers
x=3.5
print "x = ", x
print "x as the ratio of two integers:", x.as_integer_ratio()
print "get the integer value of x:", int(x)


x =  3.5
x as the ratio of two integers: (7, 2)
get the integer value of x: 3

In [22]:
y = 42/6.0
print y


7.0

In [23]:
y.is_integer()


Out[23]:
True

In [15]:
# some more formatting
x=3.1415926
print '{:.2f}'.format(x)


3.14