Iteration
– repetition of a mathematical or computational procedure applied to the result
of a previous applications, typically as a means of obtaining successively
closer approximations to the solution of the problem.
A
common pattern in assignment statements is an assignments statement that
updates a variable – where the new value of the variable depends on the old.
>>>
x = x + 1
Traceback
(most recent call last):
File "", line 1, in
NameError:
name 'x' is not defined
Unfortunately
the variable x is not defined so we got an error. The x = x +1 means get the
current value of x, add one, and then update x with the new value. In order to
successfully update variable x let’s enter the starting value.
>>>value
= raw_input(‘Enter a value:’)
Enter
a value: 5
>>>x
= int(value)
>>>x
= x + 1
>>>print
x
6
So before
you can update a variable, you have to initialize it, usually with simple
assignment but in our case we’ve used built-in Python function called raw_input
that you’ve already met in our previous lessons.
After
initialization of variable now we can update variable by adding 1 and this is
called an increment, while subtracting by 1 is called decrement.
No comments:
Post a Comment