lec_1.txt Bio 133, REBMI Art Lee 10/10/2008 [ Note: This was a short tutorial that I prepared on Python for the Bio 133 students at the Claremont Colleges in the Fall of 2008. If you find it useful, feel free to read it. ] P Y T H O N "Python is a dynamic object-oriented programming language that can be used for many kinds of software development. It offers strong support for integration with other languages and tools, comes with extensive standard libraries, and can be learned in a few days. Many Python programmers report substantial productivity gains and feel the language encourages the development of higher quality, more maintainable code." - from the Python Official Website, http://www.python.org - - Installing Python on your machine: I am going to assume that you have access to an installation of Python, which would be the case if you are a Bio 133 student. If you don't have access to an installation, see Appendix at the end of this tutorial (yet to be written - I will wirte it after I finish writing this tutorial). If you can't wait until then and if you are using a Windows machine, see the first part of last year's lecture notes. You will find a detailed note on how to install both python and ipython there for Windows. I am pretty sure that the information is still valid. What I will have in the Appendix will basically be a cleaned up version of what I had last year. 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'. My Mac is OS X 10.5.5 (Leopard) and that is what I am using. If you are using another version or a different operating system, e.g., Windows, don't worry. What I am doing in this tutorial should be operating system independent. - Note: * This tutorial is operating system independent. Whether you are using python or ipython or some other version of Python on a Unix, Linux, Windows, Mac machine, it should not matter. * You will many program examples in the tutorial. I expect you to try them out using a python system - no exception! The code included in the tutorial has been tested using a python system, a Mac OS X 10.5.5. It should work on your python system as well. - Learning the language Python There is an excellent book that you can read to learn the basics of Python and some important concepts in computer science at the same time. It is: How to Think Like a Computer Scientist: Learning with Python Allen B. Downey, Jeffery Elkner and Chris Myers [DEM] and it is available free online here: http://www.ibiblio.org/obp/thinkCSpy/ I found a tutorial online on Python. It may be a good supplement: http://docs.python.org/tutorial/. - Several different ways to run the Python interpreter 1. shell mode You start a python session using a terminal window like this: see shellmode.txt After you see shellmode.txt, come back to this file and continue with the rest of the tutorial. That is how it will work and I won't remind you again when I say see xxx again and again below. 2. script mode You would prepare a file containing a Python program and execute/run the contents of the script file. see scriptmode.txt 3. Combining shell and script mode See the "Modules" section later for detail. - Basic type of data (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, Elkner, and Myers [DEM] is a good source to find more information. In fact, I would recommend that you keep a copy of [DEM] as you read my tutorial. . integers: 34, 2, -300, ... . floats: 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). . booleans: True, False. - Comments # Any line starting with a pound sign (#) is a 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. - 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 we will see in the example 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 operations . rules on operator precedence . same as what you would expect for common operators 4 + 3 * 2 evaluates to 24, not 14. - Operations on strings . We have alredy seen some of them in operators.py - Input from keyboard . There are two built-in functions for getting keyboard input, and they are: - rqw_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