Computer Science 15-110, Fall 2010
Class Notes:  Getting Started with Python


  1. Installation
  2. Hello World
  3. Comments
    1. Errors
      1. Syntax Errors (Compile-Time Errors)
      2. Runtime Errors ("Crash")
      3. Logical Errors (Compiles and Runs, but is Wrong!)
    2. Python Shell vs Editing Window (in IDLE)
      1. Single Line  Paste
      2. Multiline Paste Problem
      3. The Underscore (_) in the Shell
      4. Alt-P and Alt-N in the Shell
    3. Variables and Expressions
    4. Basic 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()
    5. Importing Modules

    Getting Started with Python

    1. Installation
      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 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. The Underscore (_) in the Shell
        print "This works in the shell, but not in the editing window!"
        2 + 3
        4 * _
      4. Alt-P and Alt-N in the Shell
        print "Try pressing Alt-P and Alt-N a few times in the shell..."
    6. Variables and Expressions

      x = 5

      y = 3
      sum = x + y
      print x
      print "+"
      print y
      print "="
      print sum

      Once again, but printing all on one line:

      x = 5
      y = 3
      sum = x + y
      print x,
      print "+",
      print y,
      print "=",
      print sum

      Once again, but more concisely:

      x = 5
      y = 3
      sum = x + y
      print str(x) + " + " + str(y) + " = " + str(sum)

      Yet again, but with multiple arguments to the print statement:

      x = 5
      y = 3
      sum = x + y
      print x, "+", y, "=", sum


    7. 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 " + str(x) + " = " + str(x/2) # Error!


      3. Number with raw_input() and int()

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


      4. Strings and Numbers with input()

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

      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