Python 3home |
Introduction to Python
davidbpython.com
A 'workspace' in VS Code is usually the same as a'project'.
Open a Folder, which will correspond to a new workspace.
Add a new file.
print('hello, world!')
print()
Take care when reproducing the above script - every character must be in its place. (The print() at end is to clarify the Terminal output.)
Next, we'll execute the script.
scripts vs. programs
VS Code may be able to run your script, or some configuration may be required.
Attempt to run your script.
By default, VS Code passes your code to the Python Interpreter and executes it at the command line.
On my Mac, I see this output:
-> /Users/david/miniconda3/bin/python /Users/david/test_project/test.py -> (base) DavidBs-MacBook-Pro:test_project david% /Users/david/miniconda3/bin/python /Users/david/test_project/test.py -> hello, world! -> (base) DavidBs-MacBook-Pro:test_project david%
'Without error' means Python did everything you asked.
On my Mac, I see this output:
-> /Users/david/miniconda3/bin/python /Users/david/test_project/test.py -> (base) DavidBs-MacBook-Pro:test_project david% /Users/david/miniconda3/bin/python /Users/david/test_project/test.py -> hello, world! -> (base) DavidBs-MacBook-Pro:test_project david%
An 'exception' is raised when Python cannot, or will not, do everything you asked in you program, or doesn't understand what the code means (e.g., because of a SyntaxError).
To demonstrate an exception, I removed one character from my code. Here is the result:
-> /Users/david/miniconda3/bin/python /Users/david/test_project/test.py
-> (base) DavidBs-MacBook-Pro:test_project david% /Users/david/miniconda3/bin/python /Users/david/test_project/test.py
-> File "/Users/david/test_project/test.py", line 2
-> print('hello, world!)
^
-> SyntaxError: unterminated string literal (detected at line 2)
(Again, the arrows above are just indicating where each line of output starts - they will not appear in your output.) How should we read our exception?
Throughout this course I will repeatedly call upon you to identify the exception type, pinpoint the error to the line, and seek to understand the error in terms of where Python says it occurred.
Some element of the code is misplaced or missing.
print('hello, world!)
print()
File "/Users/david/test_project/test.py", line 2
print('hello, world!)
^
SyntaxError: unterminated string literal (detected at line 2)
How do we respond to a SyntaxError? First by understanding that there's something missing or out of place in the syntax (the proper placement of language elements -- brackets, braces, parentheses, quotes, etc.) We look at the syntax on the line, and compare it to similar examples in other code that we've seen. Careful comparison between our code and working code will usually show us what's missing or misplaced. In the example above, the first print() statement is missing a quotation mark. It might be hard to see at first, but eventually you will develop "eyes" for this kind of error. pythonreference.com
Use hash marks to comment individual lines; blank lines are ignored.
1 # this program adds numbers
2 var1 = 5
3 var2 = 2
4
5 # add these numbers together
6 var3 = var1 + var2
7
8 # these lines are 'commented out'
9 # var3 = var3 * 2
10 # var3 = var3 / 20
11
12 print(var3)
The exercises should be opened as a single folder in VS Code.
python_data ├── 01 ├── 02 │ ├── 2.1.py │ ├── 2.2.py │ ├── ..etc │ ├── 2.6_lab.py │ ├── 2.7_lab.py │ ├── ..etc │ └── solutions/
We will use some exercises for demos in class; you will use them to practice your skills, and prepare for tests.
├── 02 │ ├── 2.1.py <-- 'journey' exercise │ ├── 2.2.py │ ├── ..etc │ ├── 2.6_lab.py <-- 'lab' (practice) exercise │ ├── 2.7_lab.py │ ├── ..etc │ └── solutions/
The exercises come in two forms: