<jupyter_start><jupyter_text>Chapter 7 - Iteration  Multiple Assignment Same variable, different output<jupyter_code>some_number = 5
print(some_number)

some_number = 7
print(some_number)<jupyter_output><empty_output><jupyter_text>Assignment operator `=` is different from mathematics equality "="<jupyter_code>a = 5
b = a

b == a
a = 3

b == a<jupyter_output><empty_output><jupyter_text>In mathematics if $a = 7$ then $7 = a$, in Python `a = 7` is legal and `7 = a` is not<jupyter_code>a = 7

a
7 = a<jupyter_output><empty_output><jupyter_text>***  Updating Variables<jupyter_code>x = 2
print(x)

x = x + 1
print(x)
x = 2
print(x)

x += 1 # Usual way of incrementing, in Javascript, Java and C++ this is done via x++ which gives C++ its name
print(x)
new_variable = new_variable + 1 # variables need to be initialized<jupyter_output><empty_output><jupyter_text>***  The `while` statement Repeat identical or similar tasks without errors<jupyter_code>def countdown(n):
    while n > 0:
        print(n)
        n -= 1
    print('Blastoff!')
countdown(10)<jupyter_output><empty_output><jupyter_text>1. Evaluate if condition is `True` or `False`2. If `False`, exit the `while` statement and continue with the next statement3. If `True`, execute the body and go back to step 1The loop needs one or more variables that should change, so that it will ultimately terminate. If not you create an infite loop and the application will crash. ***  `break` In case condition to exit is not known until body is executed break can jump out of the loopExample: User Input<jupyter_code>while True:
    line = input('> ')
    if line == 'done':
        break
    print(line)

print('Done!')<jupyter_output><empty_output><jupyter_text>***  Square Roots Newton's method $$y = \frac{x + \frac{a}{x}}{2}$$<jupyter_code>a = 4
x = 3
y = (x + a / x) / 2

y
x = y
y = (x + a / x) / 2

y
a = 4
x = 3

while True:
    print(x)
    y = (x + a / x) / 2
    if y == x:
        break
    x = y<jupyter_output><empty_output><jupyter_text>For most values of `a` this works fine, but in general it is dangerous to test `float` equality.Floating-point values are only approximately right: most rational numbers, like $\frac{1}{3}$, and irrational numbers, like$\sqrt{2}$, can not be represented exactly with a `float`.Rather than checking whether `x` and `y` are exactly equal, it is safer to use the built-in function `abs` to compute the absolute value, or magnitude, of the difference between them:`if abs(y-x) < epsilon:    break`Where `epsilon` has a value like `0.0000001` that determines how close is close enough.<jupyter_code>a = 4
x = 3
epsilon = 0.00000000001

while True:
    print(x)
    y = (x + a / x) / 2
    if abs(y-x) < epsilon:
        break
    x = y<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup