I'm beginning to learn Python by following an introductory text and have come across an exercise that implements a for
loop to read files from a directory. I've used for
loops before in R and Java and I have some questions. I'm hoping the community could help me wrap my head around about for
loops in general.
It seems (in Python at least) that special characters and numbers can't be used as the "name" in loop. What's the reason for this?
ex:
for @ in data: print (@, end= ' ')
will produce an error. However replacing @
with x
or name
for example is fine. Is this one of those things we accept, don't question, and move on?
How does the for
loop know (or evaluate) whether an object is "iterable"?
As others have mentioned, see the rules for Python variable, ie identifier, names: https://docs.python.org/3/reference/lexical_analysis.html#identifiers
An object is iterable if it has the __next__() method defined . See also https://wiki.python.org/moin/ForLoop
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.