CMU 15-110: Principles of Computing
Running Code
- Install Python, IDLE, and pyzo
- Hello World in Python
- Python Comments
- Syntax, Runtime, and Logical Errors
- Basic Console Output
- Basic Console Input
- Importing Modules
- Install Python, IDLE, and pyzo
- We will use Python 3.6 or later.
- We will use Pyzo as our text editor / IDE.
- There are detailed install instructions here (but use most-current versions of Python and Pyzo).
- If you are having troubles, or just want some help, go to OH and a TA will gladly assist!
- 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
print("Carpe") print("diem")
- Basic Console Input
- Input a string
name = input("Enter your name: ") print("Your name is:", name)
- A string is not a number!
n = input("Enter a number: ") print("10 times that number is: ", 10*n)
- Input a string
- Importing Modules
import math print(math.factorial(20))