Q:

Write a python program to print the following number pattern using a loop.

belongs to collection: Python Input and Output Exercises

0

Print the following pattern

Write a program to print the following number pattern using a loop.

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

  • Decide the row count, i.e., 5, because the pattern contains five rows
  • Run outer for loop 5 times using for loop and range() function
  • Run inner for loop i+1 times using for loop and range() function
    • In the first iteration of the outer loop, the inner loop will execute 1 time
    • In the second iteration of the outer loop, the inner loop will execute 2 time
    • In the third iteration of the outer loop, the inner loop will execute 3 times, and so on till row 5
  • print the value of j in each iteration of inner loop (j is the the inner loop iterator variable)
  • Display an empty line at the end of each iteration of the outer loop (empty line after each row)

Solution:

print("Number Pattern ")

# Decide the row count. (above pattern contains 5 rows)
row = 5
# start: 1
# stop: row+1 (range never include stop number in result)
# step: 1
# run loop 5 times
for i in range(1, row + 1, 1):
    # Run inner loop i+1 times
    for j in range(1, i + 1):
        print(j, end=' ')
    # empty line after each row
    print("")

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< Print First 10 natural numbers using while loop us...