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

Alan Gauld alan.gauld at yahoo.co.uk
Sat Jun 6 03:39:39 EDT 2020


On 06/06/2020 05:11, Manprit Singh wrote:

> 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