Smallest python script to print even numbers 0 to 100
I'm work on a problem which I set myself for fun, which is to create a python script which prints the even numbers from 0 to 100. The challenge is to make the script as small as possible. This is what I have so far:
for x in range(0, 101):
if (x % 2 == 0):
Currently it is 60 bytes. Can anyone think of a way to make it smaller?
print(*range(2,101,2),sep='\n')which is 30 bytes. Any smaller?
This tip is located in the python golfing tips page http://codegolf.stackexchange.com/questions/54/tips-for-golfing-in-python/1020#1020.
Is this supposed to start at `0` or `2`? I don't python much, but it looks to me like your two examples do different things. Please correct me if not.
`print(*range(101),sep='\n')` dang, not even my pedantic "you didn't say I couldn't print the numbers in between!" solution isn't as short as the other one :(
Cannot compete, but for completeness a recursive solution: `f=lambda i:i and"%s\n%i"%(f(i-2),i);print f(100)`
Python 2 - 12 characters
The decimal representation of all even numbers from 0 to 100 can be found in the output:
As a bonus, so can the odd numbers.
@Sieg, even and odd... kinda got all the bases covered, not sure how you find "a lot more" in that. ;)
Isn't the first time I have a different opinion than other users. I like your answer anyways.
@0xc0de It's one of the shortest way to obtain a big number that satisfy the property requested. Other examples are `2**952`, `2**964`, `2**975`, `3**553`, `3**612` and other `1231` with the base `< 10` and exponent `< 1000`.
@paqogomez Isn't that exactly what you did in the first place..? No need to be so defensive, I was just suggesting that may have been what Sieg meant.
Python 3, 22 (Possibly not allowed)
If the challenge is "to create a python script which prints the even numbers from 0 to 100" and not "to create a python script which prints the even numbers from 0 to 100, newline separated", then the shortest solution is:
Remember, it's very important in code golf not to put any limitations on yourself you don't have to - do what the problem asks, and no more.
Wouldn't OP's second answer in the edit make you think he wanted it newline separated?
That's entirely possibly, and is why `possibly not allowed` is in the title. On the other hand, it's also possible that he/she got caught up in matching the output of his initial program, and forgot the initial problem spec. Since I don't know which is the case, I gave this answer.
Python 2 - 26
Based on the tip on exec with string multiplication found at Tips for golfing in Python.
Python 2 in *NIX, 24
os.system('seq 0 2 100')
If you need to add
Then the total is 33 characters.
If that's allowed, then `os.system('a')`, because I happen to have a program called `a` with the desired behaviour on my system..
Python 3, 29
If you're in Python 2 and happen to have already imported the print function, you don't have to make the iterator object into a list and it becomes 25 characters:
I don't know that that's entirely fair though.
Here's another fun idea that works in python 2 or 3. It's a tad longer.
if i+2:p(i-2);print i
The Python 3 version can be shortened to `*a,=map(print,range(0,101,2))` using Extended Iterable Unpacking.
Python 2 - 24
(based on bitpwner and Sparr solution)
In the shell, "_" contains the value of the previously evaluated expression
You should note that this only works, if pasted into the interactive mode of some Python interpreters (e.g., it does not work in IPython) and not as a script.
-1 -- The question explicitly mentions a python *script*, which means the code should be written to a *file* and executed. Otherwise a better solution would simply be:`range(0,102,2)` since this would display all the even numbers (plus some commas and two brackets) on the screen.