[Tutor] Fwd: Question regarding use of assignment expression in python 3.8.3

Alan Gauld alan.gauld at yahoo.co.uk
Sun Jun 7 07:10:32 EDT 2020


On 07/06/2020 06:48, Manprit Singh wrote:

This appears to be a duplicate posting from a few days ago,
so I'll send you the duplicate answer! :-)


> so for finding the area of the triangle using Heron's formula in Python ,
> if i write code like this , will it be a valid practise ?


Yes, if you just want to practice use of assignment expressions.

> area = ((s := (a+b+c) /2) *(s -a)*(s-b)*(s-c))**0.5

Its syntactically valid to use it here but offers very no
advantage over a separate assignment line

s = a+b+c

And has 2 minor disadvantages:
1) its more typing (2 keystrokes)
2) its harder to debug - no way to inspect s prior to executing
   the calculation.


-- 
-- 
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




More information about the Tutor mailing list