0

I'm new to Python but having a hard time to understand the following While Loop code as it behaves very differently. I know this code work but I do not know HOW IT WORK. Top Python experts also have no idea.

x = 1
while x < 10:
    print x
    x = x + 1 ## How does this VAR manipulate Print as the VAR comes late in the code?

I do not know whether this related to control flow or global var. Please help me out to understand deeper.

user2722968
  • 13,636
  • 2
  • 46
  • 67
Gashex
  • 7
  • 6

3 Answers3

0

x = 1 #Declares the variable while x < 10: #checks whether the value of x is less than 10 every time the loop runs print x #prints value of x for the each iterations(1 for first iteration, 2 for second and so on) x = x + 1 #this is the update part
Let me tell you one more thing, you do not have any case of global variable here.
If you have trouble in understanding whether its global or local declaration, I would recommend you to follow this link

0

I believe your question is about scope. In the above example, a new memory address for var x is created on the first line and any code on lines 2,3,4 that access x can read/write to that variable.

There are only two rules here that we need to follow:

  1. x must be declared before print so that print can access that piece of memory
  2. print must be in the same or inner scope to access 'x'

Example 1 - valid (because we follow rules 1 and 2)

x = 1
print x

Example 2 - valid (because we follow rules 1 and 2)

x = 1
while x < 10:
    print x
    x = x + 1

Example 3 - valid (because we follow rules 1 and 2)

x = 1
while x < 10:
    print x
    x = x + 1
print x

Example 4 - NOT valid (because we don't follow rule 1 and print is accessing a variable that has not yet been created in memory)

print x    
x = 1

Example 5 - NOT valid (because we don't follow rule 2 and print doesn't have access to our variable's scope, x no longer exists in memory)

y = 1
while y < 5:
    x = 1
    y = y + 1
print x

It would do you good to read this entire section on variable lifetime and scope in python all the way through Exercise 1 for good practice.

Max
  • 2,072
  • 5
  • 26
  • 42
  • What bout this one? x = 1 while x < 10: x = x + 1 print x – Gashex Nov 13 '18 at 19:00
  • The results start from 2 to 9 in other code I mentioned in the above comment while the main question code results start from 1 to 9. – Gashex Nov 13 '18 at 19:19
  • I actually understand that basic example 1 but when it comes to while loop related binding things getting messy for me. – Gashex Nov 13 '18 at 19:22
  • i can't understand your example because in python tabbing is important – Max Nov 16 '18 at 07:02
  • this isn't even valid syntax, you need to review the basic docs on python indentation – Max Nov 16 '18 at 18:39
  • I'm sorry for not putting tabs. https://pastebin.com/qtqM5dt1 and this code work. It prints from 2 to 9. – Gashex Nov 17 '18 at 12:03
  • There is nothing like an 'inner scope' in Python, and indented blocks **don't** create a new scope. Your example 5 works perfectly. For more information about scoping rules, see for example https://stackoverflow.com/a/292502/550094 – Thierry Lathuille Nov 17 '18 at 22:44
-1

Counter variable act like a tiny persistent storage. While loop checks whether the stored value in that tiny persistent storage Less than 11. If the value less than 10 then it starts the loop. Inside the loop, it instructs to Print value 1 because it's the current value in that tiny persistent storage after that the other "x" inside loop instruct that tiny persistent storage to update value by adding +1. This loop continues until it gets alert from persistent storage that value stored in persistent storage met 10 value. The loop will successfully exist.

This little software helped me to understand behind scenes http://pythontutor.com/visualize.html

Conclusion:

It seems For loops better for this kind things because it's clean and easy to maintain but understanding this concept is also important

Gashex
  • 7
  • 6
  • This answer to your own question is ridden with errors and misunderstandings. There is no comparison to 11, only to 10. There is no 'other' x variable, as all of your code is part of the same scope. There is no such thing as an 'alert from persistent storage to the loop'. This shouldn't be the accepted answer. – Thierry Lathuille Nov 17 '18 at 22:40
  • @ThierryLathuille I'm sorry but I made it easier for a newbie to understand. You are correct about 10. I put 11 by mistake. "alert from persistent storage to the loop" means the code check current memory. – Gashex Nov 18 '18 at 05:04
  • @ThierryLathuille What kind of definition you have for other "x' inside the code if it, not a variable? – Gashex Nov 18 '18 at 05:07
  • @ThierryLathuille Are you there? – Gashex Dec 01 '18 at 21:38