DEV Community

theoretician
theoretician

Posted on

2 1

How to write for loops horizontally in python?

Top comments (6)

Collapse
 
rhymes profile image
rhymes β€’

Hi Sarah, what do you mean with horizontal for loops ?

Collapse
 
theoretician profile image
theoretician β€’

Like, to print number from 1 - 10 horizontally in a for loop.

Collapse
 
rhymes profile image
rhymes β€’

Oh I get it, you need to tell print() which ending character it needs to print, it defaults to newline:

>>> for i in range(1, 11):
...     print(i)
...
1
2
3
4
5
6
7
8
9
10
>>> for i in range(1, 11):
...     print(i, end=" ")
...
1 2 3 4 5 6 7 8 9 10
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
theoretician profile image
theoretician β€’

Awesome! it was very helpful :)

Thread Thread
 
anisrm profile image
Anis β€’

How to do that in Javascript?

Collapse
 
sugsky profile image
SUGSKY β€’

how about adding a maximum value for each horizontal line? Say 8 number each line is the maximum.

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay