Monday, February 17, 2014

Starting with Python

For tomorrow's "lab", we'll start messing around with python. Nothing fancy, just a few simple examples so you can learn to do basic calculations (like multiply stuff) and display the answer. We'll work our way up to more complicated things. If you are interested enough to mess around on your own, try the excellent tutorial here. Don't feel bad if you haven't programmed before. It will be weird at first, but you'll pick up enough to do what you need to in no time.
To start playing around, go to http://repl.it/Ogi/, where you'll find a nice online python interpreter (you may notice from the menus you can also use other languages if you like). It is limited - you won't be able to make plots or fancy graphics - but it is free and will run anywhere. See the end of the post for other options.

I'm going to post links to some example code to play with below, and this is what we'll work on Monday and Wednesday of this week. Copy & paste the text from one of those files into the left-hand frame, and click on the right arrow button that appears when you move the mouse to execute the code.The left frame is your code, the right frame is the execution result. The main thing right now is just to play with the code and try to understand how it works - you will 'break' it and make it not run, but that's how you learn.

Start with "simple-math.py" - it just shows you how to declare variables, multiply two numbers, and print them. It does it in a few different ways, however, to make a few important points. First, python distinguishes between integer and decimal math. If you use integers, it will report only integer results. If you use decimals, it will give decimal results. Thus, if you ask it to do "3/2" it will report "1", but "3.0/2.0" will give "1.5". If you want to force decimals, multiply by 1.0. It also shows how to raise a number to a power. Play with the code a bit and see that you understand the integer vs decimal distinction.

Next, look at "sums.py." This one introduces a for loop as well as the range function. The former just counts through a set of numbers, the latter specifies a range of numbers to count through. Note that we have to be careful about whether the last number is counted or not - in the same way when you say 'add the first five numbers' do you mean 0-4 or 1-5? Note also neat operators like ++, where i++ just means add 1 to the variable i. Note also in printing that you use %i for printing integers and %f for printing floats (decimals). 

Next look at "simple-function.py", which shows you how to define a function that takes in parameters and returns a value (you need this to do repeated calculations with varying parameters). We've defined the multiply(a,b) function which takes in numbers a and b and returns their product. Note that we can change variables within the program, you need to be careful about that.

Finally, look at "sums2.py." This defines a bit more complicated function that finds the sum of integers between N and M. Look carefully at how the function works - the role of the for loop, the range function, and how we keep adding to the sum. Note also that the variables declared inside the function are local in the sense that they are only valid there. Outside the indented part of the function, we can re-use the variables N and M and they mean something totally different! The last part of the file shows a technique called recursion - a powerful way of solving problems that depend on a smaller instance of the same problem. It is a bit harder to figure out this function at first, but you should be able to see that it gives the same result.

If you're wanting to play around with fancy graphics or make plots, or want to use more serious numerical libraries not available in the online interpreter, you may want to just install python on one of your own computers. One easy option is Enthought - if you don't want to mess around with a bunch of separate installations and want the scientific packages out-of-the-box, it just works. The express version is free. You can also get it directly from python.org.

No comments:

Post a Comment