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 Python
If Python is not yet installed (as it already is, say, on most Mac and Linux machines), download and install Python 2.7.x from python.org's download page - 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. Otherwise, you can run IDLE from the command line with:
python -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 is known to be unstable (though you still need to be able to use IDLE even on Macs). In general, we 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 follow the steps in this file 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 Python
- 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 Statement
print "Carpe" print "diem"
- Print with Comma
print "Carpe", print "diem"
- Print Multiple Items
print "Carpe", "diem" # Another Example: # 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 Statement
- Basic Console Input
- String with raw_input()
name = raw_input("Enter your name: ") print "Your name is:", name
- Number with raw_input() (error!)
x = raw_input("Enter a number: ") print "One half of", x, "=", x/2 # Error!
- Number with raw_input() and int()
x = int(raw_input("Enter a number: ")) print "One half of", x, "=", x/2
- Strings and Numbers with input()
x = input("Try this with a string and then a number: ") # input() is not recommended! print 2*x
- String with raw_input()
- A more robust example
def inputInteger(prompt): # We won't use while or try/except until later, # but they are handy here to keep asking for input # until we get the kind of input we are looking for while True: userInput = raw_input(prompt + " --> ") try: return int(userInput) except: print "That was not an integer. Please try again." # Try entering a non-integer age... age = inputInteger("Enter your age in years") print "In 10 years, you will be", (age+10), "years old! Wow!"
- Importing Modules
- Calling without importing
print math.factorial(20) # we did not first import the math module # Python output: # NameError: name 'math' is not defined
- Calling with importing
import math print math.factorial(20) # much better...
- What does a module export?
- Use dir() and help()
import math dir(math) help(math) help(math.factorial)
- Use online documentation (usually better)
Try here: https://docs.python.org/2/library/math.html
- Use dir() and help()
- Calling without importing