lec_1.txt Bio 133, REBMI Art Lee 9/20/2011 [ Note: This was a short tutorial that I prepared on Python for the Bio 133 students at the Claremont Colleges in the Fall of 2011. If you find it useful, feel free to read it. ] P Y T H O N "Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built-in data structures, combined with dynamic typing and dynamic binding, make it very attractive for rapid application development, as well as for use as a scripting or glue language to connect existing components together. Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed. - from the Python Official Website, http://www.python.org - ===================================== Installing Python on your machine: ===================================== I assume that you have access to an installation of Python, which would be the case if you are a Bio 133 student. - Notes: * This tutorial is written for Mac OS X (or Unix) operating system. However, you should be able to use it if you are using Windows. You will just have to replace unix specific commands with equivalent Windows commands. OS-dependence will be insignificant. * If you are using a Mac OS X machine, you would have a version of Python pre-installed on your machine. Go to a Terminal window and try 'python'. * Although Python was pre-installed on my Mac, I installed Enthought Python on mine which provides a more convenient programming environment. I suggest that you install it on your machine as well and do the following if you want to do it: - Go to http://www.enthought.com/ - Try Downloads - Try Academic (at the bottom of the page) - From there it is easy * You will see many program examples in my tutorial. I expect you to try them out using a python system! The code included in the tutorial has been tested using ipython from Enthought on my Mac OS X 10.5.8. It should work on your python system as well. =============================== Learning the language Python =============================== There is an excellent book available free online that will teach you the basics of Python and some important concepts in computer science at the same time. It is: Think Python: How to Think Like a Computer Scientist Allen B. Downey [Downey] http://www.greenteapress.com/thinkpython/thinkpython.html Think Python is a new book based on the following original book. You may find the original book useful as well. I make some references to some parts of the original one in my tutorial. How to Think Like a Computer Scientist: Learning with Python 2nd Edition Jeffrey Elkner, Allen Downey, and Chris Meyers [DEM] http://openbookproject.net/thinkcs/python/english2e/ I also found a tutorial online on Python. It may be a good supplement: http://docs.python.org/tutorial/ You can find other interesting information (e.g., NumPy, SciPy, etc.) on scientific computing here: http://fperez.org/py4science/starter_kit.html ====================================================== Several different ways to run a Python interpreter: ====================================================== 1. Interactive mode whether you are using python or ipython You first start a python session using a terminal window. Then, read interactivemode.txt for detail. After you read interactivemode.txt, come back to this file and continue. When I send you to another file, you go finish that file and come back to this page to continue the rest of the tutorial. That is how it will work and I will NOT remind you again when I say see xxx again and again below for some xxx. 2. Script mode You would prepare a file containing a Python program and execute/run the program in the script file. . see scriptmode.txt 3. Whether you are using an interactive mode or a script mode, you can import other modules into your python program. . See the "Modules" section later (lec_2.txt) for detail. ======================================================= Basic data type values that programs manipulate ======================================================= Note: I am not adding very much explanation on these simple concepts since I am assuming that you would already be familiar with them. If not, the book by Downey [Downey] is a good source to find more information about them. In fact, I would strongly recommend that you keep a copy of [Downey] next to you as you read my tutorial. For those students who will come to my lectures, I will describe them in more detail. . Integers: 34, 2, -300, ... . Real numbers (floating point numbers): 3.4, 2.0, -300.0, 3.14, 4.98090909333, ... . Strings: "This is a string of letters", "Hello world!", ... Note: Strings are defined by enclosing a string of text in a pair of double quotes. Python as in other languages recognizes them as a single entity (value) that can be manipulated much like a number (integer or a floating point number) can be of course using some operators that make sense with strings. . Booleans (logical values): True, False. ============================ Comments ============================ # Any line starting with a pound sign (#) is considered a comment # and will be ignored by the Python interpreter. I will often add # my explanation as a comment in a program file as you will see in # types.py next. ==================================================================== Basic types of values we use in Python (or programming in general) ==================================================================== . see types.py . Types such as int, float, boolean are called primitive data types. Later we will see some compound data types. Actually we already saw one: string. We will see others such as lists and tuples and strings in more detail later. =============================================== Use of the 'print' command in a script file =============================================== . see print.py ============================== Variables ============================== . Storing a value with a name so we can refer to the value repeatedly by the name. . Updating the value with an assignment operator (=) : Once a value is given a name, we can even update the value of the variable by assigning a new value as many times as we want if the logic of the program that we write demands it. Let us see some examples next. . see variables.py ============================================== Operating on values: operators and operands ============================================== . Combining simple data to form more complex ones . see operators.py ========================================= Order of precedence among operators ========================================= . Rules on operator precedence . Same as what you would expect for common operators 4 + 3 * 2 evaluates to 10, not 14. . If you are not sure, use parentheses to indicate your intention. . Operations on strings - We have already seen some of them in operators.py ========================== Input from keyboard ========================== . There are two built-in functions for getting keyboard input, and they are: - raw_input - input . see input.py ========================== Functions ========================== . see functions.py ========================================== Conditionals: doing it conditionally ========================================== . see conditionals.py ========================================== Loops: doing it multiple times ========================================== . see loops.py ============================================= Compound data types: strings, lists, tuples ============================================= . next lecture