Let's take a closer look at the script above. The first line is used to indicate the location of the Python binary. As I've just explained, this line must be included in each and every Python script, and its omission is a common cause of heartache for novice programmers. Make it a habit to include it, and you'll live a healthier, happier life. Next up, we have a comment. Comments in Python are preceded by a hash (#) symbol. If you're planning to make your code publicly available on the Internet, a comment is a great way to tell members of the opposite sex all about yourself - try including your phone number for optimum results. And, of course, it makes it easier for other developers (not to mention you, circa 2010) to read and understand your code. And finally, the meat of the script: In Python, a line of code like the one above is called a "statement". Every Python program is a collection of statements, and a statement usually contains instructions to be carried out by the Python interpreter. In this particular statement, the print() function has been used to send a line of text to the screen. Like all programming languages, Python comes with a set of built-in functions - the print() function is one you'll be seeing a lot of in the future. The text to be printed is included within single or double quotes. Just as you print text, you can also print numbers... ...or the results of numeric calculations. Although every Python statement usually begins on a new line, a single statement can be split across lines with a backslash (). It is not necessary to end each statement with a semicolon (;), as in Perl and PHP.
blog comments powered by Disqus |