Python For Loop - Question about second loop












0















I am just starting to learn how to code in python and I am trying to get around understanding the following code:



import numpy as np
n=4
matrix=np.zeros((n,n))
for j in range (0,n):
for i in range (n-1,n-j-2,-1):
matrix[i,j]=2*n-i-j-1
print (matrix)


I would greatly appreciate if someone could please help me understand how each line executes and how the code is revaluated with the loop. Especially how can I interpret the second "for" loop regarding "i"
Thanks in advance!










share|improve this question



























    0















    I am just starting to learn how to code in python and I am trying to get around understanding the following code:



    import numpy as np
    n=4
    matrix=np.zeros((n,n))
    for j in range (0,n):
    for i in range (n-1,n-j-2,-1):
    matrix[i,j]=2*n-i-j-1
    print (matrix)


    I would greatly appreciate if someone could please help me understand how each line executes and how the code is revaluated with the loop. Especially how can I interpret the second "for" loop regarding "i"
    Thanks in advance!










    share|improve this question

























      0












      0








      0








      I am just starting to learn how to code in python and I am trying to get around understanding the following code:



      import numpy as np
      n=4
      matrix=np.zeros((n,n))
      for j in range (0,n):
      for i in range (n-1,n-j-2,-1):
      matrix[i,j]=2*n-i-j-1
      print (matrix)


      I would greatly appreciate if someone could please help me understand how each line executes and how the code is revaluated with the loop. Especially how can I interpret the second "for" loop regarding "i"
      Thanks in advance!










      share|improve this question














      I am just starting to learn how to code in python and I am trying to get around understanding the following code:



      import numpy as np
      n=4
      matrix=np.zeros((n,n))
      for j in range (0,n):
      for i in range (n-1,n-j-2,-1):
      matrix[i,j]=2*n-i-j-1
      print (matrix)


      I would greatly appreciate if someone could please help me understand how each line executes and how the code is revaluated with the loop. Especially how can I interpret the second "for" loop regarding "i"
      Thanks in advance!







      python python-2.7 loops for-loop






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 1:03









      Aylín PérezAylín Pérez

      12




      12
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Temporarily remove the matrix stuff, add a few print statements, and the code itself will tell you how the loops work!



          n=4
          for j in range (0,n):
          for i in range (n-1,n-j-2,-1):
          print(j, i)





          share|improve this answer































            0














            Not sure if StackOverflow is the right platform for explaining the code. Anyways...



            I changed the inner for loop to make it easy to understand



            import numpy as np
            n=4


            Create an n*n matrix



            matrix=np.zeros((n,n))  


            For each column in matrix



            for j in range (0,n):


            For each row in jth column, but from n-j-1 to n-1



            NOTE:
            In the original example, -1 at the end indicates reverse order. I reversed the loop order and removed the -1 at the end, to produce the same output. Please check to confirm



            n-j-1 : This should be understood by examples. For j==0 the value is n-1 -> Last row.
            For the last column j==n-1, the value is 0 -> First row. So for each column, starting from the last row, we proceed diagonally upward to the first row.



            Simply a logic/equation to move diagonally upward.



            NOTE: This is only the starting point for each column.



            n - 1 : Last row (though the second number is n, the call range(0, x) or range (x) expands from 0 to x - 1. Much like array indexing)



                for i in range (n-j-1, n):
            matrix[i,j]=2*n-i-j-1

            print (matrix)





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422537%2fpython-for-loop-question-about-second-loop%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              Temporarily remove the matrix stuff, add a few print statements, and the code itself will tell you how the loops work!



              n=4
              for j in range (0,n):
              for i in range (n-1,n-j-2,-1):
              print(j, i)





              share|improve this answer




























                0














                Temporarily remove the matrix stuff, add a few print statements, and the code itself will tell you how the loops work!



                n=4
                for j in range (0,n):
                for i in range (n-1,n-j-2,-1):
                print(j, i)





                share|improve this answer


























                  0












                  0








                  0







                  Temporarily remove the matrix stuff, add a few print statements, and the code itself will tell you how the loops work!



                  n=4
                  for j in range (0,n):
                  for i in range (n-1,n-j-2,-1):
                  print(j, i)





                  share|improve this answer













                  Temporarily remove the matrix stuff, add a few print statements, and the code itself will tell you how the loops work!



                  n=4
                  for j in range (0,n):
                  for i in range (n-1,n-j-2,-1):
                  print(j, i)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 1:33









                  John GordonJohn Gordon

                  9,81251729




                  9,81251729

























                      0














                      Not sure if StackOverflow is the right platform for explaining the code. Anyways...



                      I changed the inner for loop to make it easy to understand



                      import numpy as np
                      n=4


                      Create an n*n matrix



                      matrix=np.zeros((n,n))  


                      For each column in matrix



                      for j in range (0,n):


                      For each row in jth column, but from n-j-1 to n-1



                      NOTE:
                      In the original example, -1 at the end indicates reverse order. I reversed the loop order and removed the -1 at the end, to produce the same output. Please check to confirm



                      n-j-1 : This should be understood by examples. For j==0 the value is n-1 -> Last row.
                      For the last column j==n-1, the value is 0 -> First row. So for each column, starting from the last row, we proceed diagonally upward to the first row.



                      Simply a logic/equation to move diagonally upward.



                      NOTE: This is only the starting point for each column.



                      n - 1 : Last row (though the second number is n, the call range(0, x) or range (x) expands from 0 to x - 1. Much like array indexing)



                          for i in range (n-j-1, n):
                      matrix[i,j]=2*n-i-j-1

                      print (matrix)





                      share|improve this answer




























                        0














                        Not sure if StackOverflow is the right platform for explaining the code. Anyways...



                        I changed the inner for loop to make it easy to understand



                        import numpy as np
                        n=4


                        Create an n*n matrix



                        matrix=np.zeros((n,n))  


                        For each column in matrix



                        for j in range (0,n):


                        For each row in jth column, but from n-j-1 to n-1



                        NOTE:
                        In the original example, -1 at the end indicates reverse order. I reversed the loop order and removed the -1 at the end, to produce the same output. Please check to confirm



                        n-j-1 : This should be understood by examples. For j==0 the value is n-1 -> Last row.
                        For the last column j==n-1, the value is 0 -> First row. So for each column, starting from the last row, we proceed diagonally upward to the first row.



                        Simply a logic/equation to move diagonally upward.



                        NOTE: This is only the starting point for each column.



                        n - 1 : Last row (though the second number is n, the call range(0, x) or range (x) expands from 0 to x - 1. Much like array indexing)



                            for i in range (n-j-1, n):
                        matrix[i,j]=2*n-i-j-1

                        print (matrix)





                        share|improve this answer


























                          0












                          0








                          0







                          Not sure if StackOverflow is the right platform for explaining the code. Anyways...



                          I changed the inner for loop to make it easy to understand



                          import numpy as np
                          n=4


                          Create an n*n matrix



                          matrix=np.zeros((n,n))  


                          For each column in matrix



                          for j in range (0,n):


                          For each row in jth column, but from n-j-1 to n-1



                          NOTE:
                          In the original example, -1 at the end indicates reverse order. I reversed the loop order and removed the -1 at the end, to produce the same output. Please check to confirm



                          n-j-1 : This should be understood by examples. For j==0 the value is n-1 -> Last row.
                          For the last column j==n-1, the value is 0 -> First row. So for each column, starting from the last row, we proceed diagonally upward to the first row.



                          Simply a logic/equation to move diagonally upward.



                          NOTE: This is only the starting point for each column.



                          n - 1 : Last row (though the second number is n, the call range(0, x) or range (x) expands from 0 to x - 1. Much like array indexing)



                              for i in range (n-j-1, n):
                          matrix[i,j]=2*n-i-j-1

                          print (matrix)





                          share|improve this answer













                          Not sure if StackOverflow is the right platform for explaining the code. Anyways...



                          I changed the inner for loop to make it easy to understand



                          import numpy as np
                          n=4


                          Create an n*n matrix



                          matrix=np.zeros((n,n))  


                          For each column in matrix



                          for j in range (0,n):


                          For each row in jth column, but from n-j-1 to n-1



                          NOTE:
                          In the original example, -1 at the end indicates reverse order. I reversed the loop order and removed the -1 at the end, to produce the same output. Please check to confirm



                          n-j-1 : This should be understood by examples. For j==0 the value is n-1 -> Last row.
                          For the last column j==n-1, the value is 0 -> First row. So for each column, starting from the last row, we proceed diagonally upward to the first row.



                          Simply a logic/equation to move diagonally upward.



                          NOTE: This is only the starting point for each column.



                          n - 1 : Last row (though the second number is n, the call range(0, x) or range (x) expands from 0 to x - 1. Much like array indexing)



                              for i in range (n-j-1, n):
                          matrix[i,j]=2*n-i-j-1

                          print (matrix)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 22 '18 at 1:50









                          KrishnaKrishna

                          6021515




                          6021515






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422537%2fpython-for-loop-question-about-second-loop%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                              Alcedinidae

                              RAC Tourist Trophy