0

I am learning python from Coursera . I have written one program , According to that when I click on the screen , it draws circle . See the program below --

# Dots

# importing
import simplegui
import math

width = 600
height = 600
ball_list = []
radius = 20
colour = "Yellow"

# position ditector
def distance(p,q) :
    return math.sqrt((p[0]-q[0])**2 + (p[1]-q[1])**2)



# Mouse click -- Change the position
def click(pos) :
    ball_list.append(pos)
#    global position
#   global colour
#    if  distance(pos, position) < radius :
#        colour = "Blue"
#    else :
#        position = list(pos)
#        colour = "Yellow"


# Drawing the ball
def draw(canvas) :
    for position in ball_list :
        canvas.draw_circle(position , radius , 2, "Black" , colour)

# Creating the frame
frame = simplegui.create_frame("Dots" , 600,600)
frame.set_canvas_background("White")

frame.set_draw_handler(draw)

# mouse click 
frame.set_mouseclick_handler(click)


# Start
frame.start()

But my doubt is in def draw(canvas), for position in ball_list , I have not defined any position . I made position = list(pos) as comment . Then what is the value of position in position in ball_list , How can for loop work without any value ? What is iteration ? What is the difference between for loop and iteration ?

If above code doesn't work in your IDE, please go to http://www.codeskulptor.org/#user38_VSZ0jZ0uTh_0.py and run it .

3
  • Because it's an iterator. Commented Oct 23, 2014 at 6:33
  • Can you explain me How Commented Oct 23, 2014 at 6:34
  • I suggest you go through the excellent Python tutorial (docs.python.org/2/tutorial). From start to finish it does not take too much of your time and is definitely worth it. Commented Oct 23, 2014 at 6:36

4 Answers 4

1

for loop in python is little bit different compared to other languages. I'll try explaining with code you have written.Every time you call the click function then you are appending the pos to the list called ball_list which you need to draw the circle.

def click(pos) :
    ball_list.append(pos) ## appends the pos to ball_list

Now after you have list with pos then you call the following function.

def draw(canvas) :
    for position in ball_list :
        canvas.draw_circle(position , radius , 2, "Black" , colour)

Here the variable position iterates through all the pos that you have appended to the list ball_list starting from first till last value. And if you are wondering how and what is the value of position variable then print it's value and see it for yourself like the following:

def draw(canvas) :
        for position in ball_list :
            print position  ## prints one pos value in the ball_list for each iteration.
            canvas.draw_circle(position , radius , 2, "Black" , colour)
Sign up to request clarification or add additional context in comments.

Comments

0
a_list = [1, 2, 3, 4, 5, 6, 7]

for x in [10, 20, 30, 40, 50]:
    print x

10 20 30 40 50

for x in a_list:
   print x

1 2 3 4 5 6 7

for i in range(10):
    print i

1 2 3 4 5 6 7 8 9 10

Comments

0

Say you have your own class that you want it to be iterable:

 class MyRange(object):
     def __init__(self, low, high):
         self.low = low
         self.high = high

     def __iter__(self):
         low = self.low
         while self.high >= low:
             yield low
             low += 1

Now you can:

r = MyRange(1, 10)
for number in r:
    print number,

number now have the value returned from __iter__ in each iteration. That's how things work in Python, and in your case, you have a list, which has it's own __iter__ and used in a similar way.

Comments

0

In python, for x in y: iterates through every item in y, and assigns the value of the current iteration to the variable x so that you can reference that value inside the body of the for loop. Here's an eval.in that might help you see how it works.

Screenshot of the eval.in

Here's a brief comparison to Java, assuming that the variable "arrayList" is an ArrayList (Java) or list (Python) of things, and you want to call the method do_something_with() on each value in the list.

Python:

for x in arrayList:
    do_something_with(x)

Java:

for (int i = 0; i < arrayList.size(); i++) {
    do_something_with(arrayList.get(i));
}

Fundamentally, you COULD write Python that looks more like the Java:

for x in range(0, len(arrayList)):
    do_something_with(arrayList[x])

This code would do the exact same thing. However, Python realized that this is a common enough task that there was value in providing a level of abstraction for it (often called "syntactic sugar" in programming languages). You can read more about Python iterators, and how different kinds of data get iterated, in the python documentation.

5 Comments

Can you explain me with some simple example
Do you know any other programming languages? Perhaps it would be easier to compare to one of those. There's also a section in the python documentation that will show you how different kinds of python data are iterated.
I have some Java experience . Can't go to that link --- 403 Forbidden Request forbidden by administrative rules.
There's also --- a section in the python documentation --- that will , that "a section in the python documentation" link , I am in India , In my country I think I can't access that :(
It's just the official Python Documentation. Maybe try this link instead?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.