4. List Comprehension. In Python, a list comprehension is a concise way to create lists. It allows you to generate a new list by applying an expression to each item in an existing list or range.
For example, you can create a list of squares of numbers from 1 to 5 using list comprehension: squares = [x**2 for x in range(1, 6)]
.
Note that the range(start, end)
creates a list of numbers of between start
and end-1
.
Use a list comprehension to create a list of the numbers 1 to 100.