# tuples
tuple1 = (2, 4.0, "some", "string", True)
print tuple1
str1 = 'This is a \ndouble line. He said, "I\'m hungry"'
str2 = "This is a \ndouble line"
str3 = """This is a
double line"""
print str1
print str2
print str3
print "-" * 70
lst = [2, 4.0, "some", "string", True]
lst[1] = 6
print lst
# This does not work : tuple1[1] = 6
tuple2 = (4,6)
# This does not work : tuple2.append(4)
tuple1 = (5,)
print "tuple1 : ", tuple1
print "sum of tuples", tuple1+tuple2
tuple1 = tuple1 + (7,)
print "tuple1 with 7", tuple1
(a, b, c) = (4, 6,7)
a, b = 5, 7
print "a + b = ", a+b
print tuple1[0]
print "*"* 70
# elif
n = 4
if n > 0 :
print "%d is positive." % n
elif n < 0 :
print "%d is negative." % n
elif n == 0 :
print "the number is zero."
else :
print "I don't like your number."
# Nested ifs and elifs
(size, wt) = (100, 7)
if size > 60 :
print "Too big"
elif size > 40 :
print "Big but I'll let you go if you pay."
if wt > 8 :
print "Too heavy"
else :
print "weight is ok."
else :
print "Perfect bag."
if wt > 8 :
print "Too heavy"
else :
print "weight is ok."
# defining your own functions
def my_exp(x, n) :
if n == 0 :
power = 1
elif n > 0 :
power = 1
for i in range(n) :
power *= x
else :
power = 1.0
for i in range(-n) :
power /= x
return power
print "3 ** -8 =", my_exp(3, -8), 3**-8
#Another definition
import math
def my_exp2(x, n) :
if n == 0 :
power = 1
elif n != 0 and x > 0:
power = math.exp(n * math.log(x))
elif x == 0 :
power = 0
elif x < 0 and n > 0 :
print "Cannot find the power."
power = None
return power
print my_exp2(-5, .5)