Plutonic Rainbows

Creating and Amending Lists

A quick example that loops through a predefined list of colours and creates an additional empty list to which a ranger of numbers are added and then printed out.

colors = ['red','blue','green']

assets = []

for colors in colors:
	print "The colours are: %s" % colors
	
	for n in range(1, 10):
	assets.append(n)
	
for n in assets:
	print "The number is now: %d" % n

Boolean Rules

One thing I need to learn before I go further with Python is to thoroughly learn the logic rules. Sure, you can just learn the concepts but being able to write actual code will mean memorising these rules.

  • not False = True
  • not True = False

  • True or False = True
  • True or True = True
  • False or True = True
  • False or False = False

  • True and False = False
  • True and True = True
  • False and True = False
  • False and False = False

  • not (True or False) = False
  • not (True or True) = False
  • not (False or True) = False
  • not (False or False) = True

  • not (True and False) = True
  • not (True and True) = False
  • not (False and True) = True
  • not (False and False) = True

  • 1 != 0 True
  • 1 != 1 False
  • 0 != 1 True
  • 0 != 0 False

  • 1 == 0 False
  • 1 == 1 True
  • 0 == 1 False
  • 0 == 0 True

Blockchain Hype

This happens with anything fairly new in the industry. Currently, Blockchain technologies are the magic fairy-dust that will solve all of the security problems, as well as validating everything from photos to news feeds.

Ersin Akinci writing for Medium:

So then why are there so many blockchain startups appearing? Two words: hype and monetization. The hype around blockchains makes fundraising easier if you’ve got one.

Back Again

Moved back to Panic’s Transmit after a week or so with Sublime Text. It’s too quirky and the workflow is odd. Sometimes more expensive is better.

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):
	print f.read()

def rewind(f):
	f.seek(0)

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_all(current_file)

print "Now rewind."
rewind(current_file)

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 = source.read()

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.

Error

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.