I remember vividly learning to code at age 11. I had an old computer, windows would constantly crash and the only thing I could access really was QBASIC.
Alongside a book with code snippets, I would simply write a line of code, hit run and see what happens. Then, go back to the code and 'go rogue' (meaning: change the numbers a bit and make the line 5pixels instead of 2 for example). This is where the magic happened, because I actually coded something. It wasn't part of an educational application, it was the real deal inside the real editor with a real output.
The learning always came from trial and error and in a way is very similar to how I still code things (when I'm not designing SaaS products for awesome B2B companies)
The book might be replaced with Google & Stackoverflow, but the principles are the same.
The magic of coding, I believe is in writing something that is real.