find the nth square number in python

To find the n-th square number in Python, we can simply use the formula n^2. Here's a function that takes an integer n and returns the n-th square number:

main.py
def find_nth_square(n):
    return n**2
40 chars
3 lines

We can then call this function with any integer value of n to get the corresponding square number. For example:

main.py
>>> find_nth_square(5)
25
>>> find_nth_square(10)
100
54 chars
5 lines

Note that the formula works because the square of an integer n is the same as n multiplied by itself. So n^2 is always the n-th square number.

gistlibby LogSnag