TypeError in Python Programming
<p>I wrote the following program for square each digit of a number. But, it is showing an error as "int" object is not iterable.</p>

<pre><code>def square_digits(num):

a = 1
for i in num:
  a = i * i        
  return a

result = square_digits(12)
print(result)
</code></pre>

<p>You're looking for the <code>range(num)</code> function.</p>

<p>You probably also want to use a <code>list</code> (denoted by <code>[brackets, and, commas]</code>).</p>

<p>Try this for example:</p>

<pre><code>my_list = [] # empty list
for char in 'aword':
    my_list.append(char * 2)
print(my_list)
</code></pre>

<p>Or to be really pythonic you can step through the data and create your list at the same time with a list comprehension. Think of it like an in-line <code>for</code> loop that returns a <code>list</code>:</p>

<pre><code>result = [char * 2 for char in 'aword']
print(result)
</code></pre>

<p>The first part (<code>char * 2</code>) tells what you want in each entry of the list. In your case that would be a squaring operation on some source data. The second part (<code>for char in 'aword'</code>) tells you where to get the source data from. In your case it would be <code>range(num)</code>.</p>

python