Q:

Repetition Structures - Starting Out With Python

In 'Starting Out With Python 3rd Edition' by Tony Gaddis, one of the solutions to an exercise is as follows:
character = '#'
numRows = 7
space = ' '

for row in range(numRows):
    for col in range(row + 2):
        if col == 0 or col == row + 1:
            print(character, end='')
        else:
            print(space, end='')
    print()

No one in my class understands how this solution works, more specifically, the math in lines 6-7. Could someone explain how each line results in an output of:
##
# #
#  #
#   #
#    #
#     #
#      #

A:

Each row has one more whitespace than the previous. First iterates through the rows 
for row in range(numRows):

then, for each row iterates through the columns that must print(# or whitespaces)
for col in range(row + 2):

Now, if the column is the first or the last prints the separator character (#)
if col == 0 or col == row + 1:
    print(character, end='')

The trick here is telling print that it shouldn't end in a newline (default behaviour) like in 
print('one line')
print('another line')

If it's not the first or the last column just print a whitespace
else:
    print(space)

The last sentence is just to write the newline
print()

