This is a sample code I looked up. I am a rookie and I do not really understand it.

n = int(input('Enter an interger >= 0:'))
fact = 1
for i in range(2,n+1):
        fact = fact*i
print(str(n)+' factorial is '+ str(fact))

My biggest problems are:

1) for i in range(2,n+1) how do read this? I looked up the doc and it said "assigned object to target. I can identify range is the object, and i is the target. But can anyone be more specific? is 2 the start, and n+1 the increment? I come from Matlab but I have problem with this.

May someone comment on that line, in a plain word? Thank you
2) I want to restrict the limit here. I want to set range i = 1,10, because my ideal program (in a while loop is):
while 1<= n
How do I restrict i in that for loop above?

Recommended Answers

All 5 Replies

This is a sample code I looked up. I am a rookie and I do not really understand it.

n = int(input('Enter an interger >= 0:'))
fact = 1
for i in range(2,n+1):
        fact = fact*i
print(str(n)+' factorial is '+ str(fact))

The range function will generate a list of numbers. These numbers will be in the range of the first number, 2, through 1 less than the second number. Thus if num in this case is 10 then range will create the list [2,3,4,5,6,7,8,9,10], because it will go from 2 to 1 less than the 2nd value (which given 10 would be 10+1 = 11).

Next for loops in Python do not work like for loops in most other languages. They instead mimic the foreach or forall loops in other languages. Thus for each iteration of the loop i will adopt the next number in the sequence until there are no numbers left in the sequence. Thus in the first iteration i is 2, then i is 3, then i is 4, until we get down to the last number of the sequence where i will be equal to that number. Once the final iteration is complete the for loop's condition will look to see if any numbers remain in the list (which they don 't since we looked at each of them) and since it has seen all the numbers it will exit the loop.

I hope this explains how the for loop is working well enough for you to understand. If not then feel free to ask for clarification. As for how to write the next one I will let you take a shot at it first now that you hopefully understand how for loops work in Python.

erees
Thank you very much for your time. Your explaination was really good and clear to me.

I actually went ahead and tested this out

n = int(input('Enter an interger >= 0:'))
i = range(2,n+1)
print i

Now I understood what you meant by one less than the 2nd value.
when n = 3, the range is (2,4) but python did i2 - 1 = final range number.

Restrict n
As you explained, this range(x,y) does the restriction already.
If I want i = 1,10
then I will change 2 to 1

for i in range(1,n+1)
and at the prompt (when i run the program), i will input 10 and will do i = 1 to 10.

Correct?

Thanks

Restrict n
As you explained, this range(x,y) does the restriction already.
If I want i = 1,10
then I will change 2 to 1

for i in range(1,n+1)
and at the prompt (when i run the program), i will input 10 and will do i = 1 to 10.

Correct?

Thanks

If I'm reading it all correctly then yes. To get the range of 1 to N then you need to simply change the for loop to be for i in range (1, n+1) where n is the number that came in as input. This would generate a list of 1 to 10 and then iterate through it 1 number at a time with i being equal to the current number in the list it is looking at.

You know that this code is for python 3.x?

For python 2.x

n = input('Enter an interger >= 0: ')
fact = 1
for i in range(2,n+1):
        fact = fact*i
print n, 'factorial is', fact
print 'The factorial off %d is %d' % (n, fact)  #Or like this is better

Python 3.x has only input(that return a string)
That why it`s used int(input())

I take opp this for in your post #3 you use print i <-- this will not work for python 3
Print has become a function in python 3,and only this will work print(i)
That why i think you use python 2.x

If you use Python3, then you can display what range(1, 10) iterates over with this ...
print( list(range(1, 10)) )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.