So you are new to Python or programming in general? Well here are a few things that 'they' don't tell you up front that help a lot.
Commenting & commenting out:
In python there are two ways to "comment things out":
One - the hash mark - # some text
Like this:
# This is a comment about the line of code below
print '\n This is a message to the console, yep we can print \n' # Here's another comment
So, yeah, most of the time you can put a comment in line after the code. Before the code, nope, it sees that as a commenting out of the entire line
This is also referred to as line commenting 'cause it just affects the line
What about that backslash 'n' thing ( '\n )? Those are line breaks.
Two - three quotes on either end - ''' text '''
Like this:
''' This is a comment about the line of code below. I really want to say a lot about
this code 'cause it's very important to me or I know I'm going to space out later and
forget why the heck I wrote this code, so I'm writing this huge and possibly useless
run on sentence, yeah.'''
print '\n This is a message to the console, yep we can print \n' # Here's another comment
This is called block commenting or commenting a block. 'Block' meaning several lines of text together. This method is also use to identify what are called 'doc strings' but that's off topic here.
Why did I call this "commenting out". Because it takes the text in the comments out of the path of the script interpreter. Remember in Python code is interpreted at run time, sequentially, as if it were a script for a play. Each line precedes the next.
Why would you want to do this? It doesn't affect the program, right? Right.
You'll often find times when:
1.) You want to remind yourself what you just did there in a more verbose way that your code can express.
2.) You always want to leave clues for the next programmer who comes along to maintain your code. That's just good manners.
3.) While debugging I often put in comments so I know I checked a particular piece of code.
4.) While debugging you can comment out lines of code instead of deleting them. I often:
5.) You have a strange urge to write your life story as you are writing your code ...
Commenting & commenting out:
In python there are two ways to "comment things out":
One - the hash mark - # some text
Like this:
# This is a comment about the line of code below
print '\n This is a message to the console, yep we can print \n' # Here's another comment
So, yeah, most of the time you can put a comment in line after the code. Before the code, nope, it sees that as a commenting out of the entire line
This is also referred to as line commenting 'cause it just affects the line
What about that backslash 'n' thing ( '\n )? Those are line breaks.
Two - three quotes on either end - ''' text '''
Like this:
''' This is a comment about the line of code below. I really want to say a lot about
this code 'cause it's very important to me or I know I'm going to space out later and
forget why the heck I wrote this code, so I'm writing this huge and possibly useless
run on sentence, yeah.'''
print '\n This is a message to the console, yep we can print \n' # Here's another comment
This is called block commenting or commenting a block. 'Block' meaning several lines of text together. This method is also use to identify what are called 'doc strings' but that's off topic here.
Why did I call this "commenting out". Because it takes the text in the comments out of the path of the script interpreter. Remember in Python code is interpreted at run time, sequentially, as if it were a script for a play. Each line precedes the next.
Why would you want to do this? It doesn't affect the program, right? Right.
You'll often find times when:
1.) You want to remind yourself what you just did there in a more verbose way that your code can express.
2.) You always want to leave clues for the next programmer who comes along to maintain your code. That's just good manners.
3.) While debugging I often put in comments so I know I checked a particular piece of code.
4.) While debugging you can comment out lines of code instead of deleting them. I often:
- Copy a bit of suspect code
- Paste it back in just above itself
- Comment out the pasted bit
- Make changes to the original bit
5.) You have a strange urge to write your life story as you are writing your code ...