Python for-Loop

Viewing 4 reply threads
  • Author
    Posts
    • #2338
      Jorge
      Participant

      Hi Erik,

      Thank you for this tutorial. I am very much enjoying the process of learning programming for the first time.

      For the example below:
      <div class=””>
      <div><span class=”enlighter-g1″>>>></span> <span class=”enlighter-k1″>for</span><span class=”enlighter-text”> letter </span><span class=”enlighter-k0″>in</span> <span class=”enlighter-s0″>’Hello'</span><span class=”enlighter-text”>:</span></div>
      </div>
      <div class=””>
      <div><span class=”enlighter-text”>… </span><span class=”enlighter-m0″>print</span><span class=”enlighter-g1″>(</span><span class=”enlighter-text”>letter</span><span class=”enlighter-g1″>)</span></div>
      </div>
      <div class=””>
      <div><span class=”enlighter-text”>… </span></div>
      </div>
      <div class=””>
      <div><span class=”enlighter-text”>H</span></div>
      </div>
      <div class=””>
      <div><span class=”enlighter-text”>e</span></div>
      </div>
      <div class=””>
      <div><span class=”enlighter-text”>l</span></div>
      </div>
      <div class=””>
      <div><span class=”enlighter-text”>l</span></div>
      </div>
      <div class=””>
      <div><span class=”enlighter-text”>If I want to replicate this with a count: example</span></div>
      <div>>>>for the number in ’10′</div>
      <div>…print(number)= 1 to 10</div>
      </div>
      <div>How would this look correctly like the above example you provided?</div>
      <div></div>
      <div>Best,</div>
      <div></div>
      <div>JLuna</div>
      <div></div>
       

    • #2339
      Erik van Baaren
      Keymaster

      I’d love to help, but you’ll need to fix the formatting or repost the question. This is unreadable 🙁

    • #2340
      Jorge
      Participant

      My apologies Erik.

      In the example you provided Python for- Loop:

      >> for letter in ‘ Hello’:

      …    print (letter)

      H

      e

      l

      l

      o

      I am wondering if the same can be executed as:

      >> for the number ’10’

      …    print(number) 1 to 10

      How can execute a similar command to achieve this?

      Thank you for your help.

      JLuna

       

    • #2343
      Jorge
      Participant

      Hi Erik,

      I followed the same command string and the result worked as follow:

      >>for  number in ‘12345678910’:

      …print(number)

      ….

      1

      2

      3

      4

      5

      6

      7

      8

      9

      1

      0

      However, I am wondering if there is a better way to achieve this without following the sequence in numbers ‘12345678910’ but rather indicate the variable result of count for numbers between 1 t0 10? also the number 10 was not counted as two digits as one unit but two separate digits…

      Just curious to know how I can do this.

      Best,

       

      JLuna

    • #2344
      Erik van Baaren
      Keymaster

      OK sorry for the late reply. What you want is a range: https://docs.python.org/3.3/library/stdtypes.html?highlight=range#range

      E.g.:

      for i in range(1, 11):
          print(i)

      This will give the output you are looking for. The range() function is an iterable, more about that can be read here: https://python.land/deep-dives/python-iterator

      Alternatively, you can use a list of numbers. Something like for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:.

      But the example I gave you used a string, and with the for loop you iterate over the letters. Python sees those as strings, not as numbers.

Viewing 4 reply threads
  • You must be logged in to reply to this topic.