In this post we will be creating a square number generator in Python, square numbers are the result of multiplying another number by itself. See a few examples of square numbers below.
- 2 x 2 = 4 and so 4 is a square number.
- 3 x 3 = 9 and so 9 is a square number.
See the sample of Python code below where we create our square number generator, in this code example the user is required to input the amount of square numbers they want returned. These are then print to output.
while True:
number=int(input("Enter length of sequence: "))
squares = [x ** 2 for x in range(number)]
print(squares)
Some examples of output for this project can be seen below.
Enter length of squence: 5
[0, 1, 4, 9, 16]
Enter length of squence: 10
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Enter length of squence: 12
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]
Enter length of squence: 15
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196]
>>>
Take a look at some of our other content around the Python programming language by clicking here.