Friday 19 October 2012

Using the Input Statement in Python


Python Input

INPUTTING INFORMATION INTO THE COMPUTER

Running programs is ok but they do the same thing each time. In some ways this is good, but to be really useful the computer needs input.

Let us consider the ways of entering information

counter = 1

This sets counter to hold a value


The input statement allows us to enter different information each time the program is run.

age = input (" What is your age?")

We start with a variable in this case age and after the input statement we ask a question.
What is your age?
The program looks and runs better if we put a space after the question mark and before the quote

When we type in an age now the text looks better presented.

Once the number is added then hit the enter or return key and this number is put into the age variable as the program is running.

When we ask the program to print out the answer - the value of age - then what ever you typed in is what ois displayed.
Now to get you to do some work.

Write me a program do do any times table you wish
Get the program to ask you which times table 
The get the program to ask you how many
Then use a loop and a sum to display it.

An answer is at the top of this blog.

Thursday 18 October 2012

Accessing the Raspberry Pi Remotely ssh and vnc


Accessing the Raspberry Pi Remotely ssh and vnc

There are several ways to do this. The easiest of these is using ssh to connect. This gives you access to the machine in the form of a command line.
On my laptop I have a ssh program. There are many on the market that will do this Putty being one that comes to mind. It is free and works on most platforms. It needs to be installed on your windows PC or Mac.

Once this is installed you are ready to connect. On the Raspberry Pi enter the command ip addr show



In the box I have highlighted the address that is shown. In my case it is
192.168.1.133.

All my machines on my network start with the number 192.168.1
Each different machine on your network has a unique number in the range 1 to 254. I don't have 133 computers, it was just the number my router chose.




In the ssh program connect and type in the ip address.  The first time you do this you are prompted for whether you think this is safe or not. I chose yes ( since it is my machine)You need to put in a login mine is the default that came with the raspian wheezy OS. I am prompted for the password and there I have a working command line.

The actually see what is going on it is better to use VNC. There are other alternatives such as Teamviewer but this doesn't work yet and Chrome Desktop remote through a browser such as Chromium ( you can use Chrome yet). Chromium is the open source version of Chrome. It works well on the Pi.


Setting up a VNC Server
  1. sudo apt-get install tightvncserver
  2. tightvncserver
  3. vncserver :2 -geometry 1024x728 -depth 24
  4. Install tightvnc on the Desktop
In the remote host type in your ip address of the raspberry pi mine was 192.168.1.133

Follow this with :5902
The number is :590x where x is the number you typed in when you did vncserver :2 -geometry 1024x728 -depth 24 on the Raspberry Pi,
ie vncserver :2

So in the box I type 192.168.1.133:5902
You are prompted for the password and then you are given a Raspberry pi screen. Note this one is different to the one on the TV. This is not a remote desktop, this is using two "terminals" on one computer.

Monday 15 October 2012

The for loop in Python


The Basic For Loop

an image
The for loop is much simpler in Python than in most other languages
for count in range(0,5)
The function range( ) is called with three arguments:
range ( start ,end, step)

Problems to Solve

an image
Lets try some exercises.
Write a program to count from 1 to 10
Now count from 0 to 9 using only 1 value
Count to 20 from 2 in 2's
Count from 10 to 1

Making the loop interesting

                                Lets look at some different type of code
                
        
                                #!/usr/bin/env python
                                list = [2,4,6,8,10]
                                total = 0
                                for num in list:
                                   total = total + num
                                print ("The total is: %d" % total)
        
        

        
        
So what does this code do? We have a list of items. These numbers are stored in a 


large variable called an array. This array can hold many different values not just one.


As we go through the for loop, the loop first looks at the first item in the list then 


the next then the next and so on, Each time the loop is repeated it looks at the next 


item in the list.

        
  • Python is good at using for loops in a different way to other languages.
  • We will look at more in a later lesson
an image
For loops are available in most languages and are used when you know how many times you want to do something. The loops can go forwards as well as backwards

The while loop in Python


Loops - The While Loop


There a several types of Loop. The While loop here is very similar to that used in many other programming languages. The For loop is somewhat different.

Whats this used for?

Well occasionally I get lazy and don't want to write or type out things every time. Thats where the loop comes in.

While loops are often used when you are not quite sure how many times you want to do something. I want to say hello Philip until something happens. 
Sometimes this can be just counting using a Counter in this case

What does the counter do

I put something in a box. I take it out of the box add 1 to it and put it back in.

If this is in a loop then each time I do the loop this number will get bigger. If I want to I can count in 2's by making the counter add two each time.


Here is the code to copy in 

count =0
while (count <20):
      print "the counter = ", count
      count = count + 2
print "End of prgram"

Saturday 13 October 2012

Simple Calculations in Python


Simple Calculations




Let us do a simple sum
We will make 3 variables.
box, bigbox and answer. We will put 2 in the box, 12 in bigbox and nothing in answer.
Here is the code
box = 2 # Put 2 in a variable box
bigbox = 12 # Put 12 in another variable box
answer = 0 # Here is an empty box containing nothing
answer = box + bigbox
print "The answer is ",answer

Now its your go.

Make three variables, first , second and result.
Put 2 in first, 3 in second and nothing in result.
Add the numbers up in the program and then print the answer.

Thursday 11 October 2012

Simple Variables in Python


More Python Programming

The Python Programming tutorial continues. Looking at simple variables, and using them in simple loops. Counters and loops are important to programming. They for the basis of most of the programming structures.

So what is a variable?
These things are what makes computer programes work
Lets us see if we can make some

Variables are little boxes inside the memory of the computer that store things.
Lets make on and put something in it.


Here is a simple program
box = 1 # Put 1 in a box
This line makes a variable box and puts the number 1 in it.
print box
We Print out what is in the variable box.
Here is the code
box = 1
print box
 What's that thing after the 1
This is a comment. It doesn't do anything for the program. It is here to help you understand what is going on.
Lets make the program a bit easier to read.




There are solutions and more on the website
http://www.philipmrussell.co.uk/raspberrypi/programming/variables.html

Python Programming


A new series of Python Programming is available on http://www.philipmrussell.co.uk/raspberrypi/programming.html
Using a simple character I am writing this for younger students of all ages, covering some simple basics.
The pages have the code to cut out and paste into your editor to save you the typing. There are a few exercises to get you gong. Each week a new set will appear.

Thursday 4 October 2012

My Version of a Lego Raspberry Pi case



I have done some work on creating a slim line Raspberry Pi case in Lego, with an optional lid.
Instructions are available in an .lxf file

Alternatively I have created a set ofbuilding instructions ( Ok lego digital designer did the hard work)

The colour is up to you, I used the Lego that I had spare to make a strong box.
I have not planned to include an output to the GERT board as yet, but this is only a minor change to the Lego case

My Raspberry Pi Finally arrives


My Raspberry Pi Finally arrives

After a bit of a wait my Raspberry Pi has finally arrived and now I can get down to some serious business with it.
The first thing I decided to do was to create a case. Others had made some attempts with Lego, even Father-in-Law so I decided that this would be my first attempt.
I found some instructions on the web of a version made a young lady. I found her version used lots of Lego if the colours I didin't have. I wanted all red.My box of spares have different colours so our second version  used larger Lego boards to make the case stronger and I lowered the height by one brick to make a more snug fit.

With the box made and connected to a 40" TV I was ready to go.

My version came from Maplins and as a kit came with a keyboard, mouse, USB wireless dongle and USB Hub. It also came with the OS pre-installed. I put on the latest version of Debian Wheezy. I set up the machine to boot into GUI automatically and downloaded geany with apt-get install geany.
I had tried in vain to get on RS Components list to order so I decided to jump ship and get a version from Maplins instead.

MAPLINS BUNDLE


The keyboard and mouse are ok - both being only cheap versions the keys are rather poor but are perfectly good enough. The power supply is suitable and saved me having to choose a suitable version and the hub is a bonus and is powered which is the sensible choice for the Raspberry Pi. The OS was pre-installed on a 4Gb Class 4 SD card. As a bonus the Raspberry Pi power supply has two USB sockets so I can charge my phone at the same time. The system came with all the cables necessary to connect to an HDMI screen.

If you want a Raspberry Pi then this seems a great choice. My Father-in-Law bought the bare system and had to buy all these parts and it cost him considerably more.

The quality of all the pars is reasonable. There is only a cheap HDMI cable, but since these work just as well as the expensive versions it is fine. There is no composite cable and this is something that my Father-in-Law would have needed.

The Bonus of getting the OS in the SD card with help many people get started, but I wanted to put on the same OS as Father-in-Law so we could be the same ( at least to start with). I also went for a wired system as the switch is a few feet away.

I tried Fedora 14 on a Class 10 16Gb card. This sort of worked. The card worked fine, and so did the OS booting into a GUI OS but the install failed me because I couldn't get the login to work

Raspberry Pi


The Raspberry Pi Foundation is a UK registered charity (Registration Number 1129409) See Website
The Raspberry Pi is for me an ideal computer. I started computing with a TRS-80 in 1980. When I bought that computer and switched it on my Mum, said what is this, ask it who was the Queen of England in 1654?

Now that machine would only do BASIC and only what I could program it to do. Before I could do anything I had to learn to program. Within a few weeks I was writing complex programs to do all sort of things.  As a computing teacher I have had many students in my class who could use a computer , but whom had no idea about what you could get a computer to do.

I feel like many that this computer is a great step forward by taking a few steps back.

I sat in ignorance of this computer until this morning when Linux Format came through my postbox.

This seems the ideal computer for education. A small Linux platform and a simple programming language. Costing $35 dollars for 256Mb RAM, 2 USB port, an Ethernet port, an ARM1176JZFS, with floating point, running at 700Mhz, and a Videocore 4 GPU. The GPU is capable of BluRay quality playback, using H.264 at 40MBits/s..The Raspberry Pi measures 85.60mm x 53.98mm x 17mm, with a little overlap for the SD card and connectors which project over the edges. It weighs 45g.

The first shipments are with out a case which makes the computer look more like a machine that can be played with, than a black box. Later versions will have a case.

The computer has no keyboard ( so one needs to be supplied ( although it would probably fit inside one, no monitor ( any HDMI monitor TV will do) or disk. The OS is on a 16Mb or 32Mb full size SD card. The power is from 4AA cells or a small power supply a a mini 5V USB  (phone style) connector.

There is already an enthusiastic following and video's up on You Tube to look at.

At the launch in a week or so's time there is another addon board, (the Gertboard) which will allow the user to use GPIO ( General Purpose Input and Output) and the ability to add any other hardware to the computer for a truly fantastic IO  experience.

Because of its small size this computer will probably find its way into all sorts of other hardware, becoming  the computer controller  for many familiar devices.

Many years ago I was involved in creating a Beowulf cluster. With the Raspberry PI this might again became a great possibility. My students learnt an enormous amount from being able to build such a cluster. By having small inexpensive computers the way to opened up to users to discover how all types of computer systems work. Instead of having to buy several servers, the same systems could be created for well less than £100.

What can the added to the system.

Would be developers are talking about adding touch screens and other devices to the Raspberry. The appeal is a machine that is very basic - that will teach the basics of computer hardware and software. The appeal of this machine might just see it appearing in every device you can think of.