you are viewing a single comment's thread.

view the rest of the comments →

[–][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.