difference between range / len etc. when iterating over tuples
<p>One thing upfront: I am fairly now to the coding world so maybe my question is a bit stupid ... I was trying to write a function that returns the every other element of a tuple. The easiest way obviously is </p>

<pre><code>def oddTuples(aTup):
     return aTup[::2]
</code></pre>

<p>I tried to solve it differently by using the following code</p>

<pre><code>def oddTuples(aTup):
    newTup = ()
    for i in len(aTup):
        if i%2 != 0:
            newTup = newTup + (i,)
    return newTup
</code></pre>

<p>But that doesn't give me back anything at all.</p>

<p>I thought I (if used over <code>len</code>) gives back the position, so if <code>aTup = ((12, 34, 'abc', 'dfdf', 2340))</code> the return would be <code>newTup = ((12, 'abc', 2340))</code>.</p>

<p>What's the <code>i</code> iterating over when used with <code>range</code>, <code>len</code> or -in that case- while iterating over <code>for i in aTup:</code>?</p>

<pre><code>for i in len(aTup):
</code></pre>

<p>Will raise an error because <code>len()</code> returns an integer which can not be iterated over in a <code>for</code> loop.</p>

<p>In the case of:</p>

<pre><code>for i in range(len(aTup)):
</code></pre>

<p>In each iteration of the loop <code>i</code> will be an integer starting from 0 and up to the length of your tuple <strong>- 1</strong>.</p>

<p>In the case of:</p>

<pre><code>for i in aTup:
</code></pre>

<p>Each <code>i</code> will be a member of the tuple. The best way to get used to how these things work is to just pop open an interactive interpreter and do some experiments!</p>

<pre><code>&gt;&gt;&gt; aTup = ('hello', 'world')
&gt;&gt;&gt; for i in len(aTup):
...     print i
... 
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: 'int' object is not iterable
&gt;&gt;&gt; for i in range(len(aTup)):
...     print i
... 
0
1
&gt;&gt;&gt; for i in aTup:
...     print i
... 
hello
world
&gt;&gt;&gt; 
</code></pre>

python for-loop range iteration tuples