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. It has some downsides, so we suggest you use other IDE's (see below), but still you should know about and be able to use IDLE, since it is the default Python IDE you get on install, and in particular it is available on all the cluster machines at CMU. 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 - Run IEP ("eep")
IEP is a new Python IDE that is easy to setup and use. It has some nice features that IDLE does not, such as line numbers (which are a big deal, actually), an 80-character line (very handy), and so on. We highly suggest that you use it. To install IEP:- Download and install IEP from here
- Launch IEP (note that the first time you launch on a Mac you may have to use a control-click to Open and say "yes" that you approve launching this downloaded app)
- Close the extra frames (file browser, etc), only use editor frame (on top, where it is by default) and shell frame (which you should drag to the bottom), so it mirrors IDLE's simple setup.
- Select down-triangle from top-left of shell, and select "Edit Shell Configurations", then "add config" and add a Python3 configuration, then reorder it on the configuration tabs to be the first tab, then exit.
- Relaunch IEP, for a simple Python3 test, use this program: print(5/3), then choose Run File as Script or Execute File from the Run menu. You should not get 1 (which you would get if you are using Python2) but instead should get 1.666666667.
- Run Sublime
In addition to IDLE (which you should have installed and available as a fallback, but not use in general), and IEP (which you should use in general), we recommend that you also install Sublime, though we do not require this. IEP is a great tool for novices learning to code. Sublime is not that. It is a popular editor among more advanced students and many software professionals. It is not Python-specific, though, and by default it is just an editor and not an IDE, so there is a more complicated setup procedure to make Sublime act like a Python IDE. To install Sublime:- First, carefully watch this helpful video that Josh Korn created (thanks, Josh): Mac or Windows .
- Download and follow the steps in python-with-ui-options.py, which also uses sublimerepl.py.
- Or... Run another code editor
While we require IDLE (though we do not suggest you use it as your everyday editor), we only strongly suggest you use IEP and perhaps Sublime. That is, you can use whatever editor you prefer, including 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), but even that is your choice. - Edit your Python file
In IEP (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