Hey! Don't really know where to start with the second problem set.. so what we want to do, for the position (i) in the tuple that represents the poly, is multiply the coefficient on that position with the given value for x, to the power (i). so perhaps start with a for loop? but how do I get the value of the coefficient?
ok, got it- i just write (poly[i]*x**i). but uhm, how do i use it? i tried writing poly = (1, 2, 3) x=2 but it doesn't return anything. btw, what was it with the return function- perhaps it doesn't work because i don't have one? When do you need a return function, and when don't you? if anybody bothers looking at my super simple code: http://dpaste.com/767703/ would be appreciated!
whenever u want ur function to return something we use return for ex- def square(x) : return x**2 if u want to print the result def sqaure(x): print x**2 suppose u want to use the square function to find value of x^2 + x + 2 then here u have to return a value from function.(not print value the value of x^2. value = square(x) + x + 2
the power of the term in the poly is its index in the tuple. enumerate() is a handy function: http://docs.python.org/library/functions.html#enumerate http://dpaste.com/767909/
@myank_mak i still don't really understand the difference between print and return... return is rather for definitions of functions? but wht does it actually DO? (remember i am a complete beginner- a week ago i didn't even know there was a language called python:-) @bwCA yeah that's what i meant by "to the power (i)" in my confused description above:) what i didn't initially understand was how to call the objects in the tuple/poly. in your code, does it take the index number that enumerate produces and multiplies it with the coefficient? (wow:)) but what's d.append? (couldn't find the append command in the python docs...) thanks!
print just prints the value of a certain variable. return returns the value/s computed by a certain function.
@kcpaas i don't understand. what does it mean that it "returns" the value? to what? so that you can use it later on in the program? i understand that print prints, but not that return returns:-)
you should spend some time and try to go thru the tutorial in the Python documentation. It is extremely helpful. You should go back to it periodically to ensure that you understand. append is a list method and it is in the docs. The docs should be installed on your computer and i find that they are much easier to use then the online docs. (F1 while in IDLE - windows). The index tab is awesome there are a couple of Beginners links in the getting started section of the Python wiki that have good stuff in them. http://wiki.python.org/moin/
In the code here: http://codepad.org/KENOx5I9, what return does is return the value computed by the function cube() so that you can do things to that value like assigning to a variable, printing it or including it a computation.
@bwCA yeah they're good. I'm also going through the wikibook (which i find even more complete-beginner freindly.) going to do the last problem set before the first quiz today, see how it goes:-) @kcpaas thanks got it!
Join our real-time social learning platform and learn together with your friends!