Hello
Apologies in advance for this question, I realise it's a bit basic...
I've been scripting in Perl for a few years on and off, but have recently switched to Python as I would like to manipulate financial data (eventually). So, I'm kind of starting at the beginning again to some degree.
I'm trying to work out the below code, and it all makes sense except for that 'for' loop on line 4:
What's that zero doing in there? It doesn't seem to work without it, but no amount of Googling will give me the answer.
Any help much appreciated!
Here's the code (just taken from a website I'm using):
Edit: No idea why posting this has removed the indents
Apologies in advance for this question, I realise it's a bit basic...
I've been scripting in Perl for a few years on and off, but have recently switched to Python as I would like to manipulate financial data (eventually). So, I'm kind of starting at the beginning again to some degree.
I'm trying to work out the below code, and it all makes sense except for that 'for' loop on line 4:
self.sides = [0 for i in range(no_of_sides)]
What's that zero doing in there? It doesn't seem to work without it, but no amount of Googling will give me the answer.
Any help much appreciated!
Here's the code (just taken from a website I'm using):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Polygon: def __init__( self ,no_of_sides): self .n = no_of_sides self .sides = [ 0 for i in range (no_of_sides)] def inputsides( self ): self .sides = [ float ( input ( "Enter side " + str (i + 1 ) + " : " )) for i in range ( self .n)] def dispSides( self ): for i in range ( self .n): print ( "Side" ,i + 1 , "is" , self .sides[i]) class Triangle(Polygon): def __init__( self ): Polygon.__init__( self , 3 ) def findArea( self ): a,b,c = self .sides s = (a + b + c) / 2 print (a,b,c) area = (s * (s - a) * (s - b) * (s - c)) * * 0.5 print ( 'The area of the triangle is %0.2f' % area) |