why int object is not iterable while str is into python [duplicate]
2024/11/18 5:54:42
As i know we can not iterate int value while we can iterate strings in python.
I want to know exact reason why it is. ?
#Example
>>> p = 12
>>> for i in p:
... print i
...
Traceback (most recent call last):File "", line 1, in
TypeError: 'int' object is not iterable
>>> p = "abctest"
>>> for i in p:
... print i
...
a
b
c
t
e
s
t
>>>
Edited :
I need exact reason.Please do not say that you can use range , loop.
Answer
Finally i found correct answer.
Reason:
objects which are having __iter__ they are iterable.
and we can see
We can found __getitem__ so we can say string is iterable.
The iter built-in checks for the __iter__ method and in case of strings the __getitem__ method
i create two identical tuples and use is operator on them the answer that should come is false but when i use it in vscode/atom/notepadd++ it comes true but when i use the same code in pthon run throug…
Taking a class on Python coding and trying to use inheritance to code an answer to this problem: Write an Employee class that keeps data attributes for the following piece of information:
Employee name…
Thanks for everyones reply. I will explain here.
Suppose there is a given matrixx y B = [5,-4,5,-6]
[[0,0,0,0], [[0,1,0,1],[0,0,0,0], [0,0,0,0],[0,0,0,0],…
I recently started with coding, I use Python and Pycharm. I Installed and imported the needed "Add-ons" like Selenium.
For my first project I tried to get the "address" information …
I have three RGB images, but each one has only 1 non-zero channel (ie. one has a red channel with 0s in the blue and green channels) and I want to combine them into a single RGB image with the correct …
Im trying to parse XML to table-like structure in Python.
Imagine XML like this:<?xml version="1.0" encoding="UTF-8"?>
<base><element1>element 1</element1>…
The AI must predict the next number in a given sequence of incremental integers using Python, but so far I havent gotten the intended result. I tried changing the learning rate and iterations but so fa…
Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 8…
I am coding in python and I cannot seem to figure out why when the amount of tickets sold is entered it does not calculate the full price of the tickets that were sold. Any help is appreciated, Thanks.…