CMU 15-112: Fundamentals of Programming and Computer Science
Class Notes: Getting Started
Learning Goal: set up Python and run basic code in an editor
- Logistics and Preliminaries
- Check 1.1 - 1.5
- Running Python
- Hello World in Python
- Python Comments
- Basic Console Output
- Logistics and Preliminaries
We'll discuss class logistics on the first day. If you missed the first class, you can catch up by:- Browsing the TP Gallery (see sidebar)
- Reviewing the syllabus
- Reviewing the course schedule
- Checks 1.1 - 1.5 (Syllabus)
- Running Python
Note: Your TA's will be happy to help with any of these steps!- Install Python3
- Download and install Python 3.7.x from python.org's download page.
- Install IDE
- An IDE is an editor which you use to write and run code. If you already have a preferred editor, you may use that; otherwise, you should use Pyzo, the main IDE supported by the class.
- Download and install Pyzo from the appropriate link:
- Run Pyzo, then 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.7.x
- 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!
- To test that you've set up the program correctly, run the command print(5/3) in the shell. You should not get 1 (which you would get if you are using Python2) but instead should get 1.666666667.
- We recommend that you set up the Pyzo environment to only use two frames: the editor frame (on top, where it is by default) and the shell frame (which you should drag to the bottom).
- We also recommend that you set up your Pyzo environment to use spaces instead of tabs. Do this by going to File > Indentation > Use spaces.
- Writing and Running Code
- 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!
- 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). Do not select the lower options in the menu- they do not always work as expected.
- 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 - runs the single line of code once
print("Hello World!") - File edited in Pyzo - can be edited and run multiple times
Download helloWorld.py, open it in Pyzo, and run it.
- Command typed into shell - runs the single line of code once
- Python Comments
-
Comments are used to explain code to yourself or other people. They are ignored by the computer.
print("Hello World!") # This is a comment because it follows the # symbol # print "What will this line do?"
-
Comments are used to explain code to yourself or other people. They are ignored by the computer.
- Basic Console Output
- Print statements are used to display values in the shell
print("Carpe") print("diem")
- You can tell multiple print statements to print on the same line
# You can separate multiple values with commas print("Carpe", "diem") # You can also use end="" to connect two print statements print("Carpe ", end="") print("diem")
- Print statements are used to display values in the shell