Some simple Python scripts

Print a Message to the Terminal

Code:


# file: message.py
print "When that Aprill with his shoures soote";
print "The droghte of March ath perced to the roote,";
print "And bathed every veyne in swich licour";
print "Of which vertu engendered is the flour...";

Output:


> python message.py
When that Aprill with his shoures soote
The droghte of March ath perced to the roote,
And bathed every veyne in swich licour
Of which vertu engendered is the flour...
> 

Do Some Math

Code:


# calculate.py
import math
print "2 + 3 =", 2+3
print "log(1e23) =", math.log(1e23)
print "2*sin(3.1414) = ", 2*math.sin(3.1414)

Output:


> python calculate.py
2 + 3 = 5
log(1e23) = 52.9594571389
2*sin(3.1414) =  0.000385307177203
> 

Print the Time of Day

Code:


# now.py
import datetime
now = datetime.datetime.now()
print "Now is", now.strftime("%d-%m-%Y"), "at", now.strftime("%H:%M")
print "or, more precisely, %s" % now

Output:


> python now.py
Now is 02-02-2004 at 19:55
or, more precisely, 2004-02-02 19:55:43.046953
> 

This page based on Some Simple Scripts.


Andrew Dalke
Last modified: Tue Feb 3 11:38:15 SAST 2004