For all the newcomers to Nim and people who would like to learn some new Nim trick, there is now an official page at Nim website dedicated to that:
https://nim-lang.org/learn.html
There you can find all the learning resources (both official and from the community) you need in one place.
If you think something needs to be added, changed, etc. you can open an issue or send a pull request on website repo.
We are aware that currently (until v0.19.4 is out) link to macro tutorial is broken. You can read the tutorial here.
nim> for thingy in 0..9:
.... if thingy mod 2 == 0:
.... echo thingy
....
0
2
4
6
8
var
k: array[10, int]
for i in k.low .. k.high:
k[i] = (i + 1) * 10
if k[i] mod 2 == 0:
echo k[i]
Note the .low and .high in the loop. That's a good way to avoid index errors and also allows you to later change your array to, say, start at 1 instead of 0 and the loop will still work fine.
I don't think that's largely a question of what's your need and goal. If you want programming as a hobby Python might a be better choice. If on the other hand you plan to do programming more seriously learning Nim is probably a better investment.
Reason for my opinion: Nim is a quite easy to learn language for a serious system programming language plus it's very versatile; Nim covers "quick and dirty jobs" (that are typically done in Python or Ruby) and it allows also for, say, creating full servers (e.g. a web server). Python on the other hand is even easier to learn and has many more libraries, docu (incl. hundreds of websites about Python) and better tool support (like IDEs, editors and whatnot). It should be noted though that Nim being (not hard at all but) a bit harder to learn than Python is mainly due to what Nim is capable of and that Nim cares a lot about creating good and reliable software (which Python doesn't care about much).
My personal opinion/advice is that you should stay at least a bit with Nim. If it turns out that you find it to be too hard or if you feel that you would need much more and better docu, or ... you can still switch to Python and lose nothing (and much of what you learn with Nim will stay valid and be helpful). It shouldn't take more than a few weeks (with an hour or so per day) playing with Nim to find out whether you and Nim get along well.
How about
var k: array[10, int]
for i in countup(k.low, k.high, 2):
k[i] = (i + 1) * 10
echo k[i]
The macor tutorial link leads to 404...https://nim-lang.org/docs/tut3.html
Look at the original post at the top:
We are aware that currently (until v0.19.4 is out) link to macro tutorial is broken. You can read the tutorial here.
;)
Print only the elements of that array that are on odd indices (values 20, 40, …).
Ok, first thing you need it that exercise is to create an array and fill it with values:
var a: array[10, int]
for i in 1 .. 10:
a[i-1] = 10*i
Now comes the part you're struggling with:
for i in 0 .. 9:
if i mod 2 != 0:
echo a[i]
There are of course (as shown above) other ways to do it, but this is how I (if I were a beginner :)) would do it.