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



Part 1 (Tue Lecture)

  1. Logistics and Preliminaries

  2. Running Python
    Note: Your TA's will be happy to help with any of these steps!
    • Install Python 3.7.x from python.org's download page.
    • Run IDLE (the default Python text editor / IDE)
    • Edit your Python file
    • Run your code (F5 in IDLE by default)

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

    • 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. Basic Console Output
    • Basic print function
      print("Carpe") print("diem")

    • Print on same line
      # You can separate multiple values with commas print("Carpe", "diem") # You can also use end="" to stay on the same line print("Carpe ", end="") print("diem")

    • Print using f strings
      x = 42 y = 99 # Place variable names in {squiggly braces} to print their values, like so: print(f'Did you know that {x} + {y} is {x+y}?')

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

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

Part 2 (Thu Lecture)

  1. More Logistics and Preliminaries
    • Programming vs Computer Science
    • Course Objectives / TP Gallery (see sidebar)

  2. More Running Python Ways to make IDLE even better:
    • Run your code with cmd-b or ctrl-b
    • See line numbers (with idlex) (also learn to use pip!)

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

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

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

    • Input a number with int()
      x = int(input("Enter a number: ")) print("One half of", x, "=", x/2)