Computer Science 15-110, Spring 2011
Class Notes:  Data and Expressions


  1. Data Types
    1. Some Data Types
    2. Types Affect Semantics
    3. Type Conversion Functions
      1. str, float, int
      2. ord and chr
  2. int values
    1. Define a variable and use it
    2. Use a variable without defining it
    3. Assigning and re-assigning
    4. Operator Precedence  (just as you'd expect)
    5. Integer division
    6. The Modulus or Remainder operator (%)
    7. The Exponentiation Operator (**)
    8. Assignment Operators
    9. Some int functions (abs, min, max)
  3. boolean values
    1. Boolean literals (True and False)
    2. Equality operators
    3. Relational operators
    4. Logical operators (and, or, not)
    5. Short-circuit evaluation
  4. float values
    1. Use "float" variables
    2. Integer vs Floating-point division
    3. Approximate values of floating-point numbers
    4. "almost equal" comparison of doubles
    5. Some float Math functions
      1. abs, min, max
      2. round
  5. string values
    1. String length
    2. String indexing and slicing
    3. String concatenation
    4. String containment ("find" and "in")
    5. The \n Escape Sequence

Data and Expressions

  1. Data Types
    1. Some Data Types

      print type(123)
      print type(3.14159)
      print type(True)
      print type("Carpe diem")

    2. Types Affect Semantics

      print 3 * 2
      print 3 * "abc"

      print 3 + 2
      print "abc" + "def"
      print 3 + "def" # error!

    3. Type Conversion Functions
      1. str, float, int

        x = 12
        print x
        print x + x
        print type(x)

        s = str(34)
        print s
        print s + s
        print type(s)

        f = float(56)
        print f
        print type(f)

        i1 = int("90")
        print i1
        print type(i1)

        i2 = int(12.3)
        print i2
        print type(i2)

        i3 = int("45.6")
        print i3
        print type(i3)

      2. ord and chr

        print ord("A")
        print ord("a")
        print ord("0")
        print ord(" ")

        print chr(65)
        print chr(97)
        print chr(48)
        print chr(32)

        print ord(chr(65))
        print chr(ord("A"))

  2. int values
     
    1. Define a variable and use it
      x = 3
      print x

    2. Use a variable without defining it
      print "yikes =", yikes # ERROR! No such variable as yikes
    3. Assigning and re-assigning
      x = 3
      print "x =", x
      x = 4
      print "x =", x
    4. Operator Precedence  (just as you'd expect)
      print 2+3*4 # prints 14, not 20
    5. Integer division
      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)
    6. The Modulus or Remainder operator (%)
      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!
    7. The Exponentiation Operator (**)
      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)
    8. Assignment Operators
      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
    9. Some int functions (abs, min, max)
      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)
  3. boolean values
     
    1. Boolean literals ( True , False )
      print True
      print False
      b = True
      print b
      print (5 < 3)
      print (3 < 5)
      b = (5 < 3)
      print b
    2. Equality operators ( == , != )
      print (5 == 4)
      print (5 == 5)
      print (5 != 4)
      print (5 != 5)
    3. Relational operators ( < , <= , >= , > )
      print (5 < 4)
      print (5 > 4)
      print (5 < 5)
      print (5 <= 5)
      print (5 >= 5)
      print (5 > 5)
    4. Logical operators (and, or, not)
      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)
    5. Short-circuit evaluation
      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!
  4. float values
     
    1. Use "float" variables
      x = 3
      print 2*x
      x = 3.1
      print 2*x
      x = 3.0
      print 2*x
    2. Integer vs Floating-point division
      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
    3. Approximate values of floating-point numbers
      d1 = (29.0 / 7.0) * 7.0
      d2 = 29.0
      print (d1 == d2) # False!
      print (d2 - d1) # -3.5527136788e-15 (tiny!)
    4. "almost equal" comparison of doubles
      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!
    5. Some float Math functions

      1. abs., min, max
        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))
      2. round
        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)
  5. string values
    1. String length
      print "abcd"
      print len("abcd")
      print len("")
    2. String indexing and slicing
      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]
    3. String concatenation
      print "abc" + "def"
      print "abc" * 3
      print "abc" + 3 # error!
    4. String containment ("find" and "in")
      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")
    5. The \n Escape Sequence
      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