Why Do I Not Have To Define The Variable In A For Loop Using Range(), But I Do Have To In A While Loop In Python?
Solution 1:
Well, for
is a special statement that automatically defines the variable for you. It would be redundant to require you to declare the variable in advance.
while
is a general purpose loop construct. The condition for a while
statement doesn't even have to include a variable; for example
whileTrue:
or
whilemy_function() > 0:
Solution 2:
I'd like to approach this question from a slightly different perspective.
If we look at the official Python grammar specification, we can see that (approximately speaking), a while
statement takes a test
, while a for
statement takes an exprlist
and testlist
.
Conceptually, then, we can understand that a while
statement needs one thing: an expression that it can repeatedly evaluate.
On the other hand, a for
statement needs two: a collection of expressions to be evaluated, as well as a number of names to bind the results of those evaluations to.
With this in mind, it makes sense that a while
statement would not automatically create a temporary variable, since it can accept literals too. Conversely, a for
statement must bind to some names.
(Strictly speaking, it is valid, in terms of Python grammar, to put a literal where you would expect a name in a for
statement, but contextually that wouldn't make sense, so the language prohibits it.)
Solution 3:
In python there is no need, in most cases, to define/declare variables.
The rule is that if you write (assign) a variable then the variable is a local variable of the function; if you only read it instead then it's a global.
Variables assigned at top-level (outside any function) are global... so for example:
x = 12# this is an assignment, and because we're outside functions x# is deduced to be a globaldeffoo():
print(x) # we only "read" x, thus we're talking of the globaldefbar():
x = 3# this is an assignment inside a function, so x is local
print(x) # will print 3, not touching the globaldefbaz():
x += 3# this will generate an error: we're writing so it's a# local, but no value has been ever assigned to it so it# has "no value" and we cannot "increment" itdefbaz2():
global x # this is a declaration, even if we write in the code# x refers to the global
x += 3# Now fine... will increment the global
The for
statement is simply a loop that writes to a variable: if no declaration is present then the variable will be assumed to be a local; if there is a global
or nonlocal
declaration then the variable used will have the corresponding scope (nonlocal
is used to write to local variable of the enclosing function from code in a nested function: it's not used very frequently in Python).
Solution 4:
If you are coming from other programming languages like C
, C++
or Java
then do not confuse with for in
loop of python.
In python, for in
loop pick one item from list of items and does something with help of the picked item.
Solution 5:
For Loop iterates each element from the list until the given range. So no need of any variable to check condition.
While Loop iterates until the given condition is true. Here we need some variable or value to check the condition, So the variable num is used before the loop.
Post a Comment for "Why Do I Not Have To Define The Variable In A For Loop Using Range(), But I Do Have To In A While Loop In Python?"