As is the case with all computer programming tutorials, the first thing you learn to do is print some text to the screen. Normally it is a stupid “Hello World” program, but you are far too cool for that. Besides, the Spanish Inquisition is seeking us out, and the last thing we want to do is send a big old greeting out to the world. Here is how you print text to a monitor: #!/usr/local/bin/python #this line must be included in every program print “We are the Spanish Inquisition!” The above code will print this to your monitor: We are the Spanish Inquisition! To view the program in Python, save the file as firstprogram.py and open it in Python (type python firstprogram.py). Note that you must make sure that you can view and edit file extensions in your operating system, otherwise Notepad will change your file to a .txt and it will not work. To remedy this, simply turn on view file extensions, and rename the file with a .py extension. You will note several things about the preceding code. First, the first line: #!/usr/local/bin/python tells the computer where Python is located. If you change where your python directory is located, you will have to change it in your code as well. This line must appear in all of your Python code. Next, the line print “We are the Spanish Inquisition!” tells the computer to literally print the sentence in quotations to the user's monitor. Pretty simple right? If we wanted to print more than one line of code we could do this: #!/usr/local/bin/python #this line must be included in every program print “We are the Spanish Inquisition!” print “We have two weapons!” print “One...fear!” print “Two...obfuscation!” print “And vigilance!” print “Oh shucks did I say two weapons? I meant three.” print “Why don't I step outside, then we will re-enter and begin again.” This would result in the printout: We are the Spanish Inquisition! We have two weapons! One...fear! Two...obfuscation! And vigilance! Oh shucks did I say two weapons? I meant three. Why don't I step outside, then we will re-enter and begin again. A Brief Word On Terminology In the above code sample we worked with the print command. This was followed by quotation marks and some text, which is referred to as an argument. The text in the argument is referred to as a string. The combination of the command and the argument is known as a statement. So the line: print “We are the Spanish Inquisition!” is an example of a statement.
blog comments powered by Disqus |
|
|
|
|
|
|
|