Plutonic Rainbows

More Functions

Another attempt at multiplication with slightly better output. Of course, it doesn’t need to be math-based - you can use anything. That’s the beauty of it.

#multiply two numbers and divide by the third.

def calc(value1, value2, value3):
	return value1 * value2 / value3
value1 = (float(raw_input("\nThe number: ")))

value2 = (float(raw_input("\nMultiplied by: ")))

value3 = (float(raw_input("\nDivided by: ")))

answer = calc(value1, value2, value3)

print "\n{0:.0f} multiplied by {1:.0f} and divided by {2:.0f} is: {3:.1f}\n".format (value1, value2, value3, answer)

Reading Files and Numbering Lines

Python can read other files (as we know) but it can also rewind and print the lines again. Just define a function called rewind and you’re all set to go.

from sys import argv

script, input_file = argv

def print_all(f):

def rewind(f):

def print_a_line(line_count, f):
	print line_count, f.readline()

current_file = open(input_file)

print "First, let's print the whole file:\n"

print "Now rewind."

print "Let's print three lines:"

current_line = 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

Creating Lists

Looking through lists again. How to create a simple list and assign it to a variable.

albums = ['Meddle', 'Atom Heart Mother', 'Dark Side of the Moon', 'Wish You Were Here', 'The Wall']

for albums in albums:
	print "An album by Pink Floyd: %s" % albums

String Length

Python’s len() method can be used to easily find the length of a string. It’s a simple and quick way to measure the length of a string (the number of characters) without having to write a lot of code. The syntax for using the len() method is fairly straightforward, and hard to mess up. Even I can manage it.

from sys import argv

script, file = argv

source = open(file)
indata =

print "The file called '%s' is %s bytes in length." % (file, len(indata))

Remember that len() calculates the string so it can be used for characters too. You could ask Python to count the letters in a word or sentence.

Music for Installations

Out next Friday (May 4th) is the long-awaited Brian Eno box set, featuring some previously unreleased music; despite what the PR machine might say. You can order it from Amazon or Bleep.


Did anyone spot the glaring error in the last script? It misses the len(indata) which calculates the file size. I have corrected that now. Thanks to everyone who contacted me.

Coda to Sublime Text

Panic’s Coda 2 is an expensive piece of software when you are only writing in Python. I recently moved over to Sublime Text. It’s actually free, if for the occasionally prompt to buy it. If you’re on a budget, I totally recommend it.

Copying Again

A simple script that just checks the file size and copies the file to a target destination that you specify. That’s done when you pass in script, file_from, file_to = argv at the very beginning.

from sys import argv

script, file_from, file_to = argv

file_in = open(file_from)
indata =

print "The file called '%s'is %r bytes in length" % (file_from, len(indata))

file_out = open(file_to, 'w')


Functions in Math

I’ve been looking again at functions and how they work. They are great for simple math but can do a lot more besides. With that in mind, here is a simple way of multiplying numbers with user input. Remember to tell Python to use float when handling numbers.

def calc(value1, value2):
	return value1 * value2
print "\nLet's multiply two numbers.\n"	

value1 = (float(raw_input("Enter a number: ")))
value2 = (float(raw_input("Enter another number: ")))

answer = calc(value1, value2)

print "\nThe answer is {0:.2f}.\n".format (answer)