This snippet demonstrates list comprehension in Python, a concise way to create lists.
Description
This snippet demonstrates list comprehension in Python, a concise way to create lists. It generates a list of squares from a range of numbers.
Code Snippet
# Create a list of squares using list comprehension
number_range = range(1, 11) # Numbers from 1 to 10
squares = [x**2 for x in number_range]
print(squares) # Output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
#List comprehension with a conditional statement
even_squares = [x**2 for x in number_range if x % 2 == 0]
print(even_squares) # Output: [4, 16, 36, 64, 100]