CMU 15-112: Fundamentals of Programming and Computer Science
Class Notes: Getting Started
- Logistics and Preliminaries
- Running Python
- Hello World in Python
- Python Comments
- Basic Console Output
- Importing Modules
- More Logistics and Preliminaries
- Syntax, Runtime, and Logical Errors
- Basic Console Input
- Logistics and Preliminaries
- Course web site (http://www.cs.cmu.edu/~112)
- Course Policies / Syllabus
- Course Schedule
- Waitlist policy
- 15-112 vs 15-110
- 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.
- 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)
- 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.
- Command typed into shell
- Python Comments
print("Hello World!") # This is a comment # print "What will this line do?"
- 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}?')
- Basic print function
- 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...
- Call without importing
- More Logistics and Preliminaries
- Programming vs Computer Science
- Course Objectives / TP Gallery (see sidebar)
- 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
- Syntax Errors (Compile-Time Errors)
- 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)
- Input a string