[Tutor] Mathematical operations on a list

JUDE EJEPU ejepujude at gmail.com
Wed Oct 20 20:24:53 EDT 2021


Thank you so much for the input. I have learnt more than I bargained. I
will try out these suggestions. Your input are appreciated

On Thu, 21 Oct 2021, 00:12 Alan Gauld via Tutor, <tutor at python.org> wrote:

> On 20/10/2021 21:59, JUDE EJEPU wrote:
> > Sir,
> > I created a function to calculate a variable G
> > def gfactor(dist,electro):
> >        G = pie_value() * (dist**2 - electro**2)/(2*electro)
> >     return G
> >
> > So I tried an example and it worked.
> > gFactor(10,2) ----> 75.408
> > However, when I passed a list of arguments,:
> > x = [1, 2, 3]
> > y = [0.5, 0.5, 1]
> > gFactor(x,y), I got an error
>
> I'm not sure what you expected to happen but that's
> what I would expect.
>
> dist = x = [1,2,3]
>
> What do you think [1,2,3]**2 should equal?
> What is a list squared?
>
> > Please, how may I correct the function?
>
> I suspect you may be confusing Python lists with vectors?
> If that the case you probably need to look at the
> SciPy/numpy libraries.
>
> If you do want to use lists of numbers as input then you
> will need to process the lists inside your function. I've
> no idea what you expected to happen so I can't advise
> you on how to do tHat processing, but it probably
> involves some kind of loop.
>
> And if you want to be able to pass both lists and single
> values then you'll need to test the types of the input
> at the top of the function and branch accordingly.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list