0

I'm trying to make list contain (even numbers) with using while loop :

a=0
while a<8:
    a=a+2
    print(a,end=' ')
    t=list(a)
    print (t)

and how do I can make a code to division the numbers from the first list into two list: one for(even numbers) and the other for (odd numbers) ?

3 Answers 3

1

Use list to convert.

In [1]: a=(1,2,3)

In [2]: list(a)
Out[2]: [1, 2, 3]

In [3]: b=('City')

In [4]: list(b)
Out[4]: ['C', 'i', 't', 'y']
Sign up to request clarification or add additional context in comments.

Comments

1

You can just create a list directly from each variable

>>> a = list(a)
>>> a
[1, 2, 3]

>>> b = list(b)
>>> b
['C', 'i', 't', 'y']

To make a list of even numbers using a while loop, you can do something like this

a = 0
t = []
while a < 8:
    t.append(a)
    a += 2

>>> print(t)
[0, 2, 4, 6]

Note that the above is just for learning purposes, this can be done using Python's range function more easily

>>> list(range(0, 8, 2))   # Evens
[0, 2, 4, 6]

>>> list(range(1, 8, 2))   # Odds
[1, 3, 5, 7]

4 Comments

Please either edit your question to include the code and what is not working about it, or post a new question if it is separate than the one you initially asked.
a=(1,2,3,4,5,6) I'm trying to make two lists the first contain the even numbers and the other one contain the odd numbers. I used while loop and when I put (a=list(a)) it doesn't work : TypeError: 'int' object is not iterable
Post your actual while loop, not just that line
All of the formatting of your code is lost in the comment section. As I said before, please edit your post or post a new question. It is very difficult to parse your code without any formatting or indentation
-1

Here is my answer to your question:

List = []
ListEven = []
ListOdd = []
Count = 0
while(Count < 11):
    print(Count, end = ' ')
    List.append(Count)
    if(Count % 2 == 0):
        ListEven.append(Count)
    elif(Count % 2 != 0):
        ListOdd.append(Count)
    Count += 1
print("\n""This is the main list:")
print(List)
print("This is the even list:")
print(ListEven)
print("This is the odd list:")
print(ListOdd)

I fixed the Count from Count += 2 to Count += 1 because you asked for a list of odds. The code you had would only produce even numbers. I hope this helped answer your question. :)

2 Comments

-.- How is my answer not good enough the sense that someone thought it deserved a down arrow, please explain!
Your answer does not address the question any better than any of the existing answers, includes extraneous and unexplained code that is not needed to answer the question, does not follow standard python coding style, and was added over a year after the original question was both asked and correctly answered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.