Copyright (C) 1999 by Steve Litt, All rights reserved. Material provided as-is, use at your own risk.
pythonYou should see a message and prompt similar to this:
Python 1.5.1 (#1, Sep 3 1998, 22:51:17) [GCC 2.7.2.3] on linux-i386 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> |
If that message does not appear, do what it takes to get python installed and accessible.
print "Hello World" |
Be absolutely sure not to have any spaces to the left of the word "print". Python uses indentation sensitive syntax, and will fail if there are any spaces or tabs to the left of the print statement.
Run it with this command:
python hello.pyIt should print the words "Hello World" to the screen. If it doesn't, make sure python the file you made is called hello.py, it's in the current directory, it contains the text shown in the box above, and contains no space before the command.
#!/usr/bin/python print "Hello World" |
The top line assumes that your Python executable is in /usr/bin. If it's somewhere else, substitute that path.
Now make the file executable with this command:
chmod a+x hello.pyFinally, run the command like this:
./hello.pyIt should print the words "Hello World" to the screen. If not, troubleshoot.
Notice that a semicolon is NOT required at the end of the line. Semicolons are necessary ONLY to separate multiple commands on the same line. Otherwise, the end of the line ends the command.
#!/usr/bin/python myname = "Steve Litt" print "Hello World" print myname |
This should print a line saying "Hello World", followed by a line saying "Steve Litt" to the screen. If not, troubleshoot.
Notice these things:
#!/usr/bin/python myname = "Steve Litt" #This is a comment # So is this #And this is a comment print "Hello World" #This is an inline comment print myname |
Note the following:
#!/usr/bin/python longname = "Anderson" print longname longname = longname + "berg" print longname |
This program should print a line saying "Anderson", followed by another line saying "Andersonberg". If not, troubleshoot.
Python can do incredible things with strings, including all the regular expression activities as seen in Perl. These will be presented later in this tutorial.
#!/usr/bin/python ss = 1 while ss < 10: print "Inside loop, subscript is", ss ss = ss + 1 print "Finished looping." |
This should print out nine lines stating the subscript, and a final line saying "Finished Looping". If anything goes wrong, make sure the while statement ends with a colon and check your indentation.
Everything intended to be subservient to the while statement (in other words, all the statements to be looped through), should be indented a uniform amount to the right of the while statement. That indentation can be either hard tabs, or (my preference), a certain amount of spaces. For the sake of future maintainers of your code, use one or the other consistently.
As a generality, whenever a Python statement ends in a colon (like the while statement above), it's expected to have subservient statements, and those subservient statements must be consistently indented relative the statement with the colon.
if (i < 10)
{
printf("Less than 10\n");
i = 20;
}
or
if (i < 10)
{
printf("Less than 10\n");
i = 20;
}
or
if (i < 10){
printf("Less than 10\n");
i = 20;
}
or
if (i < 10){
printf("Less than 10\n");
i = 20;
}
or even
if (i < 10) {
printf("Less than 10\n");
i = 20;
}
Then we were urged to write readable code, even though the compiler would
accept almost anything. Without the compiler to catch indentation mistakes,
succeeding generations of maintenance programmers slowly made the code
unreadable. Not so with Python. The above if statement would be ultra
readable:
if (i < 10):
printf("Less than 10\n")
i = 20
No bracket matching, no strange indentation -- everything in a block looks
like a block.
I'm not saying indentation sensitive syntax is perfect. It can be maddening to fail because of an extra space. But in these days of inadequate documentation, the simplicity and self-documentation of Python syntax can be welcome.
#!/usr/bin/python ss = 1 while ss < 10: if ss % 3 == 0: print "Inside loop, subscript is", ss, print "Divisible by three" elif ss % 2 == 0: print "Inside loop, subscript is", ss, print "Divisible by two" else: print "Inside loop, subscript is", ss, print "Divisible neither by three nor two" ss = ss + 1 print "Finished looping." |
This should put out the following:
Inside loop, subscript is 1 Divisible neither by three nor two Inside loop, subscript is 2 Divisible by two Inside loop, subscript is 3 Divisible by three Inside loop, subscript is 4 Divisible by two Inside loop, subscript is 5 Divisible neither by three nor two Inside loop, subscript is 6 Divisible by three Inside loop, subscript is 7 Divisible neither by three nor two Inside loop, subscript is 8 Divisible by two Inside loop, subscript is 9 Divisible by three Finished looping.If there are problems, troubleshoot. Pay special attention to indentation and colons
#!/usr/bin/python month = 3 #var in if statement must have been declared if month == 1: pass elif month < 6: print "early" else: print "late" |
Here January does nothing, months Feb thru May print "early", and months June thru December print "late". This is not a particularly good example, but this is how the pass statement is used.
On the other hand, break statements can sometimes improve readability and even maintainability. This is especially true when two different conditions can stop the action. Create this break.py program, make it executable and execute it. Note that the strategic placement of the break statement prevents an extra comma after the last number. Note that the while 1 would go on forever if not for the break statement.
#!/usr/bin/python ss = 1 while 1: print ss, if(ss == 9): break; print ",", ss = ss + 1 print "." |
The output of this program should be:
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 .If it isn't, troubleshoot.
#!/usr/bin/python seasons = ["Winter","Spring","Summer","Fall"] print seasons |
['Winter', 'Spring', 'Summer', 'Fall']If not, troubleshoot.
What you have done here is created a list. Lists are mutable (changable),
both in terms of changing individual members and in terms of adding, deleting
or rearranging members.
#!/usr/bin/python seasons = ["Winter","Spring","Summer","Fall"] print len(seasons) #there are 4 seasons print seasons[0] #element 0, the 1st element print seasons[2] #element 1 print seasons[len(seasons)-1] #last element print seasons[-1] #last element print seasons[0:2] #elements 0 and 1, (1st & 2nd) print seasons[1:3] #elements 1 and 2 print seasons[1:9] #1 thru last, overrange not error print seasons[-1:9999] #last element print seasons[-1:len(seasons)] #last element print seasons[-3:-2] #3rd &2nd to last elements # print seasons[len(seasons)] would produce an error, as # the last element is seasons[3]. |
#!/usr/bin/python
seasons = ["Winter","Spring","Summer","Fall"]
n = seasons.index("Summer")
print n
print seasons[n]
|
Use this with care. If the argument to index is not a value in the list, the program fails to run. also, index pulls up only the first element with that value. To be sure all such elements have been deleted, use listname.count(string) to get a count of the number of times it appears, then run through a loop to get them all. Note also that this prevents a non-occurring string from bombing the program.
#!/usr/bin/python seasons = [] seasons.insert(len(seasons), "Spring") #append to empty list seasons.insert(0, "Winter") #insert at beginning of list seasons.insert(len(seasons), "Fall") #append to end of list print seasons seasons.insert(2, "Summer") #insert before seasons[2], "Fall" print seasons |
In this exercise, you inserted elements at the beginning, at the end, and in the middle.
#!/usr/bin/python
seasons = []
seasons.append("Winter") #append element
print seasons
seasons = seasons + ["Spring", "Summer"] #append a list to a list
print seasons
seasons.append("Fall") #append element
print seasons
|
In this exercise you used the append command to append elements, and you used the plus (+) operator to append an entire list.
#!/usr/bin/python seasons = ["Winter","Spring","Summer","Fall"] print seasons del seasons[2] print seasons seasons = ["Winter","Spring","Summer","Fall"] print seasons del seasons[1:3] print seasons |
#!/usr/bin/python seasons = ["Winter","Spring","Summer","Fall"] for x in seasons: print x, "season." |
The output here should be:
Winter season. Spring season. Summer season. Fall season.
#!/usr/bin/python lastnames = ["Albright","Barclay","Chavez","Dugan","Eng"] firstnames = ["Allen","Betty","Charles","Donna","Edward"] empno = 1001 outstring = "" for first in firstnames: for last in lastnames: print "%04d,%s,%s" % (empno, last, first) #comma delimeted empno = empno + 1 |
Note the following: