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).
- Course Policies / Syllabus
- Your well-being and happiness
- Course Resources
- Optional Textbooks (use them!)
- Course Schedule
- Running Python
Note: Your TA's will be happy to help with any of these steps!- Install Python3
Download and install Python 3.5.x from python.org's download page. - Run IDLE (the default Python text editor / IDE)
We require that everyone uses Pyzo (former IEP) for the first 4 weeks of the course. However, since IDLE is available without extra installs in nearly all Python installations (including the cluster machines at CMU), you should also know about and be able to use IDLE. In most cases, you can type IDLE into your Mac or Windows search and it will find and run it. - Run Pyzo (formerly "IEP")
Pyzo 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. Everyone in 15-112 will use Pyzo at least for the first 4 weeks of the semester. To install pyzo:- Download and install Pyzo from the appropriate link:
- Pyzo for Windows
- Pyzo for OS X
Note: drag the icon to your Applications folder. You may also need to control-click to Open the first time only, to approve launching this downloaded app. - Pyzo for Linux
- Launch Pyzo.
- Setup your shell configuration for Python 3
- On the top menu, click Shell->Edit Shell Configurations
- Click the dropdown labeled exe, and choose Python 3.5.
- Under "gui", select "None no gui support" (otherwise you may have problems when we do graphics).
- Press done.
- Restart Pyzo and you're ready to go!
- 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.
- Relaunch Pyzo, 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.
- Download and install Pyzo from the appropriate link:
- Edit your Python file
In Pyzo, 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 Pyzo, from the "Run" menu, select "Run file as script". This loads and runs your code in the Python Shell (the interpreter that runs Python code, instead of editing it).- There are several options on the Run menu. Be sure to use the first one, "Run file as script".
- Some students reported an error where Pyzo could not find files that are imported from the same folder as the main file. In these cases, this was remedied (if not outright fixed) by going into Shell/Edit-Shell-Configurations and setting the startDir field either to "." (just a dot, no quotes) or, in some cases, to the full path where your main file is.
- 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