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

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.

No comments: