Is there a way i print odd and even numbers separately? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Is there a way i print odd and even numbers separately? (/thread-28502.html) |
Is there a way i print odd and even numbers separately? - spalisetty06 - Jul-21-2020 I don't want the following output, I want the odd numbers, even numbers separately. Can you please help. '''odd and even numbers under 10''' for i in range(1, 11): if i % 2 == 0: print(i, " is even") else: print(i, "is odd")
RE: Is there a way i print odd and even numbers separately? - deanhystad - Jul-21-2020 You could print all the even numbers then print all the odds. If you want all the evens on the same line you can print without a newline or you could join the numbers together in a single string before printing. What you cannot do is jump back and forth between lines. RE: Is there a way i print odd and even numbers separately? - spalisetty06 - Jul-21-2020 There should be a better way than this '''odd and even numbers under 10''' print("even numbers") for i in range(1, 11): if i % 2 == 0: print(i, end=" ") print() print("odd numbers") for i in range(1, 11): if i % 2 !=0: print(i, end=" ")
RE: Is there a way i print odd and even numbers separately? - deanhystad - Jul-21-2020 There are better ways, but not a lot better. But what you are doing is kind of odd to start with. Slightly better way: print('Even numbers ', [x for x in range(0, 11, 2)]) print('Odd numbers ', [x for x in range(1, 11, 2)])Or if you don't like the brackets: print('Even numbers ', ', '.join([str(x) for x in range(0, 11, 2)])) print('Odd numbers ', ', '.join([str(x) for x in range(1, 11, 2)])) RE: Is there a way i print odd and even numbers separately? - ndc85430 - Jul-21-2020 What's wrong with that? How do you want to print them? RE: Is there a way i print odd and even numbers separately? - spalisetty06 - Jul-21-2020 There is nothing wrong but does it look like a good programming way? I am learning python, I don't want to go the wrong path. Thank you. |