CMU 15-110: Principles of Computing
Running Code


  1. Install Python, IDLE, and pyzo
  2. Hello World in Python
  3. Python Comments
  4. Syntax, Runtime, and Logical Errors
  5. Basic Console Output
  6. Basic Console Input
  7. Importing Modules

  1. Install Python, IDLE, and pyzo
    • We will use Python 3.6 or later.
    • We will use Pyzo as our text editor / IDE.
    • There are detailed install instructions here (but use most-current versions of Python and Pyzo).
    • If you are having troubles, or just want some help, go to OH and a TA will gladly assist!

  2. 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.

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

  4. 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

  5. Basic Console Output
    print("Carpe") print("diem")

  6. Basic Console Input
    • Input a string
      name = input("Enter your name: ") print("Your name is:", name)

    • A string is not a number!
      n = input("Enter a number: ") print("10 times that number is: ", 10*n)

  7. Importing Modules
    import math print(math.factorial(20))