DEV Community

Onaolapo-11
Onaolapo-11

Posted on

Why integer objects in python are not iterable

Day 81 [January 16th, 2025]

I need to buckle down, as I'm still lagging on day day 3 & 4 goals, "Day 3-4: Control structures (if-else, loops)", as well as day 5 (and 6) goals, "Day 5-6: Functions and modules", and Day 7 target (exercises) (Meta AI, personal communication, August 8, 2025). If I haven't covered this, I can't make progress on day 8 - 80 goals.

Goals:
As extracted from the 'Python for Software Development' textbook by Halvorsen (n.d.):

  • Plotting in Python ✅
  • Subplots✅
  • Exercises✅
  • If ... Else
  • Arrays
  • For Loops
  • Nested
  • For Loops
  • While Loops
  • Exercises
  • Creating Functions in Python - Introduction
  • Functions with multiple return values
  • Exercises
  • Creating Classes in Python
  • The init () Function
  • Exercises
  • Creating Python Modules
  • Exercises

Notes:
Python for Data Science, AI & Development Course (IBM) (Santarcangelo, n.d.):
Module 3: Python Programming Fundamentals

While doing the Hands-on-Lab for the topic on Loops (Santarcangelo, n.d.),

After inputting this code:
dates = [1997,2001,20003]
N = len(dates)

for i in N:
print(dates[1])

I got a TypeError:


TypeError Traceback (most recent call last)
Cell In[8], line 4
1 dates = [1997,2001,20003]
2 N = len(dates)
----> 4 for i in N:
5 print(dates[1])

TypeError: 'int' object is not iterable

So a question came to mind: 'Why are integer objects in python not iterable?'
I found this:

Why integer objects in python are not iterable (Ramakrishna, 2021):

  • Contain singular integer values
  • Do not have 'iter' method

print(dir(int)) shows if an object contains 'iter' method or not (Ramakrishna, 2021).

Summary:
I learnt why integer objects in python are not iterable.

References:

  1. Ramakrishna, S. (2021, September 9). How to fix the python typerror: ‘int’ object is not Iterable. Hackernoon. https://hackernoon.com/how-to-fix-the-python-typeerror-int-object-is-not-tterable

  2. Halvorsen, H. (n.d.). Python. https://halvorsen.blog/documents/programming/python/python.php#python4

  3. Santarcangelo, J. (n.d.). Python for data science, AI & development [MOOC]. Coursera. https://coursera.org/learn/python-for-applied-data-science-ai

Top comments (0)