Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use for loop or multiple prints?

What programming style should I use?

...
print(1)
print(2)

or

...
for i in range(1, 3):
    print(i)

The output is the same 1 and on the next line 2, but which version should I use as a Python programmer?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I mean the first version is redundant or not?

>Solution :

It depends.

There is an old rule "three or more, use for". (source)

On the other hand, sometimes unrolling a loop can offer a speed-up. (But that’s generally more true in C or assembly.)

You should do what makes your program more clear.

For example, in the code below, I wrote out the calculations for the ABD matrix of a fiber reinforced composite laminate, because making nested loops would make it more complex in this case;

    for la, z2, z3 in zip(layers, lz2, lz3):
        # first row
        ABD[0][0] += la.Q̅11 * la.thickness  # Hyer:1998, p. 290
        ABD[0][1] += la.Q̅12 * la.thickness
        ABD[0][2] += la.Q̅16 * la.thickness
        ABD[0][3] += la.Q̅11 * z2
        ABD[0][4] += la.Q̅12 * z2
        ABD[0][5] += la.Q̅16 * z2
        # second row
        ABD[1][0] += la.Q̅12 * la.thickness
        ABD[1][1] += la.Q̅22 * la.thickness
        ABD[1][2] += la.Q̅26 * la.thickness
        ABD[1][3] += la.Q̅12 * z2
        ABD[1][4] += la.Q̅22 * z2
        ABD[1][5] += la.Q̅26 * z2
        # third row
        ABD[2][0] += la.Q̅16 * la.thickness
        ABD[2][1] += la.Q̅26 * la.thickness
        ABD[2][2] += la.Q̅66 * la.thickness
        ABD[2][3] += la.Q̅16 * z2
        ABD[2][4] += la.Q̅26 * z2
        ABD[2][5] += la.Q̅66 * z2
        # fourth row
        ABD[3][0] += la.Q̅11 * z2
        ABD[3][1] += la.Q̅12 * z2
        ABD[3][2] += la.Q̅16 * z2
        ABD[3][3] += la.Q̅11 * z3
        ABD[3][4] += la.Q̅12 * z3
        ABD[3][5] += la.Q̅16 * z3
        # fifth row
        ABD[4][0] += la.Q̅12 * z2
        ABD[4][1] += la.Q̅22 * z2
        ABD[4][2] += la.Q̅26 * z2
        ABD[4][3] += la.Q̅12 * z3
        ABD[4][4] += la.Q̅22 * z3
        ABD[4][5] += la.Q̅26 * z3
        # sixth row
        ABD[5][0] += la.Q̅16 * z2
        ABD[5][1] += la.Q̅26 * z2
        ABD[5][2] += la.Q̅66 * z2
        ABD[5][3] += la.Q̅16 * z3
        ABD[5][4] += la.Q̅26 * z3
        ABD[5][5] += la.Q̅66 * z3
        # Calculate unit thermal stress resultants.
        # Hyer:1998, p. 445
        Ntx += (la.Q̅11 * la.αx + la.Q̅12 * la.αy + la.Q̅16 * la.αxy) * la.thickness
        Nty += (la.Q̅12 * la.αx + la.Q̅22 * la.αy + la.Q̅26 * la.αxy) * la.thickness
        Ntxy += (la.Q̅16 * la.αx + la.Q̅26 * la.αy + la.Q̅66 * la.αxy) * la.thickness
        # Calculate H matrix (derived from Barbero:2018, p. 181)
        sb = 5 / 4 * (la.thickness - 4 * z3 / thickness ** 2)
        H[0][0] += la.Q̅s44 * sb
        H[0][1] += la.Q̅s45 * sb
        H[1][0] += la.Q̅s45 * sb
        H[1][1] += la.Q̅s55 * sb
        # Calculate E3
        c3 += la.thickness / la.E3
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading