Skip to content

Tag: python-3.x

When are static variables initialized in Python?

Consider the following code When exactly does the initialization of i take place? Before the execution of the init method or after it? Answer Before. The __init__ method isn’t run until Foo is instantiated. i=1 is run whenever the class definition is encountered in the code You can see this by adding pr…

Formatting a float number without trailing zeros

When I do a simple division in Python 3, such as 123000/1000, I get 123.0, or 4/2 I get 2.0. How do I get rid of the trailing zero in Python 3’s division? EDIT: I don’t want just simple integer division. For ex, 1234/1000 should give 1.234. To be clear, this question is about formatting the output…