First steps with Python programming

Lead Image © Eric Isselee, 123RF.com

Snakes Alive!

Take your first steps with Python programming and discover the turtle module for creating graphics.

Don't be afraid of the snake: Python might look intimidating if you haven't done much programming before, but it's friendly. If you're using a text-based programming language for the first time, Python is a great choice. Raspberry Pi Foundation co-founder Eben Upton has called Python his favorite language for learning and development, and it's a natural next step after Scratch.

If you're an experienced programmer, you'll find that Python is powerful and helps you be productive. It's not just an educational language: Google hires Python programmers, and the language was used by Industrial Light and Magic in producing Star Wars: Attack of the Clones. With all those spaceships and light sabre fights, few people noticed the Python in the background.

Two different versions of Python are in popular use today, and the Raspberry Pi supports both of them. Python 3 is the latest version of Python, and it's the one that's being actively updated. The only reason not to use it is that some software you might want to use doesn't yet support Python 3. In those circumstances, retreat to Python 2.7. Most of the time you won't have to worry about which version you're using, but programs for Python 2.7 won't always work in Python 3 and vice versa.

Python is an important language for the Raspberry Pi environment, and it comes pre-installed in many Raspberry Pi Linux distributions. If you can't find Python on your system, install it through your distro's package management system.

If you are using the Raspbian Wheezy distribution, you've probably noticed the IDLE icons lounging around on your desktop (Figure 1). IDLE is an Integrated Development Environment (IDE) used for writing Python programs. An IDE is a user interface that formats the code and provides easy access to helpful developer tools. IDLE offers an easy way to create and run Python programs. Use IDLE for Python 2.7 and IDLE 3 for Python 3. To start, double-click the IDLE 3 icon.

Figure 1: The IDLE and IDLE 3 icons are your gateways to Python programming.

Using the Python Shell

When you open IDLE, you start in the Python Shell. The >>> prompt means that Python is ready for you to give it an instruction, which it will carry out straightaway. To begin, try typing:

print("Hello!")

The print command has nothing to do with printers: It's used in Python to write text to the screen, so when you press Return, the computer will write a friendly Hello! before returning you to the prompt. You can change what goes between the quote marks to display any message you like. With commands like this, always make sure you have matching opening and closing brackets, and opening and closing quotes.

Python is case sensitive, which means you can't use uppercase in place of lowercase or the other way around. If you try entering the command with a capital P, you'll get an error message, which is shown in red to hammer home the point (Figure 2). The words print and Print might look similar to us, but to Python, they're completely different.

Figure 2: Python gets on your case if you use capitals by mistake.

Using this simple command, I can show you one of the differences between Python 2 and Python 3. In Python 2 (IDLE), you can use the command above or you can use:

print "hello"

That doesn't work in Python 3 (IDLE 3), though, because Python 3 needs those parentheses. From this simple example, you can see that it might be fiddly to get a program from Python 2 to work in Python 3, depending on the program.

Doing the Sums

I used to know a joke about a snake who borrowed money, and when he came to repay it, he hadn't worked out the amount plus interest correctly. It was because he was a poor adder.

Luckily, I can't remember that joke any more (ahem!), but you'll be pleased to know that Python is an excellent calculator. You can just type sums into it, and it'll tell you the answers. You use the plus (+) and dash (-, "minus") symbols for addition and subtraction, the slash (/) for division, and the asterisk (*) for multiplication. For example, try:

9 + 9
7 - 2
8 * 7
9 / 2

These sums are trivial (except perhaps 8 * 7: Many people get hazy around that part of the multiplication table), but they're good for testing.

These operations work in the shell, because when you enter something that isn't a command (or something Python can do), it evaluates what you give it. That means it asks itself, What is plus whatever you entered, and it gives you the answer.

You'll notice that I've used a space on either side of the math symbols here. I could have used several spaces or none at all, and it would work equally well. The difference, though, would be readability.

One reason Python is a great language is that it's designed to be easy to read. The language has strict rules about how you indent some lines to make your programs easy to skim, as you'll see shortly. However, a strong community around Python also evangelizes readability and has its own conventions as to what constitutes true "Pythonic" code. One of the guidelines is that you use one space on each side of a math symbol.

If you're collecting Python 2 quirks, I have another one for you: Ask Python 2 for 9 / 2, and it gives you the answer 4 (and not 4.5), because it discards the decimal portion (Figure 3). To force Python 2 to give you a more precise answer, you would enter numbers with decimal points, like this:

9.0 / 2.0

You don't need to get hung up on the differences between the versions of Python, but if you can't get an example you've found online to work, it might well be because of oddities like this.

Figure 3: Strangely, in Python 2, 9 divided by 2 equals 4.

Buy this article as PDF

Express-Checkout as PDF

Pages: 5

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Pi with Sugar and turtles as a learning tool

    Despite its popularity elsewhere, the Raspberry Pi is first and foremost an educational tool designed with primary and secondary students in mind. We look at how teachers can make the Pi fulfill this role with the help of the Sugar Educational Desktop and turtle graphics.

  • Python Primer

    Your Raspberry Pi comes with Python, a flexible open source programming and scripting language. Download this PDF for a quick tutorial on getting started with Python.

  • Using Scratch to explore turtle geometry

    We show you how to use some simple Scratch concepts to create shapes and patterns.

  • How to get your Pi to go

    Pi2Go-Lite is a new fully integrated robot kit that makes it easy to get into robotics with the Raspberry Pi. We speak to its creator about how it works, then build our own autonomous line-following robot.

  • Math, Music, and Cat Toys

    Welcome to Raspberry Pi Geek – the first and only print magazine dedicated to the amazing Raspberry Pi mini-PC and the open hardware revolution. We ring in the new and old in this issue. (Actually, nothing is really very old with the Raspberry Pi, but we follow up on some previous themes, including a report on how it went for the wind-turbine-powered Raspberry Pi we described last time.)