Go Back   Science Forums
View Single Post
Old 04-21-2009   #3 (permalink)
phillip1882's Avatar
phillip1882
Thinking


Location:
florida
 
phillip1882 is a glorious beacon of lightphillip1882 is a glorious beacon of lightphillip1882 is a glorious beacon of lightphillip1882 is a glorious beacon of light
 



Not Ranked  0 score     
Re: programming for beginners

were you able to get the practice problem? it should look something like this...
Code:
x = float(raw_input("what value would you like to square root? "))
a = 0
b = x
temp = x/2
while temp*temp < x-.001 or temp*temp > x+.001:
   print temp
   if temp*temp <x:
      a = temp
      temp = (a +b)/2
   else:
      b = temp
      temp = (a+b)/2
print temp
in lesson 2 we explored conditional statements. in lesson 3 we'll explore functions.
a function is basically a piece of code that you write when you want to reuse it.
for example, i could rewrite the sqrt code above to be...
Code:
def sqroot(x):
   a = 0
   b = x
   temp = x/2
   while temp*temp < x-.001 or temp*temp > x+.001:
   etc.
and replace the print x with return x. then if i saved it as mymath.py, i could later access it. for example...
Code:
import mymath
n = 10
val = mymath.sqroot(n)
val will now approximately be the square root of 10. this can be quite handy, and you can write functions to do pretty much anything. lets take a recursive example. a recursive function is a function which calls itself, until it reaches a terminating condition.
Code:
def fibonacci(a,b,n):
   if n ==0:
      return a
   elif n ==1:
      return b
   else:
      a =a+b
      b = a+b
      n= n-2
      val =fibonacci(a,b,n)
      return val

x = fibonacci(1,1,7)
print x
in case you can't guess, the above code produces a modified fibonacci sequence. in this particular case, you can pick any two start values, and go as high as you like, within reason. let's trace the recursive calls, just so you can see what this code is doing.
first, fib is called with parameters 1,1,and 7.
then a gets the value 1, b also 1, and n gets 7.
so it runs though the two checks for n, find them both false, then sets a to be 1+1 =2, b to be 2+1 = 3, and n to be 5. then it calls fib again with these parameters.
a = 2+3 = 5, b = 5+3 = 8, n = 3.
a = 5 +8 = 13, b = 13 +8 = 21, n = 1.
finally one of the terminating conditions is met. 21 gets returned up the path 3 times before finally becomes val, then val gets returned.
now would be a good time to go over global and local variables i think.
the names are pretty self descriptive, but basically a global variable is known to everything, whereas a local variable in only known to that particular function. lets do a quick example to display the difference.
Code:
def increment1():
   global x
   x = x+1
   return x

def increment2(y):
   y = y+1
   return y

x = 5
z = increment1()
print z, x

y = 5
z = increment2(y)
print z, y
at first it may seem like you want global variables all the time, but for recursive functions they can be very hard to deal with. (try declaring a and b global in the fib function and see what you get.) for most purposes you need the local version, but there are cases that crop up when global is needed. well that's it for this lesson.
two homework problems, write a recursive function that does factorials. (x *(x-1) *(x-2) etc down to 1.) found that one too easy? try a recursive function that does all permutations of 1, 2, 3, 4, 5, 6.
Reply With Quote
 
» Advertisement
» Current Poll
Who's the sexiest man alive? Johnny Depp or Robert Pattinson?
Johnny Depp - 27.27%
3 Votes
Robert Pattinson - 0%
0 Votes
Someone else (please specify) - 45.45%
5 Votes
I'm too macho to think a guy is sexy - 27.27%
3 Votes
Total Votes: 11
You may not vote on this poll.


All times are GMT -8. The time now is 03:48 AM.

Hypography?

Hypography [n.]: A combination of "hyperlink" and "bibliography" - ie, a list of links to electronic documents. Comparable to discography and bibliography, but not cartography.

We have been online since May 2000, and aim to be the best place to find and share science-related content of all kinds.

Share the love!

Please add more science to your life. Use our RSS feeds on your blog, your portal, or your favorite feedreader!


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright © 2000-2009 Hypography
Part of the Hypography - Science for Everyone Network