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
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
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
In [6]:
# and
# or
# not
c = True
b = False
print b and c
print b or c
print not b
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), ')'
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
In [3]:
# is
# is not
a = 1
b = a
print b is a
print b is not a
In [3]:
divmod(20,8)
Out[3]:
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]:
In [9]:
# print all the example variables
print x
print m
print s
print y
print z
print f
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)
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()
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)
In [22]:
y = 42/6.0
print y
In [23]:
y.is_integer()
Out[23]:
In [15]:
# some more formatting
x=3.1415926
print '{:.2f}'.format(x)