In [2]:
# print("Hello World)
Woah !? Nothing happened!? Why is that?
The reason “hello world” did not get printed to the console in this case is because that line of code is “commented out”. Comments in Python have a very simple syntax:
# {Any text we want}
# {More gibberish}
To use comments it is actually super easy, we just type the "#" character, and this will make Python ignore everything on that line from that point on. If we want comments to span multiple lines, then we have to remember to put a # at the start of every line. For example:
In [3]:
# Lots...
# and lots...
# of comments...
You can also place comments after some code, in which case the code executes. Here, let me show you:
In [4]:
print("this works") # this works because the "#" symbol is placed AFTER the bit of code we want to run!
In [7]:
"abc" * 4 # ???
Out[7]:
In [8]:
"a" * 3 # string * number repeats the character. Thus "a" * 2 = "aa" and "az" * 2 = "azaz".
Out[8]:
In later lectures I'll explain how multiplying strings work. But for now just notice that in the first case you didn't know what was going on (because there were no helpful comments), but you understand what is happening in the second case because the comment explains the code.
Also as a quick heads up, generally speaking, if you feel you need to write comments explaining the code then this often a sign that your code isn't actually good code. Good code usually has the property that you can just tell what it is doing just by looking at it. For example:
In [17]:
print( 4 * 2 ) # Very simply and clear code, you can tell what it does just by looking at it.
print( int(chr(52)).__mul__(int(chr(50))) ) # A TERRIBLE and confusing way to calculate "4 * 2"
Complex code often requires comments to explain what it does, and that's why comments are (sort-of) bad.
Make no mistake, comments are really useful and you SHOULD use them. However, it’s often a good idea to think of comments as a last-resort; only use them if you cannot think of a way to make your code less complicated. In later lectures, I'll discuss a number of techniques for writing simple code.
In [3]:
# print("\n * ,MMM8&&&. *\n MMMM88&&&&& .\n MMMM88&&&&&&&\n * MMM88&&&&&&&&\n MMM88&&&&&&&&\n 'MMM88&&&&&&'\n 'MMM8&&&' *\n |\\___/|\n ) ( . '\n =\\ /=\n )===( *\n / \\\n | |\n / \\\n \\ /\n _/\\_/\\_/\\__ _/_/\\_/\\_/\\_/\\_/\\_/\\_/\\_/\\_/\\_\n | | | |( ( | | | | | | | | | |\n | | | | ) ) | | | | | | | | | |\n | | | |(_( | | | | | | | | | |\n | | | | | | | | | | | | | | |\n | | | | | | | | | | | | | | |\n")