/* jquery */ /* jquery accordion style*/ /* jquery init */

29 August 2012

Learn Python on the Raspberry Pi: Turtle Fun

This week in my Learn Python on the Raspberry Pi series it's all about having fun with Turtle Graphics.

Using Python's 'turtle' module all it takes is a few lines of code to create engaging, colourful graphical patterns and designs.

If you don't have a Raspberry Pi you can still follow the series with these emulation instructions.

Here are a few extracts from Part 5:

Logo is an interesting computer language.

While its origins go back to the 1960s (see boxouts), in recent times Logo has been associated with something called 'turtle graphics'. The idea is to draw pictures and generate patterns by issuing commands to a screen-based 'turtle'.

Logo has a lot going for it. It's a language based on simple commands, using words that even the youngest primary school pupil will understand. Words such as 'forward', 'backward', 'left' and 'right'.

So, how do we access all this fun with Python? Well, it's all down to a module called 'turtle', which has a collection of Logo-like commands, implemented as functions - plus some useful window and event management features.

I hope you enjoyed this introduction to turtle graphics. Maybe you've been surprised at how easy it is to create quite complex patterns. There's so many ways to experiment, so have some fun creating your own little turtle programs. Visit the turtle module reference pages to discover more.

Next time I'll continue the visual theme by introducing game programming with the PyGame module.

Visit my Raspberry Pi page for news, reviews, advice and tutorials.

22 August 2012

Learn Python on the Raspberry Pi: A GUI World

Part four of my six part Learn Python on the Raspberry Pi series introduces the Tkinter module, which allows us to code Graphical User Interface (GUI) apps, complete with windows and widgets.

The article shows how to create a file search app with search criteria entry fields, search button and scrolling results list.

Unfortunately, there's a missing boxout containing instructions on installing the Tkinter module. It's a simple enough task, just type the following line into a terminal window:
sudo apt-get install python-tk
If you don't have a Raspberry Pi you can still follow the series with these emulation instructions.

Here are a few extracts from Part 4:

Every GUI application has a very similar structure. Once we understand this structure we'll be able to create any number of GUI programs, from the very simple to the highly complex.

Just as we saw last time, we can use functions to divide the program into easy-to-read, easy-to-maintain code blocks. A typical GUI program has four main sections:
  • window and widget creation
  • window initialisation
  • the main window loop
  • event handlers

A GUI program must be able to determine which key was pressed. Not just the alphanumeric keys, but also shift keys, control keys, alt keys, function keys, cursor keys, the escape key and special keys like the Windows key. As for the mouse, a program needs to determine the current coordinates of the mouse pointer, whether a mouse button has been clicked and if the scroll wheel has moved.

How do we do this? In a GUI program it's done with something called 'callbacks'.

As you can see a GUI program requires quite a bit more code than a simple terminal-based one. However, the flexibility offered by a widget-based user interface is certainly worth the effort. And the possibilities for refinement are almost endless. For example, you could change a widget's size or colour, add new widgets, or create a completely different layout.

Next time I'll show you how to have loads of fun with turtle graphics.

Visit my Raspberry Pi page for news, reviews, advice and tutorials.

15 August 2012

Learn Python on the Raspberry Pi: Building Blocks

In part three of my six part Learn Python on the Raspberry Pi series you'll discover how to defined your own functions, creating a number guessing game in the process.

Functions enable you to develop modular, well structured programs. Such programs are easier to edit, extend and debug.

If you don't have a Raspberry Pi you can still follow the series with these emulation instructions.

Here are a few extracts from Part 3:

So far all our programs have had a simple flow, from top to bottom. While this is fine for small examples, as soon as programs become longer and more complex, things tend to become more difficult.

...

A far better approach is to break the solution down into a series of identifiable steps. Each step can be designed and coded in isolation.

As an analogy think of a Lego set. You have a wide selection of bricks in all kinds of sizes, shapes and colours. These bricks enable you to build models quite quickly, just by combining the right type of bricks in the right sequence. If you change you mind, or have a new idea, the type of bricks used or their assembly sequence can be quickly changed.

All functions will have a pair of brackets, or parenthesis, after the name. Optionally there may be one or more parameters declared inside these brackets. Here there is just one called 'radius', which will contain a numeric radius value. Finally, there's a colon ':' at the end of the definition. As we saw last time with Python loops, a colon signifies that all the indented code below will belong to this function, and will be executed when it's called.

Creating our own functions is a major step in becoming a more accomplished programmer. Study just about any program example on the web or in a book and you'll see functions everywhere.

Next time we'll be getting much more visual. We're going to create graphical user interface (GUI) programs using another powerful Python module, Tkinter. Until then have fun with the guessing game.

Visit my Raspberry Pi page for news, reviews, advice and tutorials.

9 August 2012

Learn Python on the Raspberry Pi: Going Loopy

Part two of my six part Learn Python on the Raspberry Pi series is out today. This week you'll learn about loops and conditional statements, using them to create a simple timer and a file search program.

Python loops and conditional statements are important programming elements for anyone thinking of entering the Raspberry Pi Coding Competition.

If you don't have a Raspberry Pi you can still follow the series with these emulation instructions.

Here are a few extracts from Part 2:

Loops are a very important concept in any programming language. Almost every program will contain at least one loop. They are especially important in graphical user interface (GUI) and game programming.

There are number of looping scenarios. One is to repeat the code inside a loop a fixed number of times. Another is to loop until a specified loop condition is satisfied. Alternatively, a loop exit command can be issued depending on a particular program state. In this and subsequent articles we'll meet all three of these scenarios.

Sometimes we need a loop that executes a fixed number times. The 'for' keyword is the classic way to create such a loop. You'll find this keyword in many other programming languages.

Let's look at a typical Python 'for' loop statement. Suppose we wanted to print out each character of a string. The code would look like this:
message = "Hello"
for char in message:
   print char

That concludes our first look at loops. We've seen how they enable us to create small yet extremely useful programs. Loops are usually associated with conditional statements - another key programming technique. We'll be using loop and conditional statements extensively throughout this series.

Next time we'll be defining our own functions, the raw building blocks of any well structured program.

Visit my Raspberry Pi page for news, reviews, advice and tutorials.

2 August 2012

Learn Python on the Raspberry Pi

My new Learn Python on the Raspberry Pi tutorial series started today in issue 1220 of the weekly Micro Mart magazine.

Each week for six weeks there'll be an interactive four page article covering a different aspect of Python coding.

It starts with the basics then moves on to more advanced topics such as GUI with Tkinter, Turtle graphics and animation using the PyGame module.

See my Gentle Introduction to Python Coding on the Raspberry Pi.

Visit my Raspberry Pi page for Pi news, reviews, advice and tutorials.