Self-study python notes with a small turtle...List comprehension/list comprehension

After reading these notes, you can get started with Python.
Watch the video of Little Turtle on Station B, and organize your notes by the way. .

List comprehension is also called list comprehension

Have you heard of list comprehensions or list comprehensions?
Insert picture description here
List comprehension can be used to dynamically create a list, the syntax:
[A expression for A in B]
is equivalent to the above code

list1 = []
for x in range(10):
	list1.append(x**2)

Give another example:

 list1 = [(x, y) for x in range(10) for y in range(10) if x%2==0 if y%2!=0]

Is equivalent to

list1 = []
for x in range(10):
    for y in range(10):
        if x%2==0:
            if y%2!=0:
                list1.append((x,y))

Run it yourself

Guess you like

Origin blog.csdn.net/A_Tu_daddy/article/details/105051821