Python Code for nested loops

Does anyone know how to print the following pattern using nested loops?

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

There’s a number of way that could be done. What sort of ‘nested loop’ do you have in mind?

This sounds like a homework exercise. What have you tried?

Hints:

  • can you write down a loop that will print “1”?
  • now can you write down a loop that will print “1 2”?
  • and so on, up to “1 2 3 4 5”?
  • can you see a pattern in the loops?
  • can you write a second loop, to run the other loops inside it?

Some more hints:

  • range(1, 10) will produce the values from 1 to 9;
  • print(8, end=' ') will print 8 ollowed by a space;
  • print(i, end=' ') will print the value of i followed by a space;
  • print() will print a new line.

Give it a go, and report back what happens.

in your future you will have many more cases of “how can i do …”. now’s the time to practice figuring it out for yourself. take the whole problem and break it into smaller pieces. what’s the first thing you see that you can make one piece out of (whether you know how to do that or not). if you need to figure out how to do that piece, hopefully it is now a smaller problem and easier to figure out. go for it!

to literally answer the question you asked: yes!