Python 3

home

Introduction to Python

davidbpython.com




Executing Programs and Using the Lab Exercises

creating a new script (.py file) in PyCharm

A folder in PyCharm is known as a 'project'.


Open a Folder, which will correspond to a new workspace.


Add a new file.

Create a 'hello, world!' script.
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.





executing a script

PyCharm may be able to run your script, or some configuration may be required.


Attempt to run your script.





when programs run without error

'Without error' means Python did everything you asked.


On my Mac, I see this output:

hello, world!

Process finished with exit code 0

When you see the terminal prompt repeated, it means that the script has completed executing





when exceptions occur

An 'exception' is when Python cannot, or will not, do everything you asked in you program.


To demonstrate an exception, I removed one character from my code. Here is the result:

   File "/Users/david/test_project/test.py", line 2
     print('hello, world!)
          ^
SyntaxError: unterminated string literal (detected at line 2)

How should we read our exception?


Throughout this course I will repeatedly stress that you must identify the exception type, pinpoint the error to the line, and seek to understand the error in terms of the exception type, and where Python says it occurred.





the SyntaxError exception

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.





writing code: comments and blank lines

Use hash marks to comment individual lines; blank lines are ignored.


# this program adds numbers
var1 = 5
var2 = 2

var3 = var1 + var2        # add these numbers together

# these lines modify the value further
# var3 = var3 * 2
# var3 = var3 / 20

print(var3)





Using the Lab Exercises

We will use some exercises for demos in class; you will use them to practice you skills, and prepare for tests.


python_data/
├── session_00_test_project/
├── session_01_objects_types/
├──── inclass_exercises/
│       ├── inclass_1.1.py
│       ├── inclass_1.2.py
│       ├── ..etc..
│       ├── inclass_1.6_lab.py
│       ├── inclass_1.7_lab.py
│       ├── ..etc..
├──── notebooks_inclass_warmup/
├── session_02_funcs_condits_methods/
├──── inclass_exercises/
│       ├── inclass_2.1.py
│       ├── inclass_2.2.py
│       ├── ..etc..
├── session_03_strings_lists_files/
├── session_04_containers_lists_sets/
├── ..etc..
└── session_10_classes/

The exercises come in two forms:





[pr]