Write a Python program to generate and print a list of first and last 5 elements where the values are square of numbers between 1 and 30


The program creates an empty list called "l" and then uses a for loop to iterate through the range of numbers from 11 to 25 (not including 25). For each number in the range, the program raises it to the power of 2 and appends the result to the "l" list.

After the for loop completes, the program then prints out the first 5 elements of the "l" list using list slicing. The syntax "l[:5]" means to return the first 5 elements of the list. Then the program prints the last 5 elements of the "l" list using list slicing. The syntax "l[-5:]" means to return the last 5 elements of the list.

Source Code

l = list()
for i in range(11,25):
	l.append(i**2)
print(l[:5])
print(l[-5:])

Output

[121, 144, 169, 196, 225]
[400, 441, 484, 529, 576]

Example Programs