0

I see David using a lot of for loops like this, usually in a table:

for (singular variable) in (plural variable)

e.g.

for registrant in registrants

He doesn't actually declare the singular variable anywhere. He would only assign something for the plural variable.

Shouldn't he also need to declare what the singular variable too? Or is python just so smart to know the difference between singular and plural variables?

(Sorry variables are very much C programming words aren't they.. what are python's instance?.... haha)

0

In fact it is not necessary to declare the type, unlike what happens in C, the python interpreter 'knows' what kind of variable it needs depending on what registrants are. For those who study for a long time C, it is a fact that attracts attention and costs a little getting used to it, but the truth is that it is much more comfortable. Also the concept of iteration is much broader, we can not only iterate over successions of numbers, but practically about anything, if not look at the next code and its output:

>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
>>> for fruit in fruits:
        print(fruit)


orange
apple
pear
banana
kiwi
apple
banana

As you can see we have not defined fruit, we could have simply said: for i in fruits

3
  • Thanks for that. So Python is just really smart. Quick clarifications, (1) when i say for i in fruits, does it mean i have declared a type, for i? meaning each loop it goes through, i = orange followed by, i = apple, then i = pear.. ..i = banana(on the 7th/last loop)? (2) does this mean that on after the loop, i will permanently remain as 'banana'?
    – nvs0000
    May 14 '17 at 12:13
  • This is true, if you use a python shell, the last value of i stays until we close the shell, ie at the end i = 'banana', if you use script in a python interpreter this value remains until this script ends.
    – MARS
    May 14 '17 at 12:22
  • Thanks very much indeed for that!
    – nvs0000
    May 15 '17 at 15:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .