evens_to_50 = [i for i in range(51) if i % 2 == 0] print evens_to_50 

This code generates a list of even numbers up to fifty. But for me, as a new thing in programming, some moments are incomprehensible -:

  1. Why generation occurs in square brackets, they are usually used when working with indexes
  2. What does the first i do before the for loop? Why, after range (51), there is no colon and a new line with tabulation?
  3. Where else can brackets be used?
  4. In square brackets you can write as many if or for ? What are the rules in them?

    3 answers 3

    This design is called "generator". This is a way to create a list (array) filled with values ​​in one line. There is no colon just because it is not a for loop, but a generator. Let's analyze in detail:

     [i for i in range(51) if i % 2 == 0] 

    or more generally:

     [expr(variable) for variable in iterable if condition(variable)] 

    Here:

    • iterable is an object from which you can get values ​​one by one (until they run out). List, tuple, or, for example, dictionary keys. From here, values ​​are individually taken and assigned to variable . Alternate operations are called "iterations", and the above-described object is iterable .

      The range(51) expression returns a list of positive integers from 0 to 50 inclusive. We are going through them.

    • variable (arbitrary name) is just a variable that in turn takes all the values ​​from an iterable . It works exactly the same as the counter in the loop.
    • expr(variable) is any function that returns a value. It can take a variable argument, it can do anything else, it can take nothing at all. It is clear that i returns simply the value of i as it is. The values ​​returned by this function become elements of the generated list. Other examples:

      • [i**2 for i in range(51)] - squares of numbers
      • [0 for i in range(51)] - just fill with zeros
    • condition(variable) is an optional condition. If it is present, then the resulting list will include only those values ​​for which condition(variable) == True . You can write one if , but inside there can be an arbitrarily complex expression, i.e. if a(i) and b(i) or c(i)...

      In this case, select those numbers that are multiples of two (ie, even).

      Here is the equivalent:

       evens_to_50 = list() for i in range(51): if i % 2 == 0: evens_to_50 += [i] 

      If you want to better understand the basics of Python, then I advise you to go here. There are excellent courses in which this is explained in detail.

      About brackets. This is a special thing that is built into the language itself. List comprehension is called and is a way to create a list on the fly (there is always a list in square brackets), i.e. ability to declare the contents of the list right inside the list

      This is all based on the iteration mechanism. If interested, you can see my outline, there is a short about it. Catch Just please do not blame me for literary mistakes. :)

      And further. You have the word "array" in your tags. But here it turns out not an array like in C or Java, but a list. Array and list - different things and it is very important.

      • not equivalent, you have no output - tCode
      • Really did not print) - faoxis
      • This code is completely understandable to me, I also understood that in my example the code does the same, I don’t understand the rules of how my code is composed - why square brackets, why i before for, why there are no colons. tabs and other things. - Yahya
      • @Yahya I understand perfectly, at first it seems like some kind of magic ... :) From above, I added an answer with explanations. - faoxis
       print(*(i for i in range(51) if not i % 2)) print({i for i in range(51) if not i % 2}) print({i: i for i in range(51) if not i % 2}) print([i if not i % 2 else '*' for a in range(51) for i in range(a) if a > 22])