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

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.

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.

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.

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.