[Tutor] Meaning of %g ?

Alan Gauld alan.gauld at freenet.co.uk
Thu Apr 13 14:28:54 CEST 2006

> What is the exact meaning of %g in Python ?

Same as in C.
Its the shorter of the Scientific notation (%e)  or Floating 
Point(%f) representations.

In 20 years of using C I've never needed it, I wouldn't worry 
too much! :-)

>From Python ducumentation:
> %g: Same as "e" if exponent is greater than -4 or less than 
> precision, "f" otherwise.

Yeah, thats not very clear.

> Why "%.15g", "%.16g", "%.17g", "%.f", "%.16f" are different ?

Why? Because Kernighan & Ritchie made them different in C.
Why? Because they did a lot of sacientific calculations and wanted 
the subtle variations I suspect.


Alan G
Author of the learn to program web tutor

More information about the Tutor mailing list