for loop in range help

This is the place for queries that don't fit in any of the other categories.

for loop in range help

Postby Hypernova » Mon Jun 10, 2013 12:45 am

I'm very new to programming and python and I am beginning to get a feel for understanding basic commands, but I'm having difficulty understanding the logic behind using the range(a,b) function in a for loop.

I'm looking at the for loops example on this page: http://cscircles.cemc.uwaterloo.ca/7c-loops/

On that very helpful site, it automatically inputs variables for input(), instead of taking user input. What I don't understand is what
Code: Select all
i
or
Code: Select all
j
is referring to when used in the function.:

Code: Select all
for i in range(0, 5):
  X = 0
  for j in range(0, 5):
    X = (X*10)+1
  print(X)


According to the site it's a variable name but where is it taking it's assigned value from? Or what's it referring to?
Hypernova
 
Posts: 18
Joined: Mon Jun 10, 2013 12:23 am

Re: for loop in range help

Postby metulburr » Mon Jun 10, 2013 1:34 am

range() is a generator. So if we do
Code: Select all
 range(10)
that essetially iterates as:
Code: Select all
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Code: Select all
for i in range(10):

i is at that point created for each index in that list

so this loop
Code: Select all
for i in range(10):
    print(i)

the first time it loops i is 0, the second time, i is 1, third time it loops, i is 2, etc. etc.

In your example, you have a nested for loop, one inside another. i is corresponding to the first for loop, and j is corresponding to the second loop. However in your loop i or j is not even used. It's just saying that "I want to do this X amount of times, i just dont care what loop count i am on"
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1473
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: for loop in range help

Postby Hypernova » Tue Jun 11, 2013 9:34 am

Thank you metalburr :)

I wrote the code and solved the exercise. Not sure if it's the best way of doing it but there it is:

Code: Select all
n=int(input())
for i in range(0,n):
   x=0
   for j in range(0,n):
      x=(x*10)+1
   n=n-1
   print(x)


The output is to make this:

Code: Select all
111
11
1
Hypernova
 
Posts: 18
Joined: Mon Jun 10, 2013 12:23 am

Re: for loop in range help

Postby Mekire » Tue Jun 11, 2013 12:34 pm

Based on the description of the problem it seems like you solved it as intended.
One thing: You should use:
Code: Select all
n-=1
instead of:
Code: Select all
n=n-1

This problem as written can definitely be solved in much simpler ways but, as I said, this seems to be the answer that the author was aiming at.

Here is another simpler way:
Code: Select all
n=int(input())
for i in range(n,0,-1):
    print('1'*i)

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: for loop in range help

Postby Hypernova » Tue Jun 11, 2013 5:48 pm

of course! A string! lol That makes complete sense. Also, in the forloop you wrote, it looks like it's counting backwards from the highest number and going into negative to keep the 0 index. Is that correct?

Thanks for the advice about n-=1. Does that basically replace any x=x update code, or is that only for loops?

Thanks again for your help :) you guys are the first people I've ever discussed programming with lol
Hypernova
 
Posts: 18
Joined: Mon Jun 10, 2013 12:23 am

Re: for loop in range help

Postby Mekire » Tue Jun 11, 2013 11:28 pm

Yes I am looping "backwards."
Code: Select all
>>> list(range(5,0,-1))
[5, 4, 3, 2, 1]

For the n-=1 thing you can use that syntax anywhere you like.

These:
Code: Select all
x = x+2
x = x-3
x = x*4

Would become:
Code: Select all
x += 2
x -= 3
x *= 4

Augmented assignment is great. Read more here: Augmented Assignment

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: for loop in range help

Postby Hypernova » Wed Jun 12, 2013 12:33 pm

Thank you very much mekire :) Very concise
Hypernova
 
Posts: 18
Joined: Mon Jun 10, 2013 12:23 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests