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.