Comments in Python

(0 votes, average 0 out of 5)

Line Comments

Line comments in Python are very simple.  The # character denotes that everything afterward on that line is a comment.  

1
2
#here is a whole-line comment for some code
x = 3   #this comments code on the current line

 

Block Comments

Unfortunately, there is no specific syntax for block comments in python.  To comment a block of code, you must simply place a # before every line in the block of code you wish to comment.  Many Python IDE's, however, provide a simple way to comment out a block of code.  A tricky way of quickly block-commenting a large block of code is to prefix the block with if False: and then indent the block to be commented.  Just remember un-indent and delete the conditional afterward!


Partner Links:
Last Updated on Tuesday, 29 June 2010 12:34  
Related Articles

» Redirect all output in shell script to a file

Redirecting a single command in a script to a file is straight-forward using the > syntax. For example, to redirect all STDERR output:1echo "hello, world" 2>> /tmp/filename.logTo redirect all lines of a Bash script to a file, add a scope around all of the lines and then redirect the scope to a file. For example:123456(echo "this is line 1 of my script"cat /proc/cpuinfodmesgecho "this is the end of my script") 2>> /tmp/filename.logThis allows you to capture all output of a script...

» For Loops in PHP

To create a for loop in php, follow these simple steps:Set a counter variable to some initial value.Check to see if the conditional statement is true.Execute the code within the loop.Increment a counter at the end of each iteration through the loop.The following is pseudo php code for a for loop:123for(init counter; conditional statement; increment counter){ inner code;}As you can see, all of the above steps are taken care of within the for loop, making the syntax short and easy to...

» A Brief Introduction to Grep

One of the most useful features of operating from a linux terminal is being able to easily perform functions like grep, which searches a group of files for a matching string.  It can return lines with the matching expression, lines without the matching expression, the number of lines matched, and more.  This will serve as a basic tutorial and should cover most usage models; grep is an extremely powerful tool.  Consult the grep manual for a much more detailed description....