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")
Output:
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
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.
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=" ")
Output:
even numbers
2 4 6 8 10
odd numbers
1 3 5 7 9
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)]))
What's wrong with that? How do you want to print them?
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.