CMU 15-112: Fundamentals of Programming and Computer Science
Class Notes: Getting Started
- Logistics and Preliminaries
- Running Python
- Hello World in Python
- Python Comments
- Syntax, Runtime, and Logical Errors
- Basic Console Output
- Basic Console Input
- Importing Modules
- Logistics and Preliminaries
- 15-112 vs 15-110 (Are you in the right course?) (Really?)
- Programming vs Computer Science
- Course Objectives (eg, student project gallery)
- Waitlist policy
- Course web site (http://www.cs.cmu.edu/~112) and backup site.
- Course Policies / Syllabus
- Your well-being and happiness
- Course Resources
- Optional Textbooks (use them!)
- Course Schedule
- Running Python
Note: Your CA's will be happy to help with any of these steps!- Install Python3
To check if Python3 is properly installed, run a terminal or command shell and from there run this command:
python3 --version
If you do not see "Python 3.4.x" (for some x), then you should download and install Python 3.4.x from python.org's download page. After installing, re-rerun the "python3 --version" command to confirm your install and paths are set up correctly. If they are not, then go to CA Office Hours asap to get help! - Run IDLE
IDLE is a text editor just for code, and it is available in nearly all Python installations. In many cases, you will have an icon you can click to run IDLE. You probably can also run IDLE by double-clicking on any Python file ending in ".py". Otherwise, you can run IDLE from the command line with:
python3 -m idlelib.idle - Or... Run another code editor
Everyone is expected to be able to use IDLE on the cluster computers (where you have recitation, and where we could take computer-based quizzes on those same machines). Beyond that, you do not have to use IDLE to study or do your homework. This is especially important on Macs, where IDLE has in the past been unstable at times (though you still need to be able to use IDLE even on Macs). In general, if you experience such problems, we then recommend Sublime, but you can use Sublime, or Komodo Edit, or PyCharm, or Wing, or many others. Each is set up a bit differently, so get your CA to help you if needed. Also, note that we disrecommend Eclipse for 15-112 (it's great for experts, but too complicated for novices). Note: if you do use Sublime, we recommend that you download and follow the steps in python-with-ui-options.py, which also uses sublimerepl.py to help set it up as an IDE and not just a standalone editor. - Edit your Python file
In IDLE (or whatever editor you are using), you need to open a Python file or create a new Python file, which then places you in the editing window. That is where you write your code. Be sure to save your code in a file ending in .py (like: foo.py) before running it! - Run your code
Each IDE has its own way to run code. In IDLE, it is F5 ("Run Module"). In Sublime, if you followed our instructions above, it is command-b on Macs and control-b on Windows. Either way, this loads and runs your code in the Python Shell (the interpreter that runs Python code, instead of editing it).
- Install Python3
- 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.
- Command typed into shell
- Python Comments
print("Hello World!") # This is a comment # print "What will this line do?"
- 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 Output
- Basic print function
print("Carpe") print("diem")
- Print on same line
print("Carpe ", end="") print("diem")
- Print multiple items
print("Carpe", "diem") # Another Example: print() # blank line # Compute the hypotenuse of a right triangle a = 3 b = 4 c = ((a**2) + (b**2))**0.5 print("side a =", a) print("side b =", b) print("hypotenuse c =", c)
- Basic print function
- 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
- 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...
- What does a module export?
# list all the functions in the math module # (ignore items in __double_underscores__) import math print(dir(math)) # even better, read the online docs! import webbrowser input("Hit enter to see the online docs for the math module.") webbrowser.open("https://docs.python.org/3/library/math.html")
- Call without importing