15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

560<br />

The Structure of a Perl/Python Program<br />

Both Perl and Python are used in Web applications but also <strong>for</strong> more specialized<br />

uses, such as text manipulation. Perl is particularly popular in the field<br />

of bioin<strong>for</strong>matics and finance whereas Python has been adapted as a scripting<br />

language <strong>for</strong> many graphics and animation programs.<br />

Although system <strong>programming</strong> languages like C/C++ were designed to maximize<br />

the efficiency of computer equipment, languages like Perl and Python<br />

are designed to maximize the efficiency of programmers, who are now more<br />

expensive than computer equipment. When programmers need to write<br />

something in a hurry that doesn’t involve manipulating the hardware of a<br />

computer, they often turn to Perl and Python.<br />

Python is often associated with the British comedy troupe “Monty Python’s<br />

Flying Circus.” It’s considered good <strong>for</strong>m among Python programmers to slip<br />

in Monty Python references in their programs whenever possible.<br />

The Structure of a Perl/Python Program<br />

Because Perl and Python are interpreted languages, you can often type in<br />

commands a line at a time or type and save commands in a file. A simple Perl<br />

program might look like this:<br />

print “This is a simple Perl program.\n”;<br />

exit;<br />

A Python program is even simpler:<br />

print “This is a simple Python program.”<br />

Perl adapts the syntax of the C language including the use of semicolons at<br />

the end of each statement and the use of curly brackets to identify a block of<br />

commands. Python omits semicolons, and instead of using curly brackets to<br />

identify a block of comments, Python uses indentation. To identify a block of<br />

commands in Perl, use curly brackets like this:<br />

if $x > 5<br />

{<br />

command1;<br />

command2;<br />

}<br />

In Python, the same program might look like this:<br />

if x > 5:<br />

command1;<br />

command2;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!