CMU 15-112: Fundamentals of Programming and Computer Science
Class Notes: Getting Started
- Part 1 (Tue Lecture)
- Part 2 (Thu Lecture)
Part 1 (Tue Lecture)
- 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.
- Run IDLE (the default Python text editor / IDE)
- Edit your Python file
- Run your code (F5 in IDLE by default)
- 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.
- 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
Part 2 (Thu Lecture)
- More Logistics and Preliminaries
- Programming vs Computer Science
- Course Objectives / TP Gallery (see sidebar)
- 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!)
- 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