append

We’ve seen how to modify a list by concatenating another one to it. What if we would like to modify a list by adding a single value. One option would be to add a value wrapped in a list but python has a dedicated way of doing it with .append():
l = [12, 54, 'hello']
l += [38]
print(l)    # [12, 54, 'hello', 38]
l = [12, 54, 'hello']
l.append(38)
print(l)    # [12, 54, 'hello', 38]
We can declare an empty array in the beginning and add elements one by one with append:
l = []
l.append('Hi')
l.append('this')
l += ['is', 'the', 'number']
l.append(42)
print(l)       # ['Hi', 'this', 'is', 'the', 'number', 42]

Challenge

Declare an empty list and add 7 integers read from the input with append.
Change each element by replacing it with its square.
Print the resulting list in the output.
Input
Output
1 2 3 4 5 6 7
[1, 4, 9, 16, 25, 36, 49]
 

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue