15-112: Fundamentals of Programming and Computer Science
Class Notes: Getting Started


  1. Logistics and Preliminaries
  2. Running Python
  3. Hello World in Python
  4. Python Comments
  5. Syntax, Runtime, and Logical Errors
  6. Basic Console Output
  7. Basic Console Input
  8. Importing Modules

  1. Logistics and Preliminaries
    • 15-112 vs 15-110 (Are you in the right course?) (Really?)
    • Programming vs Computer Science
    • Course Objectives (eg, student project gallery)
    • Waitlist policy
    • Course web site (http://www.cs.cmu.edu/~112) and backup site.
    • Course Policies / Syllabus
    • Your well-being and happiness
    • Course Resources
    • Optional Textbooks (use them!)
    • Course Schedule

  2. Running Python
    Note: Your CA's will be happy to help with any of these steps!

    • Install Python
      If Python is not yet installed (as it already is, say, on most Mac and Linux machines), download and install Python 2.7.x from python.org's download page

    • Run IDLE
      IDLE is a text editor just for code, and it is available in nearly all Python installations. In many cases, you will have an icon you can click to run IDLE. Otherwise, you can run IDLE from the command line with:
      python -m idlelib.idle

    • Or... Run another code editor
      Everyone is expected to be able to use IDLE on the cluster computers (where you have recitation, and where we could take computer-based quizzes on those same machines). Beyond that, you do not have to use IDLE to study or do your homework. You can use Sublime, or Komodo Edit, or PyCharm, or Wing, or many others. Each is set up a bit differently, so get your CA to help you if needed. Also, note that we disrecommend Eclipse for 15-112 (it's great for experts, but too complicated for novices).
      Note: if you do use Sublime, you may be interested in following the steps in this file to help set it up as an IDE and not just a standalone editor.

    • Edit your Python file
      In IDLE (or whatever editor you are using), you need to open a Python file or create a new Python file, which then places you in the editing window. That is where you write your code. Be sure to save your code in a file ending in .py (like: foo.py) before running it!

    • Run your code
      Each IDE has its own way to run code. In IDLE, it is F5 ("Run Module"). This loads and runs your code in the Python Shell (the interpreter that runs Python code, instead of editing it).

  3. Hello World in Python
    • Command typed into shell
      print "Hello World!"
      

    • Function typed into shell
      def helloWorld():
          print "Hello World!"
      
      helloWorld()
      

    • File edited in IDLE
      Download helloWorld.py, edit it in IDLE, and run it.

  4. Python Comments
    print "Hello World!" # This is a comment
    # print "What will this line do?"
    

  5. Syntax, Runtime, and Logical Errors
    • Syntax Errors (Compile-Time Errors)
      print "Uh oh! # ERROR!  missing close-quote
      
      # Python output:
      #   SyntaxError: EOL while scanning string literal
      

    • Runtime Errors ("Crash")
      print 1/0 # ERROR!  Division by zero!
      
      # Python output:
      #   ZeroDivisionError: integer division or modulo by zero
      

    • Logical Errors (Compiles and Runs, but is Wrong!)
      print "2+2=5" # ERROR!  Untrue!!!
      
      # Python output:
      #   2+2=5
      

  6. Basic Console Output
    • Basic Print Statement
      print "Carpe"
      print "diem"
      

    • Print with Comma
      print "Carpe",
      print "diem"
      

    • 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
      

  7. Basic Console Input
    • String with raw_input()
      name = raw_input("Enter your name: ")
      print "Your name is:", name
      

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

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

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

    • A more robust example
      def inputInteger(prompt):
          # We won't use while or try/except until later,
          # but they are handy here to keep asking for input
          # until we get the kind of input we are looking for
          while True:
              userInput = raw_input(prompt + " --> ")
              try:
                  return int(userInput)
              except:
                  print "That was not an integer. Please try again."
      
      # Try entering a non-integer age...
      age = inputInteger("Enter your age in years")
      print "In 10 years, you will be", (age+10), "years old! Wow!"
      

  8. Importing Modules
    • Calling without importing
      print math.factorial(20)  # we did not first import the math module
      
      # Python output:
      #   NameError: name 'math' is not defined
      

    • Calling with importing
      import math
      print math.factorial(20)  # much better...
      

    • What does a module export?