[Tutor] Odds and even exercise

yd ydmt923 at gmail.com
Sat Mar 27 23:33:23 CET 2010


Hi, just started python at Uni and think i am in for a rough ride with zero
> prior experience in programming.
> Anyway my problem that i can't fix my self after googling.
>
> The exercise is to generate a list of odd numbers between 1-100 and the
> same
> for even numbers.
>
> So far this is what i have
> way to display numbers 1 - 100
> numbers = range(100)



> this gives me numbers= range(0,100) as an output in python 3.0
>
   I find it easy to do all this stuff with list comprehensions, but i am a
beginner so this might not be the most efficient way to do it
  numbers=[]
  for x in range(1,101):
    numbers.append(x)

> #A way to display all odd numbers
> odd = numbers[::2]
>
  instead i do this:
  odd=[]
  for y in range(1,101,2):
    odd.append(y)


> #A way to display all even numbers
>

  even=[]
  for z in range(2,101,2):
    even.append(z)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100327/6ded8ed9/attachment.html>


More information about the Tutor mailing list