-3

I want to print numbers 1-100 in this format

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30

etc.

But I can't find out a way to do this effectively,

MarianD
  • 13,096
  • 12
  • 42
  • 54
Oli
  • 21
  • 2
  • 3
  • 8

4 Answers4

1

There are more efficient ways of doing it but seeing it looks like you are just starting out with python try using for loops to iterate through each row of 10.

for i in range(10):
  for j in range(1, 11):
    print(i * 10 + j, end=" ")
  print()
Cory L
  • 273
  • 1
  • 12
1

You can try this

Code:

lst = list(range(1,11))
numrows = 5  #Decide number of rows you want to print

for i in range(numrows):
    print [x+(i*10) for x in lst]

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
[31, 32, 33, 34, 35, 36, 37, 38, 39, 40]
[41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
Siddharth Satpathy
  • 2,737
  • 4
  • 29
  • 52
0

I would check out how mods work in Python. Such as: How to calculate a mod b in python?

for i in range(1, 101):
  print(i, end=" ")
  if(i%10==0):
      print("\n")
Zack Tarr
  • 851
  • 1
  • 8
  • 28
0
[print(i, end=" ") if i % 10 else print(i) for i in range(1, 101)]

This list comprehension is used only for its side effect (printing); the resulting list itself isn't used.

MarianD
  • 13,096
  • 12
  • 42
  • 54