0

I have searched for a really basic explanation on what the %len does when trying to loop over a list.

This is an example of the code. What I am trying to do it to print each of the letters in the list giving a starting point. In this example I have just set it to 0. The idea is that I am going to create a cypher shift program. I know loads of people have given answers to the cypher but I just don't understand what the %len does and what I am doing that it wont work in this example.

Thanks a million in advance!

alphabet = list("abcdefghijklmnopqrstuvwzyz")


count = 0


for i in range (100):
    print (alphabet[i]%len(alphabet))
StarsSky
  • 6,721
  • 6
  • 38
  • 63
  • % is modulo or string formatting, this is distinct from len() which will give you the length of a list for example. – Graeme Stuart Mar 16 '14 at 18:15
  • First of all I can see that print alphabet[i] with i being greater than 25 will produce an index out of range error. So you could change the print statement to print(alphabet[i % len(alphabet)]) which in turn will recursively print the letter of the alphabet 4 times – Alex Koukoulas Mar 16 '14 at 18:16

3 Answers3

2

It is a remainder operator, sometimes (incorrectly) also called "modulo operator".

i your case will grow beyond the size of your string. By calculating the remainder of i and the maximum value len(alphabet) it will automatically restart at 0:

print 25 % 26 # 25
print 26 % 26 # 0
print 27 % 26 # 1
print 28 % 26 # 2
Nils Werner
  • 34,832
  • 7
  • 76
  • 98
  • Note % is a remainder, *not* a mod operator. – Grijesh Chauhan Mar 16 '14 at 18:18
  • To my knowledge the modulo operator calulates the remainder. – Nils Werner Mar 16 '14 at 18:41
  • yes for +ve number, but not for negative you would like to read [this](http://stackoverflow.com/questions/13683563/whats-the-difference-between-mod-and-remainder) – Grijesh Chauhan Mar 16 '14 at 18:46
  • hmm I got a better blog post: here [MODULUS AND REMAINDER ARE NOT THE SAME](http://www.sitecrafting.com/blog/modulus-remainder) – Grijesh Chauhan Mar 16 '14 at 18:54
  • @NilsWerner: The `%` operator in Python is not the same as the `%` in some other languages like C and Java. In Python it *is* [the modulo operator](http://docs.python.org/2/reference/expressions.html#binary-arithmetic-operations), not a remainder operator. `-1 % 4 == 3`, not `-1`, for example. – unutbu Mar 16 '14 at 20:26
1

Your code as it stands doesn't work, I get a TypeError. I think what you are trying to do is:

for i in range(100):
    print(alphabet[i % len(alphabet)])

Here the benefit of % is that x % c gives you the remainder after x is divided by c, so ensures that the index is always within the bounds of your list.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
0

In my opinion what you are trying to achieve is to iterate recursively through the alphabet list 4 times (hence 100 = 25*4) although it should be 104 instead of 100

As it is currently your code will produce a list index our of range error so you should consider changing it to:

alphabet = list("abcdefghijklmnopqrstuvwxyz")

for i in range(104):

    print (alphabet[i % len(alphabet)])
Alex Koukoulas
  • 998
  • 1
  • 7
  • 21
  • Thank you so much people, you are stars! Its really simple now you explain it! I couldnt find anywhere that would explain it simply! – clairebear8182 Mar 18 '14 at 09:01
  • No problem mate anytime :) It is nice to see people who are appreciating our effort in trying to help them and don't just ignore us ^^ – Alex Koukoulas Mar 18 '14 at 11:31