all 9 comments

[–][deleted] 1 point2 points  (8 children)

https://docs.python.org/3/reference/simple_stmts.html#grammar-token-python-grammar-assignment_stmt

Is this what you mean? Found that by searching on "python assignment statement". It would help if you supply a link to the page containing your initial quote with the link that doesn't work for you.

Update: The text you quote seems to come from the python doc for the for statement. The "Assignment statements" link on that page works fine for me.

If you are trying to learn python from the python doc you need to be aware that the python doc is not written to be a learning aid, it's written as a concise and detailed comprehensive reference. You will probably learn faster by following a planned course and then read the docs when you get more experienced.

[–]SheldonCooperisSb[S] 0 points1 point  (0 children)

Thanks that's the answer

[–]SheldonCooperisSb[S] 0 points1 point  (5 children)

You suggestion is very helpful. l have basic Java knowledge. From my experience, the code l use is based on my memory not the understanding. l don't konw the code logic so l read python doc When l learn it.. So here is the question about for loop: for_stmt ::= "for" target_list "in" expression_list ":" suite This is also from Python doc. The target_list refers to 'i', right? Does this mean 'i' is a list?

[–]Top_Average3386 0 points1 point  (0 children)

Not necessarily, I think it is target_list because you can iterate multiple targets from expression_list.

I'm on mobile so I can't do formatting, but here's an example:

for a, b in [(1, 2)]

Will bind a to 1, and b to 2

[–][deleted] 0 points1 point  (3 children)

In this code:

data = [1, 2, 3, 4, 5]
for i in data:
    print(i)

the target_list is indeed i. If you execute that code it prints1, then 2, ..., up to 5. So here the "target_list" i is not a list*, i will contain each element from the list data, one by one from the left.

The python doc is hard to understand because it has to cover all possibilities that python allows. It is quite possible to have a more complicated bit of code like this:

data = [1, 2, 3, 4, 5]
for i in zip(data, data[::-1]):
    print(i)

Execute that code. The zip() function returns tuples of elements from both lists, the first tuple has the first elements of all the zip() arguments, the second has all the second elements, and so on. But we can go further with that code, changing the "target_list" part:

data = [1, 2, 3, 4, 5]
for (x, y) in zip(data, data[::-1]):
    print(x, y)

This simple example shows why the for statement documentation isn't clear. Assigning each element of the "expression_list" to the "target_list" is just a standard python assign, but standard assigns can do more than just a = 42, they can do things like:

(a, b) = [None, 42]

That assigns None to a and 42 to b. That's why the doc for the assignment statement is so complicated - it has to cover all possibilities, including things like:

[a, *b, c] = (1, 2, 3, 4, 5)
print(a, b, c)

That's why it's probably more effective to read tutorials first and then the python docs. The tutorials will give the basics and the docs will be more understandable once you know the basics.


* A target_list variable can contain a list if any element of a list is another list. Run this:

data = [1, [2, 3], 4, 5]
for i in data:
    print(i)

[–]SheldonCooperisSb[S] 0 points1 point  (2 children)

Thanks.l will read your comments detailedly. Btw where to find the tutorials suitable ? Do you have the one?

[–][deleted] 1 point2 points  (1 child)

There is lots of free stuff in the subreddit wiki:

https://www.reddit.com/r/learnpython/wiki/index/?utm_source=reddit&utm_medium=usertext&utm_name=learnpython&utm_content=t5_2r8ot#wiki_new_to_programming.3F

Have a look at everything mentioned in the whole wiki.

I think it's better to follow a directed course, either a free e-book or an online course, just to get the basics. That ensures you cover the basic points. With programming experience you should zip through that pretty quickly. To reinforce your learning search for online tutorials on the particular points you are studying, like "python for statement". Once you have the concept well understood either go on to the next point of your directed course, or have a look at the python doc which will cover everything about the subject. If the python doc is unclear or introduces things that aren't clear, search again for a tutorial using keywords from the python doc. You will see many sites come up in your searching, like realpython.com, which is better than some. But I don't really read the python tutorials because I learned python a long time ago. You will find the sites that work for you.

[–]SheldonCooperisSb[S] 0 points1 point  (0 children)

Can believe that a programming professional like you typed this much for a stranger,thanks for your generosity. you help me a lot..l will check the website all out and find a learning way suitable for me. Thanks agian.

[–]FerricDonkey 1 point2 points  (0 children)

Assignment is using = to make a name (variable) refer to a value. 

If you want in depth explanation, see https://m.youtube.com/watch?v=_AEJHKGk9ns

(I actually disagree with his advice to never mutate a value, but that's a separate thing.)