Re: [Tutor] Writing a prime number program using upper bound of square root of n
Nick, If you are using python 2.X, xrange(2,n) is just like range(2,n), the xrange function is faster. In python 3.X, they got rid of the slower range function and renamed xrange to range. The x in [x for x in xrange...] will just go from 2 to the number n, but not including n. This brings up a possible issue. You may or may not want to include that number n if n itself is a prime number, therefore you should add 1 to n. The code should then go as follows. def p(n): return [2,3,5,7]+[x for x in xrange(2,n+1) if x%2!=0 and x%3!=0 and x%5!=0 and x%7!=0] Denis On Sun, Aug 22, 2010 at 5:10 PM, Nick wrote: > I will play with this and see what happens. One question though, [x for > x in xrange(2,n) .. what is xrange or is this an error? > -- > *From:* Denis Gomes [denisg...@gmail.com] > *Sent:* Sunday, August 22, 2010 4:07 PM > *To:* Nick > *Subject:* Re: [Tutor] Writing a prime number program using upper bound of > square root of n > > Hey Nick, > >You can also try using list comprehensions to do the same thing. You > could do something as follows: > > def p(n): > return [2,3,5,7]+[x for x in xrange(2,n) if x%2!=0 and x%3!=0 and > x%5!=0 and x%7!=0] > >This is cleaner and probably faster. More Pythonic. > > Denis > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Function object
Daniel, Considering that Python is your first programming language, let's start from the absolute beginning. Before you think about what a function object is, try to understand what a function is. A function is a series of python commands put together under a common heading or name in order to achieve a specific task. A function can receive parameters from the main program, perform some operations using the parameters and return value(s) to the main program. In the following example, add is a function and it, like all other functions is defined using the def statement: def add(x,y): sum=x+y# python commands here return sum The x and y are parameters or arguments that you pass to the function add. x and y can be anything depending on what you want your function to do. In our particular example x and y will be numbers (assuming, integers) because the add function will take the parameters, adds them together and temporarily save the result in a variable called sum. The return statement will then take the result of the addition and send it back to the main program. When this happens the sum variable you created inside the function will go out of scope (i.e. you will not be able to refer to it any more). To use the function above say you have the following snippet in a file called foo.py: foo.py def add(x,y): sum=x+y return sum if __name__=="__main__":# Execution of code starts here when you type ">>> python foo.py". Python knows you defined add above. Think of this as the main program. result=add(1,1)# You are calling add and storing the value returned into a variable called result print result# Assuming you are using Python2.X Type >>> python foo.py Run it and see what happens. You should get the answer 2. From the main program, you are calling the function add and storing the value that it returns into a variable called result. Then you are printing that value to screen. Once you get to learning about classes you will get a better understanding of why a function in python is an object but I think you should try to get the mechanics down first. Experiment! Good luck. Denis On Wed, Aug 25, 2010 at 2:44 PM, Alan Gauld wrote: > > "Daniel" wrote > > > another problem regarding understanding a concept- function object. As I >> said, I do not understand what a function object is, what it does, and >> what >> can I do with it? >> > > You are actually using them all the time. > Every function in Python is a function object. > You can execute the function by putting parenthesesc after its name(filled > with any required arguments) > > def foo(): return None > > defines a function object called foo that does nothing but return None. > > We can rewrite that using the lambda operator which returns function > objects with no name: > > foo = lambda : None > > This assigns an anonymous function object to a variable foo - thus giving > it a name, just like any other variable. > > In both cases we can now call the function with: > > foo() > > The advantage of treating functions as objects is that we can store them > and then call them later. This is the basis of event driven programming > and GUIs. It also allows us to easily build reconfigurable dynamic logic > into applications - just by changing the order in which functions are > called. > > It also allows us to create functions which return functions as their > output, > but this is getting into deeper water, so I'll ignore that for now! :-) > > You will find a slightly different explanation in the Functional > Programming > topic of my tutorial > > > HTH > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trouble with exercise regarding classes
Andrew, For starters you have some errors in the way you are trying to access methods from within a class instances. For example in your code in line 7 and 8, def main(): angle, vel, h0, time = getInputs() cball = Projectile(angle, vel, h0) zenith = 0.0 while cball.getY() >= 0: cball.update(time) if Projectile.getY > zenith: # This should be -- if cball.getY()>zenith: zenith = Projectile.getY() # This should be -- zenith = cball.getY() print "\nDistance traveled: %0.1f meters." % (cball.getX()) print "The heighest the cannon ball reached was %0.1f meters." % (zenith) You access the method of an instance using the instance name that you created on line 3, not the class name. Secondly, you have to change the time variable so that the value of cball.getY() changes or else nothing will happen. Assuming that the time you are entering is a delta_t value, you can create another variable say actual_time which starts at 0 and add delta_t to it at the end of the while loop each time through. actual_time=actual_time+delta_t This will update your Y position because you will be calling cball.update(actual_time). You will converge to a solution. Good luck, Denis On Wed, Aug 25, 2010 at 9:56 PM, Andrew Martin wrote: > All I want to do is add a line that displays that maximum height the > cannonball reaches. I created a variable zenith to store the highest y > value. I then wanted to compare the current y value of the cannonball to > zenith while the cannonballs y value is greater than zero. If the > cannonballs current y value is greater than zenith, I want to have the > current value replace zenith. Finally, once the cannonball has reaches y = > 0, I wanted the program to write out the value for zenith. > > I want to compare zenith, a floating point number, with the current y > value? I thought the current y value could be retrieved by Projectile.getY. > And how do I call the getY using the instance? > > > > > On Wed, Aug 25, 2010 at 7:24 PM, Alan Gauld wrote: > >> >> "Andrew Martin" wrote >> >> >> However, when I did so I got this error: "TypeError: unbound method >>> getY() >>> must be called with Projectile instance as first argument (got nothing >>> instead) " >>> >> >> def main(): angle, vel, h0, time = getInputs() cball = Projectile(angle, vel, h0) >>> >> cball is a Projectile instance >> >> >> zenith = 0.0 while cball.getY() >= 0: >>> >> So this is fine >> >> >> cball.update(time) >>> >> >> if Projectile.getY > zenith: zenith = Projectile.getY() >>> >> But what are you doing here? >> You are trying to compare the getY method of the class with a floating >> point number? >> Then you call getY using the class rather than the instance? >> I'm confused - and so is Python... >> >> >> -- >> Alan Gauld >> Author of the Learn to Program web site >> http://www.alan-g.me.uk/ >> >> >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Hiding Superclass Methods
Hi Everyone, I have a basic python question. I am writing an n dimensional vector class by inheriting from the builtin python list object. I want to be able to hide the parent object's methods in the derived class instances. I know I can overload the method in the derived class and raise some sort of an implementation error but that is not what I had in mind. I am also not looking to use numpy. This is strictly for learning purposes. Is there a way to hide superclass methods in python? Thanks to all, Denis ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hiding Superclass Methods
Thank you both for your responses. I do have one other question if I use the method both of you describe. How do I go about implementing slicing and indexing for an object in python? A list object innately has them and that is really why I wanted to use it. I would appreciate it if you can point me to something. Denis On Mon, Oct 11, 2010 at 4:13 AM, Alan Gauld wrote: > > "Denis Gomes" wrote > > > I have a basic python question. I am writing an n dimensional vector >> class by inheriting from the builtin python list object. I want to be >> able to hide the parent object's methods in the derived class instances. >> > > Doing so would break the Liskofff Substitution Principle which says > you should be able to use your subclass anywhere that the parent > class can be used. This is a very bad thing! > > If you want to expose a reduced set of operations, rather than an > extended set, then inheritance is the wriong solution. You should > consider using delegation instead. Create a list indside your class > and forward any requests for the operations you do want to the > list object. > > The only snag with delegation in this scenartio is that you have > to write an awful lot of one-liner methods. To get round that > you can use setattr() and getattr() combined with a list of allowed > operation names. Check the operation is in the list and then > forward the request by name. That can save a lot of coding! > > HTH, > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hiding Superclass Methods
I understand where to go from here. Thanks to all who responded. Appreciate it. Denis ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor