Computer Science 15-110, Spring 2011
Class Notes:  Getting Started with Python


  1. Installation
  2. Hello World
  3. Comments
  4. Errors
    1. Syntax Errors (Compile-Time Errors)
    2. Runtime Errors ("Crash")
    3. Logical Errors (Compiles and Runs, but is Wrong!)
  5. Python Shell vs Editing Window (in Chopin and IDLE)
    1. Single Line  Paste
    2. Multiline Paste Problem
    3. Shell History
  6. Variables and Expressions
  7. Importing Modules (Libraries)

Getting Started with Python

  1. Installation

    Chopin + Jython:  Coming Soon.
    IDLE + Python 2.7.1:  See http://www.python.org/
     
  2. Hello World

    print "Hello World!"
     
  3. Comments
     
    print "Hello World!" # This is a comment

    # print "What will this line do?"

  4. Errors
     
    1. Syntax Errors (Compile-Time Errors)
      print "Uh oh! # ERROR! missing close-quote
      
      Python output:
      SyntaxError: EOL while scanning string literal
    2. Runtime Errors ("Crash")
      print 1/0 # ERROR! Division by zero!
      Python output:
      ZeroDivisionError: integer division or modulo by zero
    3. Logical Errors (Compiles and Runs, but is Wrong!)
      print "2+2=5" # ERROR! Untrue!!!
      Python output:
      2+2=5
  5. Python Shell vs Editing Window (in Chopin and IDLE)

    1. Single Line Paste
      print "This works just fine
    2. Multiline Paste Problem
      print "But this example demonstrates a problem"
      print "with multiline paste in the Python Shell"
    3. Shell History
      print "Try pressing Up Arrow and Down Arrow a few times in the Chopin shell..."
      print "This also works with Alt-P (previous) and Alt-N (next)."
      print "Note that IDLE only supports Alt-P/Alt-N and not the Up/Down Arrow."

  6. Variables and Expressions

    x = 5

    y = 3
    sum = x + y
    print sum



  7. Importing Modules (Libraries)

    Fails:

    print math.sqrt(5)
    Python output:
    NameError: name 'math' is not defined
    Works:

    import math
    print math.sqrt(5)
    Python output:
    2.2360679775

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