Raspberry pi projects 2015

Page 167

Raspberry Pi-thon If you’re following this tutorial on a Raspberry Pi (1 or 2) and using the Raspbian distribution, there’s good news: you don’t need to perform any additional steps to get Python working. In fact, you get the pleasure of a ready-to-roll development environment called IDLE. You might find this more fun to work with than the command-line interpreter we use throughout this tutorial, and such things certainly become advantageous when working with larger projects.

IDLE enables you to write and edit multiple lines of code as well as providing a REPL (Read Evaluate Print Loop) for immediate feedback. To see this in action, select New Window, hammer out a few lines of code and then run them by pressing F5 or selecting Run Module from the Run menu. Newer versions of Raspbian come with both versions 2 and 3 of Python, together with two separate IDLE shortcuts for starting each

Now x has been rounded down to the nearest whole number. We could convert it to a string as well, using the str type. Another important construct in Python is the list. A list is defined by using square brackets and may contain any and all manner of data – even including other lists. Here’s an arbitrary example: >>> firstList = [‘aleph’, ‘beth’, 3.14159265] Items in our list are zero-indexed, so we access the first item, for example, with firstList[0] and the third one with firstList[2]. Some languages are one-indexed, which some people find more intuitive, but Python is not one of them. Strings are similar to lists (in that they are a sequence of characters) and many list methods can be applied to them. Thus we can get the first character of name with name[0]. We can also get the last character using name[ -1] rather than having to go through the rigmarole of finding the string’s length first, for which you would have to use the len() function. Strings are immutable – which means that once they’ve been defined, they cannot be changed – but this is fine because they can be redefined, copied or reconstructed depending on our purposes. A particularly useful construct is list (or string) slicing. We can get the first two elements of our list by using firstList[:2], or all but the first item with firstList[1:]. These are short forms of the more general slicing syntax [x:y], which returns the substring starting at position x and ending at position y. Omitting x defaults to the beginning of the list, and omitting y defaults to the end. One can even specify a third parameter, which defines the so-called step of the slice. For example, the slice [x:y:2] gives you every second item starting at position x and ending at position y, or wherever the previous step lands. Again, this can be abbreviated – if you just want every second item from the whole list – to [::2]. Negative step sizes are also permitted, in which case our starting point x is greater than y, and omitting x defaults to the end of the list. Thus slicing our list above like so returns the reverse of that list: >>> firstList[::-1] Besides defining lists by explicitly specifying their members, there are a number of constructs available for defining lists that have some sort of pattern to them. For example, we can make the list [0,1,2,3,4,5] by using list(range(6)). In Python 3, the range() function returns an iterator (it doesn’t contain any list items but knows how to generate them when given an index), so there is an additional function call to turn it into a list proper. Again, there is potential for 0-based grievances, because 6 is not in that list, and again it’s something we simply have to get used to. Similar to slicing, the range() function can take optional

version. Previous Raspbian versions came with only the former, but if you’ve done an apt-get dist-upgrade recently, you’ll probably find that the new version has been pulled in. But if you don’t have Python 3 installed (in other words running python3 returns command not found, then you can get it by opening LXTerminal and issuing the following commands: $ sudo apt-get update $ sudo apt-get install python3

arguments specifying the start value and step size of the range. This means that we can get all the odd numbers between 5 and 19 (including the former but excluding the latter) by using: >>> twostep = list(range(5,19,2)) Negative step sizes are also supported, so we could count down from 10 to 1 with range(10,0,-1). Items can be inserted into lists – or removed from them – by using various methods. Methods in Python are properties of an object, and are called by suffixing said object with a dot, followed by the method name. So we could add 19 to the end of our previous list by using the append() method, such as: >>> twostep.append(19) We could also insert the value 3 at the beginning of the list with twostep.insert(0,3). There are many other list methods, which you can peruse using help(list). Help is available for any Python keyword and many other topics, and this can be a very useful resource when you get stuck. When you start working with multiple lists, you might witness some rather strange behaviour, such as the diagram

The IDLE environment is ideal [ha, ha – Ed] for developing larger Python projects. It’s included in the Raspbian distribution, too.

WorldMags.net

Raspberry Pi Projects | 167

Coding

WorldMags.net


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.