Plutonic Rainbows

Loops and Lists

I started looking at loops and lists today - while it's snowing outside. More snow tomorrow apparently. The new addition today is the range() function.

elements = []

for i in range(1, 6):
    print "Adding number %d" % i
    elements.append(i)


for i in elements:
    print "The numbers %d" % i

Limiting File Size to Copy with Python

This script uses some of the things that I have learnt so far; open(), read() and len(). I use import argv to pass in the file I wish to use.

The script then makes a decision using if and else as to copying it based on the file size. In this case, 290418 bytes. Once copied, the files are then closed, as this is good practice in Python.

from sys import argv

script, file_from, file_to = argv

file_in = open(file_from)
indata = file_in.read()

print "\nThe file size is %s bytes.\n" % len(indata)

if len(indata) > 290418:
    print "Sorry. The file is too large to copy."

    file_in.close()

else:
    raw_input("\nPress enter to copy file.\n")

    file_out = open(file_to, 'w')
    file_out.write(indata)

    print "All done."

    raw_input("Press enter to close the files.\n")

    file_in.close()
    file_out.close()

Percentage Calculator

This script will ask you to enter your amount and the percentage to be deducted before finally giving you the new amount.

# -*- coding: utf-8 -*-

def percentage(input_sum, divide, calc):
    return input_sum / divide * calc

def reduction(input_sum, answer):
    return input_sum - answer

input_sum = float(raw_input("\nEnter sum total: "))

calc = float(raw_input("\nEnter Percentage: "))

answer = percentage(input_sum, 100, calc)

reduction_total = reduction(input_sum, answer)

print "\nYou are left with £{0:.2f}\n".format (reduction_total)

Making Decisions in Python

This is a short script that shows how to use if as well as elif and else so that Python can make decisions based on the input it is given.

people = 100
cars = 30
buses = 15

if cars > people:
    print "We should take the cars."
elif cars < people:
    print "We should not take the cars."
else:
    print "We can't decide."


if buses > cars:
    print "That's too many buses."
elif buses < cars:
    print "Maybe we could take the buses."
else:
    print "We still can't decide"


if people > buses:
    print "Alright, let's just take the buses."
else:
    print "Fine. Let's stay home then."

How Rice crippled the Japanese Navy

There is a great article this month in Atlas Obscura detailing how fine, polished white rice helped destroy the Japanese navy and upset most of the residents of Tokyo.

Gleaming white rice was a status symbol—it was expensive and laborious to husk, hull, polish, and wash. In Japan, the poor ate brown rice, or other carbohydrates such as sweet potatoes or barley. The rich ate polished white rice, often to the exclusion of other foods.

In his book Beriberi in Modern Japan: The Making of a National Disease, Alexander R. Bay describes the efforts of Edo-era doctors to figure out the disease. A common suspect was dampness and damp ground. One doctor administered herbal medicines and a fasting regimen to a samurai, who died within months. Other doctors burned dried mugwort on patients’ bodies to stimulate qi and blood flow.