Scripting Basics: Python: Difference between revisions
m (14 revisions) |
(No difference)
|
Latest revision as of 03:42, 16 January 2014
Work in Progress
This walk through is based on the Dive Into Python free online book. Corresponding chapters should be read prior to each example section. The purpose of this page is not to reinvent or regurgitate the topics covered in each chapter, but to extend the content with examples that fMRI researchers face on a daily basis.
Class Prerequisites
This course assumes a basic familiarity with programming principles such as
- functions/methods/subroutines
- an understanding of control structures such as loops, conditionals, and I/O
- understanding of basic datatypes, e.g. string, array, etc
All of these topics are covered in any 101 programming course available at this or other education institutes. Other sources for beginner programming principles are the O'reilly Head First series of books, the Sam's Teach Yourself series, and of course Google.
If you decide to forgo a formal class, it's highly recommended that one focus on books for either Python (of course!), C/C++, or matlab as these are the languages you are most likely to encounter during your career as a NeuroImaging Scientist Extraordinaire.
General "Good Practice" Tips
Type out all the examples
Yes, it may seem bothersome. But typing out all the examples manually helps in the memory encoding process. If you just glance at the examples, then cut and paste you'll find yourself quickly becoming lost.
Use a programming oriented text editor
Programming oriented text editors have features like syntax highlighting and auto-indenting. These are essential to generating bug free code and making the learning experience as pain free as possible. For a perfunctory discussion of various such editors, please see New_to_the_Lab wiki section.
Read everything in a section, then go back and follow instructions to the letter
Again, sometimes boring but essential to successful completion of the sections. It's very tempting at times to skim through steps or jump ahead. If something isn't working for you, go back over the instructions to make sure you haven't missed an essential step.
For example, in Section 2.1 the reference book provides a simple example program then says "Now run this program and see what happens." This instruction is immediately followed by an itemized list of how to do so for each platform supported by Python. Without completing a section, the eager student might be at a loss on how perform this instruction. This is a common construct used in instructional texts, e.g.
Do X.
- Doing X on foo
- Doing X on bar
Class Format
- Each subsection is based on a chapter or chapters from the Dive Into Python online (free) book. It is intended that these chapters be read before each section.
- Each class is based on a subsection, some subsections may span multiple classes. It is intended that the student work each subsection's problems before each class.
- Each subsection is preceded by a "What you should know?" section that covers the key concepts a student should glean from the Dive Into Python content before working the problems.
- Each subsection is followed by set of "Challenge Problems" based on the material learned.
- Finally, each one hour class is dedicated to addressing any ambiguities students might have or delving deeper into any issues students may be confronted with.
Part I of II: The Basics
Chapters 1, 2, & 3
What you should know by now
- How to start and exit the Python Interactive Shell
- Where to download the example problems
- How to execute a Python script on your respective platform
- Difference between dynamic and static languages
- How to declare a function and pass arguments
- How to create a functions documentation
- How to 'import' a python script
- About python and code indentation
- The if __name__ trick
Challenge Problems
- Write a Python function that takes one variable as an argument, if it is less than 50 print the variable to screen. If it is not, print a message that says "Variable too large!"
- Document the function using python doc string
- Bonus! import your new function into another script and execute it two times, once with a variable < 50 and once with a variable > 50 only if __name__ == "__main__"