slice

Taking part of a list or tuple is a very common operation. For example, a list might look like this:

L = [‘Michael’, ‘Sarah’, ‘Tracy’, ‘Bob’, ‘Jack’]

The first element has an index of 0 and the second element has an index of 1…… , the index of the penultimate element is -1, and the index of the penultimate element is -2……

With indexes, you’ll find that slicing is very useful.

The iteration

The for loop iterates through a set of elements or primitives. This operation is called iteration.

List generator

List Comprehensions are a simple yet powerful Python generator that can be used to create lists.

For example, to generate a list[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], we can use list(range(1, 11)) :

But if you want to generate [1×1, 2×2, 3×3… 10×10]?

However, the method above is still a bit troublesome. If you use List Comprehensions, it will be very simple.

[x * x for x in range(1, 11)]

You can get more regular lists in a similar way.

Sharing and attention is the greatest support for the author!