Computer Science 15-110, Spring 2011
Class Notes: Console I/O


  1. Console Output
    1. Basic Print Statement
    2. Print with Comma
    3. Print Multiple Items
  2. Console Input
    1. String with raw_input()
    2. Number with raw_input() (error!)
    3. Number with raw_input() and int()
    4. Strings and Numbers with input()

Console I/O

  1. Console Output
    1. Basic Print Statement

      print "Carpe"
      print "diem"

    2. Print with Comma

      print "Carpe",
      print "diem"

    3. Print Multiple Items

      print "Carpe", "diem"

      Another Example:

      # Compute the hypotenuse of a right triangle
      a = 3
      b = 4
      c = ((a**2) + (b**2))**0.5
      print "side a =", a
      print "side b =", b
      print "hypotenuse c =", c

  2. Basic Console Input
    1. String with raw_input()

      name = raw_input("Enter your name: ")
      print "Your name is: " + name

    2. Number with raw_input() (error!)

      x = raw_input("Enter a number: ")
      print "One half of ", x, " = ", x/2 # Error!


    3. Number with raw_input() and int()

      x = int(raw_input("Enter a number: "))
      print "One half of ", x, " = ", x/2


    4. Strings and Numbers with input()  (Not recommended!)

      x = input("Try this with a string and then a number: ") # input() is not recommended!
      print 2*x


carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem