Skip to Main Content
Raspberry Pi Cookbook, 2nd Edition
book

Raspberry Pi Cookbook, 2nd Edition

by Simon Monk
June 2016
Beginner to intermediate content levelBeginner to intermediate
508 pages
9h 40m
English
O'Reilly Media, Inc.
Content preview from Raspberry Pi Cookbook, 2nd Edition

Chapter 5. Python Basics

5.0 Introduction

Although there are many languages that can be used to program the Raspberry Pi, Python is the most popular. In fact, the Pi in Raspberry Pi is inspired by the word Python.

In this chapter, you will find a host of recipes to help you get programming with Raspberry Pi.

5.1 Deciding Between Python 2 and Python 3

Problem

You need to use Python but are unsure about which version to use.

Solution

Use both. Use Python 3 until you face a problem that is best solved by reverting to version 2.

Discussion

Although the latest version of Python is Python 3 and has been for years, you will find that a lot of people stick to Python 2. In fact, in the Raspbian distribution, both versions are supplied and version 2 is just called Python, whereas version 3 is called Python 3. You even run Python 3 by using the command python3. The examples in this book are written for Python 3 unless otherwise stated. Most will run on both Python 2 and Python 3 without modification.

This reluctance on the part of the Python community to ditch the old Python 2 is largely because Python 3 introduced some changes that broke compatibility with version 2. This means that some of the huge body of third-party libraries that have been developed for Python 2 won’t work under Python 3.

My strategy is to write in Python 3 wherever possible, and revert to Python 2 when I have to because of compatibility problems.

See Also

For a good summary of the Python 2 versus Python 3 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Raspberry Pi Cookbook, 3rd Edition

Raspberry Pi Cookbook, 3rd Edition

Simon Monk

Publisher Resources

ISBN: 9781491939093Errata Page