An interactive introduction to programming in Python, for human beings and whoever else
Last time we built a for loop, I did a little explaining about each step. Here’s the description we saw before:
potato, and print it times ten.
And the code looked like this:
numbers = [1, 2, 3, 4] for potato in numbers: print(potato * 10)
Sometimes it’s difficult to look back and forth between notes about code and the code itself. Luckily, Python lets you take notes inside the code! Instead of having a long description and then a chunk of code, we can actually write out code like this:
# Build a list of numbers numbers = [1, 2, 3, 4] # Take each number, call it 'potato' # And print out that number times ten for potato in numbers: print(potato * 10)
When you run it, Python ignores every line that starts with
#. Try it!
# lines are called comments, and they’re a really really useful way to keep notes to yourself. You can put anything in comments, and Python won’t ever try to run it as code! From now on, if we need to explain something step-by-step, we might use comments.
Even though comments can be helpful, I think they can sometimes make code seem more complicated than it is — so many extra lines! If you’re feeling anxious about understanding long blocks of code, sometimes it’s actually useful to remove the comments and then try to read it again.
So when you see what looks like
#hashtags floating all over the place, you’ll know whats happening: they’re just hopefully-helpful comments trying to explain complex pieces of code.