Computer Science 15-110, Spring 2011
Class Notes: Data and Expressions
Data and Expressions
x = 3
print x
print "yikes =", yikes # ERROR! No such variable as yikes
x = 3
print "x =", xx = 4
print "x =", x
print 2+3*4 # prints 14, not 20
print "20/3 =", (20/3)
print " 6/3 =", ( 6/3)
print " 5/3 =", ( 5/3)
print " 2/3 =", ( 2/3)
print " 0/3 =", ( 0/3)
print "-2/3 =", (-2/3)
print "-3/3 =", (-3/3)
print "-4/3 =", (-4/3)
print "20%3 =", (20%3)
print " 6%3 =", ( 6%3)
print " 5%3 =", ( 5%3)
print " 2%3 =", ( 2%3)
print " 0%3 =", ( 0%3)
print "-2%3 =", (-2%3)
print "-3%3 =", (-3%3)
print "-4%3 =", (-4%3)
print " 3%0 =", ( 3%0) # error!
print "2**3 =", (2**3)
print "2**4 =", (2**4)
print "2**10 =", (2**10)
print "2**20 =", (2**20)
print "2**1/2 =", (2**1/2) # huh?!?
print "2**1/2 =", (2**(1/2)) # huh?!?
print "2**0.5 =", (2**0.5)
x = 5
print x
x += 2
print x
x *= 2
print x
x %= 9
print x
x /= 2
print x
x -= 5
print x
x **= 3
print x
print abs(4)
print abs(-4)
print min(3,2)
print min(2,3)
print min(3,2,8,4,5)
print max(3,2)
print max(2,3)
print max(3,2,8,4,5)
print True
print False
b = True
print b
print (5 < 3)
print (3 < 5)
b = (5 < 3)
print b
print (5 == 4)
print (5 == 5)
print (5 != 4)
print (5 != 5)
print (5 < 4)
print (5 > 4)
print (5 < 5)
print (5 <= 5)
print (5 >= 5)
print (5 > 5)
print "AND"
print (True and True )
print (True and False)
print (False and True )
print (False and False)
print ("--------------")
print "OR"
print (True or True )
print (True or False)
print (False or True )
print (False or False)
print ("--------------")
print "NOT"
print (not True)
print (not False)
x = 0
y = 0
print ((y != 0) and ((x/y) != 0)) # Works!
print (((x/y) != 0) and (y != 0)) # Crashes!
Once again, using the "or" operator
x = 0
y = 0
print ((y == 0) or ((x/y) == 0)) # Works!
print (((x/y) == 0) or (y == 0)) # Crashes!
x = 3
print 2*x
x = 3.1
print 2*x
x = 3.0
print 2*x
print 10 / 3
print 10.0 / 3
print 10 / 3.0
x = 10
print x / 3
print 1.0 * x / 3
print float(x) / 3
d1 = (29.0 / 7.0) * 7.0
d2 = 29.0
print (d1 == d2) # False!
print (d2 - d1) # -3.5527136788e-15 (tiny!)
d1 = (29.0 / 7.0) * 7.0
d2 = 29.
print (d1 == d2) # still false, of course
epsilon = 0.000001
print (abs(d2 - d1) < epsilon) # True!
print abs(4.3)
print abs(-4.3)
print "-----------------------"
print min(3.5, 2)
print min(2.0, 3.5)
print min(3.5, 2, 8, 4, 5)
print "-----------------------"
print max(3.5, 2)
print max(2.0, 3)
print max(3.5, 2, 8, 4, 5)
print "-----------------------"
print type(min(3.5, 2))
print type(min(3, 2.0))
print round(7.6543)
print round(7.6543, 0)
print round(7.6543, 1)
print round(7.6543, 2)
print round(7.6543, 3)
print "----------------"
print round(-7.6543)
print round(-7.6543, 0)
print round(-7.6543, 1)
print round(-7.6543, 2)
print round(-7.6543, 3)
print "----------------"
print round(2.5)
print round(2.49999)
print round(-2.5)
print round(-2.4999)
print "abcd"
print len("abcd")
print len("")
print "abcdef"
print "abcdef"[0]
print "abcdef"[1]
print "abcdef"[2]
print "---------------"
print "abcdef"[-1]
print "abcdef"[-2]
print "---------------"
print "abcdef"[0:3]
print "abcdef"[1:3]
print "abcdef"[2:3]
print "abcdef"[3:3]
print "abcdef"[3:]
print "abcdef"[:3]
print "---------------"
print "abcdef"[len("abcdef")-1]
print "abcdef"[len("abcdef")]
print "abcdef"[22]
print "abc" + "def"
print "abc" * 3
print "abc" + 3 # error!
print "abcdef".find("c")
print "abcabc".find("c")
print "abcabc".find("c", 3) # start at index 3
print "abcabc".find("d")
print ("f" in "abcdef")
print ("g" in "abcdef")
print ("cde" in "abcdef")
print ("CDE" in "abcdef")
print "Newline (in brackets): [\n]"
s = "This is a string\nwith a newline in it!"
print s
s = "AB\nCDE"
print s
print len(s)
# surprised?
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem