Python Forum

Full Version: Dumb newbie question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:
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):

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)
Edit: No idea why posting this has removed the indents
Please use proper tags when posting code.
it's called list comprehension - https://docs.python.org/3/tutorial/datas...rehensions and you could write it like this:
class Polygon:
    def __init__(self, no_of_sides):
        self.n = no_of_sides
        self.sides = []
        for i in range(no_of_sides):
            self.sides.append(0)
it will create a list and append a 0 for every side, so let's say:
p = Polygon(no_of_sides=3)
print(p.sides)
then
Output:
[0, 0, 0]
The location of the zero is what goes into the list on each iteration
>>> no_of_sides = 5
>>> [0 for i in range(no_of_sides)]
[0, 0, 0, 0, 0]
>>> ['A' for i in range(no_of_sides)]
['A', 'A', 'A', 'A', 'A']
>>> [i for i in range(no_of_sides)]
[0, 1, 2, 3, 4]
Ah, I see!

Thanks for explanations and the link to the tutorial. It makes sense now.

(Also - I'll put the tags in next time, hadn't spotted the instructions)

Thanks again!
Just to write it in a more modern Python way so you can see how that look,and use the class.
class Polygon:
    def __init__(self, no_of_sides):
        self.n = no_of_sides
        self.sides = [0 for i in range(no_of_sides)]

    def input_sides(self):
        self.sides = [float(input(f"Enter side {i+1}: ")) for i in range(self.n)]

    @property
    def disp_sides(self):
        for i in range(self.n):
            print(f"Side {i+1} is {self.sides[i]}")

class Triangle(Polygon):   
    # Could have set it as a default value in Polygon <no_of_sides=3>  
    def __init__(self):
         super().__init__(3)

    @property
    def find_area(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(f'The area of the triangle is {area:.2f}')
Usage.
>>> obj = Triangle()
>>> obj.n
3
>>> obj.sides
[0, 0, 0]
>>> 
>>> # Overwrite sides
>>> obj.input_sides()
Enter side 1: 5
Enter side 2: 4
Enter side 3: 3
>>> obj.sides
[5.0, 4.0, 3.0]
>>> 
>>> obj.disp_sides
Side 1 is 5.0
Side 2 is 4.0
Side 3 is 3.0
>>> 
>>> obj.find_area
5.0 4.0 3.0
The area of the triangle is 6.00