CMU 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. Basic Console Output
  6. Importing Modules
  7. More Logistics and Preliminaries
  8. Syntax, Runtime, and Logical Errors
  9. Basic Console Input

  1. Logistics and Preliminaries

  2. Running Python
    Note: Your TA's will be happy to help with any of these steps!
    • Install Python 3.8.x from python.org's download page.
    • Install VSCode (see the Getting Started with VSCode notes here)
    • Run VSCode (the default Python text editor / IDE)
    • Edit your Python file
    • Run your code (command-B or control-B in VSCode once you followed our setup instructions)

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

    • File edited in VSCode
      Download helloWorld.py, edit it in VSCode, 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...

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

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

  9. 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)