kandi background
kandi background
Explore Kits
kandi background
Explore Kits
Explore all Programming Style open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Programming Style

v0.26.1

3.1.4

1.3.5

Atlas

axios

v0.26.1

RxJava

3.1.4

jadx

1.3.5

fetch

RxSwift

Atlas

Popular Libraries in Programming Style

Trending New libraries in Programming Style

Top Authors in Programming Style

1

98 Libraries

2234

2

62 Libraries

12959

3

49 Libraries

7836

4

36 Libraries

22896

5

36 Libraries

2616

6

34 Libraries

250

7

28 Libraries

1933

8

26 Libraries

1089

9

24 Libraries

490

10

23 Libraries

5125

1

98 Libraries

2234

2

62 Libraries

12959

3

49 Libraries

7836

4

36 Libraries

22896

5

36 Libraries

2616

6

34 Libraries

250

7

28 Libraries

1933

8

26 Libraries

1089

9

24 Libraries

490

10

23 Libraries

5125

Trending Kits in Programming Style

No Trending Kits are available at this moment for Programming Style

Trending Discussions on Programming Style

    Use for loop or multiple prints?
    Why doesn't the rangeCheck method in the java.util.ArrayList class check for negative index?
    Are java streams able to lazilly reduce from map/filter conditions?
    Are any{}, all{}, and none{} lazy operations in Kotlin?
    Use map and zip to be more func style in 2 for loops
    malloc a "member" of struct v.s. whole struct when struct is quite simple
    Difference between Running time and Execution time in algorithm?
    Lifetime of get method in postgres Rust
    Is there a way to implement mapcar in Common Lisp using only applicative programming and avoiding recursion or iteration as programming styles?
    Create TKinter label using class method

QUESTION

Use for loop or multiple prints?

Asked 2022-Mar-01 at 21:31

What programming style should I use?

1...
2print(1)
3print(2)
4

or

1...
2print(1)
3print(2)
4...
5for i in range(1, 3):
6    print(i)
7

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

I mean the first version is redundant or not?

ANSWER

Answered 2022-Mar-01 at 21:31

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;

copy icondownload icon

1...
2print(1)
3print(2)
4...
5for i in range(1, 3):
6    print(i)
7    for la, z2, z3 in zip(layers, lz2, lz3):
8        # first row
9        ABD[0][0] += la.Q̅11 * la.thickness  # Hyer:1998, p. 290
10        ABD[0][1] += la.Q̅12 * la.thickness
11        ABD[0][2] += la.Q̅16 * la.thickness
12        ABD[0][3] += la.Q̅11 * z2
13        ABD[0][4] += la.Q̅12 * z2
14        ABD[0][5] += la.Q̅16 * z2
15        # second row
16        ABD[1][0] += la.Q̅12 * la.thickness
17        ABD[1][1] += la.Q̅22 * la.thickness
18        ABD[1][2] += la.Q̅26 * la.thickness
19        ABD[1][3] += la.Q̅12 * z2
20        ABD[1][4] += la.Q̅22 * z2
21        ABD[1][5] += la.Q̅26 * z2
22        # third row
23        ABD[2][0] += la.Q̅16 * la.thickness
24        ABD[2][1] += la.Q̅26 * la.thickness
25        ABD[2][2] += la.Q̅66 * la.thickness
26        ABD[2][3] += la.Q̅16 * z2
27        ABD[2][4] += la.Q̅26 * z2
28        ABD[2][5] += la.Q̅66 * z2
29        # fourth row
30        ABD[3][0] += la.Q̅11 * z2
31        ABD[3][1] += la.Q̅12 * z2
32        ABD[3][2] += la.Q̅16 * z2
33        ABD[3][3] += la.Q̅11 * z3
34        ABD[3][4] += la.Q̅12 * z3
35        ABD[3][5] += la.Q̅16 * z3
36        # fifth row
37        ABD[4][0] += la.Q̅12 * z2
38        ABD[4][1] += la.Q̅22 * z2
39        ABD[4][2] += la.Q̅26 * z2
40        ABD[4][3] += la.Q̅12 * z3
41        ABD[4][4] += la.Q̅22 * z3
42        ABD[4][5] += la.Q̅26 * z3
43        # sixth row
44        ABD[5][0] += la.Q̅16 * z2
45        ABD[5][1] += la.Q̅26 * z2
46        ABD[5][2] += la.Q̅66 * z2
47        ABD[5][3] += la.Q̅16 * z3
48        ABD[5][4] += la.Q̅26 * z3
49        ABD[5][5] += la.Q̅66 * z3
50        # Calculate unit thermal stress resultants.
51        # Hyer:1998, p. 445
52        Ntx += (la.Q̅11 * la.αx + la.Q̅12 * la.αy + la.Q̅16 * la.αxy) * la.thickness
53        Nty += (la.Q̅12 * la.αx + la.Q̅22 * la.αy + la.Q̅26 * la.αxy) * la.thickness
54        Ntxy += (la.Q̅16 * la.αx + la.Q̅26 * la.αy + la.Q̅66 * la.αxy) * la.thickness
55        # Calculate H matrix (derived from Barbero:2018, p. 181)
56        sb = 5 / 4 * (la.thickness - 4 * z3 / thickness ** 2)
57        H[0][0] += la.Q̅s44 * sb
58        H[0][1] += la.Q̅s45 * sb
59        H[1][0] += la.Q̅s45 * sb
60        H[1][1] += la.Q̅s55 * sb
61        # Calculate E3
62        c3 += la.thickness / la.E3
63

Source https://stackoverflow.com/questions/71314703

Community Discussions contain sources that include Stack Exchange Network

    Use for loop or multiple prints?
    Why doesn't the rangeCheck method in the java.util.ArrayList class check for negative index?
    Are java streams able to lazilly reduce from map/filter conditions?
    Are any{}, all{}, and none{} lazy operations in Kotlin?
    Use map and zip to be more func style in 2 for loops
    malloc a "member" of struct v.s. whole struct when struct is quite simple
    Difference between Running time and Execution time in algorithm?
    Lifetime of get method in postgres Rust
    Is there a way to implement mapcar in Common Lisp using only applicative programming and avoiding recursion or iteration as programming styles?
    Create TKinter label using class method

QUESTION

Use for loop or multiple prints?

Asked 2022-Mar-01 at 21:31

What programming style should I use?

1...
2print(1)
3print(2)
4

or

1...
2print(1)
3print(2)
4...
5for i in range(1, 3):
6    print(i)
7

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

I mean the first version is redundant or not?

ANSWER

Answered 2022-Mar-01 at 21:31

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;

copy icondownload icon

1...
2print(1)
3print(2)
4...
5for i in range(1, 3):
6    print(i)
7    for la, z2, z3 in zip(layers, lz2, lz3):
8        # first row
9        ABD[0][0] += la.Q̅11 * la.thickness  # Hyer:1998, p. 290
10        ABD[0][1] += la.Q̅12 * la.thickness
11        ABD[0][2] += la.Q̅16 * la.thickness
12        ABD[0][3] += la.Q̅11 * z2
13        ABD[0][4] += la.Q̅12 * z2
14        ABD[0][5] += la.Q̅16 * z2
15        # second row
16        ABD[1][0] += la.Q̅12 * la.thickness
17        ABD[1][1] += la.Q̅22 * la.thickness
18        ABD[1][2] += la.Q̅26 * la.thickness
19        ABD[1][3] += la.Q̅12 * z2
20        ABD[1][4] += la.Q̅22 * z2
21        ABD[1][5] += la.Q̅26 * z2
22        # third row
23        ABD[2][0] += la.Q̅16 * la.thickness
24        ABD[2][1] += la.Q̅26 * la.thickness
25        ABD[2][2] += la.Q̅66 * la.thickness
26        ABD[2][3] += la.Q̅16 * z2
27        ABD[2][4] += la.Q̅26 * z2
28        ABD[2][5] += la.Q̅66 * z2
29        # fourth row
30        ABD[3][0] += la.Q̅11 * z2
31        ABD[3][1] += la.Q̅12 * z2
32        ABD[3][2] += la.Q̅16 * z2
33        ABD[3][3] += la.Q̅11 * z3
34        ABD[3][4] += la.Q̅12 * z3
35        ABD[3][5] += la.Q̅16 * z3
36        # fifth row
37        ABD[4][0] += la.Q̅12 * z2
38        ABD[4][1] += la.Q̅22 * z2
39        ABD[4][2] += la.Q̅26 * z2
40        ABD[4][3] += la.Q̅12 * z3
41        ABD[4][4] += la.Q̅22 * z3
42        ABD[4][5] += la.Q̅26 * z3
43        # sixth row
44        ABD[5][0] += la.Q̅16 * z2
45        ABD[5][1] += la.Q̅26 * z2
46        ABD[5][2] += la.Q̅66 * z2
47        ABD[5][3] += la.Q̅16 * z3
48        ABD[5][4] += la.Q̅26 * z3
49        ABD[5][5] += la.Q̅66 * z3
50        # Calculate unit thermal stress resultants.
51        # Hyer:1998, p. 445
52        Ntx += (la.Q̅11 * la.αx + la.Q̅12 * la.αy + la.Q̅16 * la.αxy) * la.thickness
53        Nty += (la.Q̅12 * la.αx + la.Q̅22 * la.αy + la.Q̅26 * la.αxy) * la.thickness
54        Ntxy += (la.Q̅16 * la.αx + la.Q̅26 * la.αy + la.Q̅66 * la.αxy) * la.thickness
55        # Calculate H matrix (derived from Barbero:2018, p. 181)
56        sb = 5 / 4 * (la.thickness - 4 * z3 / thickness ** 2)
57        H[0][0] += la.Q̅s44 * sb
58        H[0][1] += la.Q̅s45 * sb
59        H[1][0] += la.Q̅s45 * sb
60        H[1][1] += la.Q̅s55 * sb
61        # Calculate E3
62        c3 += la.thickness / la.E3
63

Source https://stackoverflow.com/questions/71314703

Community Discussions contain sources that include Stack Exchange Network

Tutorials and Learning Resources in Programming Style

Tutorials and Learning Resources are not available at this moment for Programming Style

Share this Page

share link

Get latest updates on Programming Style

  • © 2022 Open Weaver Inc.