EZ Study

Actuarial Biology Chemistry Economics Calculators Confucius Engineer

Python programming Study Notes

When you run a well-defined python program(say, hello.py), you can go to start-->run-->cmd (Not going directly to Python GUI or Python command line), then in this "DOS" like environment, you can first change the working directory to the one that contains hello.py: cd c:\directory...
To see whether you get the right directory, run: dir to see the python file hello.py.
After you changed to the right directory, run: python hello.py

If you see some error message like, "python is not a recognized program....", then you need to change your computer system settings:

right click "my computer", -->"Properties" --> "Advanced system settings" --> "Advanced" -->
"Environment Variables" --> "System Variables"
-->edit the variable "Path", at the end of "variable value", add the directory that contains "python.exe", for example: "; C:\Python27"

Like C++ and Java, Python is case sensitive so "a" and "A" are different variables.
Unlike C++ &Java, Python not require a semicolon at the end of each statement.
Just out of habit, You can include semicolons at the end of Python statements.
Comments begin with a '#' and extend to the end of the line.

Get step by step examples at Python.org.

Code Is Checked At Runtime

Python does very little checking at compile time, deferring almost all type, name, etc. checks on each line until that line runs. Suppose the above main() calls repeat() like this:

def main():
    if name == 'Guido':
        print repeeeet(name) + '!!!'
        print repeat(name)

The if-statement contains an obvious error, where the repeat() function is accidentally typed in as repeeeet(). The funny thing in Python ... this code compiles and runs fine so long as the name at runtime is not 'Guido'. Only when a run actually tries to execute the repeeeet() will it notice that there is no such function and raise an error. This just means that when you first run a Python program, some of the first errors you see will be simple typos like this. This is one area where languages with a more verbose type system, like Java, have an advantage ... they can catch such errors at compile time (but of course you have to maintain all that type information ... it's a tradeoff).

For more tutorial, you can check Python study tutorial from google, very useful.
Python Lecture: Day 1 Part 1
Python Lecture: Day 1 Part 2

Day 1 Part 1
Day 1 Part 2
Day 1 Part 3

Day 2 Part 1
Day 2 Part 2
Day 2 Part 3
Day 2 Part 4
Some highly-recommended R Tutorial books
Related links:
Continue to Use SQl statement in R   SAS Interview

Back to Statistics tutorial home   Excel Analytics Tutorial Home