Computer Science

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 (both included).

Python

Python List Manipulation

7 Likes

Answer

squares = []
for num in range(1, 31):
    squares.append(num ** 2)
first_5 = squares[:5]
last_5 = squares[-5:]
combined_list = first_5 + last_5
print("Combined list:", combined_list)

Output

Combined list: [1, 4, 9, 16, 25, 676, 729, 784, 841, 900]

Answered By

3 Likes


Related Questions