Print for loop in one line python | Example code

Let’s see some Print for loop in single-line python examples.

Python Print 1 to 10 in One-line code

Compress Two Lines in One Line (print from 0 to 9).

for i in range(10): print(i)

Output::

Print for loop in one line python

Print square of the number in for loop one line python

List Comprehension, The code squares the first ten numbers and stores them in the list squares.

print([i**2 for i in range(10)])

Output:

Print square of the number in for loop one line python

Do comment if you have any doubts and suggestions on this Python for loop topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.