Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)
Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)

Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications): Learn to design and implement reliable Python applications on the Raspberry Pi using a range of external libraries, the Raspberry Pis GPIO port, and the camera module

eBook
$24.99 $35.99
Paperback
$43.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)

Chapter 2. Understanding Control Flow and Data Types

Now that we are able to run Python code, we will take a look at some of the ways in which we can store data and control the flow of execution through the Python code.

In this chapter, we will cover:

  • The basics of data types in Python
  • Using the math module for operations on numerical types
  • Using the tring module for string manipulation

As mentioned earlier, all the code used in this chapter is written for Python 2.7, and is able to be executed on any Python 2.7 interpreter.

Data in Python

Before jumping to the various data types that are available in Python, it is worth noting that Python is a strong dynamically typed programming language, which means both that:

  • Once a variable (a unit of stored data in a program) has been given a value, its type is set and will not change until the variable is assigned a value of a different type (strong)
  • A variable can hold a value of any type as the type is given by the value, not the variable itself (dynamic)

This is best explained with an example that can be executed from the interactive console, assuming we have a variable representing a string:

flan = "flan"

We can query the value held by the variable and the type using the following code (note that this will only work on an interactive console as return values are automatically printed to the terminal):

flan
type(flan)

As the following output shows, the variable is of type str which represents a string in Python:

Data in Python

We can now reassign the variable to a new numerical...

Control flow operators

If you have ever used any other programming language, you are probably familiar with the standard control flow operators if, for, and while, which are all present in Python and operate in a fairly similar way.

if and while can be used in a similar way as they would usually be with any Boolean expression, as shown in the following example. Since the interactive terminal is not fantastic at handling indentation, we will just run these examples as standard Python scripts.

import random
if random.randint(1, 100) % 2 == 1:
    print "Win!"
else:
    print "Lose!"
times_lost = 0
while random.randint(1, 100) % 2 == 0:
    times_lost += 1
print "Win! (after %d losses)" % (times_lost)

As the following output shows, the execution in the first example is controlled solely by the random number generated in the if statement. In the second, the random number generation determines the amount of times a section of code will be executed whilst the overall...

Using functions

The final topic we will cover in this chapter is that of separating code into functions, and how variables can be passed to and returned from them.

We will first start with a simple function that takes no arguments and does not return a value. The following example shows the basic syntax for a function in Python:

def say_hello():
    print "Hello!"
say_hello()

This example simply prints Hello! to the terminal, as shown in the following output:

Using functions

In the next example, we will introduce an argument which will be used in the string. As the example shows, all that is needed is the argument name unlike the other programming languages, which require a type.

def say_hello(person_name):
    print "Hello, %s!" % (person_name)
say_hello("Sanae")

Tip

If you have a function that only operates on a specific type, you can still enforce arguments to take a specific type using the isinstance() function.

Here the string Hello, Sanae! is printed as shown in the following...

Summary

In this chapter, we looked at the most common data types in Python, took an overview of their operations, and understood how control flow can be controlled within a Python script.

In the next chapter, we will look further into the container data types that can be used to hold multiple pieces of data. We will also take a look at IO and see how our Python programs can interact with the other data and devices on the system.

Left arrow icon Right arrow icon

Description

The Raspberry Pi is one of the smallest and most affordable single board computers that has taken over the world of hobby electronics and programming, and the Python programming language makes this the perfect platform to start coding with. The book will start with a brief introduction to Raspberry Pi and Python. We will direct you to the official documentation that helps you set up your Raspberry Pi with the necessary equipment such as the monitor, keyboard, mouse, power supply, and so on. It will then dive right into the basics of Python programming. Later, it will focus on other Python tasks, for instance, interfacing with hardware, GUI programming, and more. Once you get well versed with the basic programming, the book will then teach you to develop Python/Raspberry Pi applications. By the end of this book, you will be able to develop Raspberry Pi applications with Python and will have good understanding of Python programming for Raspberry Pi.

Who is this book for?

This book is designed for those who are unfamiliar with the art of Python development and want to get to know their way round the language and the many additional libraries that allow you to get a full application up and running in no time.

What you will learn

  • Fundamentals of Python applications
  • Designing applications for multithreading
  • Interacting with electronics and physical devices
  • Debugging applications when they go wrong
  • Packaging and installing Python modules
  • User interface design using Qt
  • Building easy to use commandline interfaces
  • Connecting applications to the Internet

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Sep 29, 2015
Length: 200 pages
Edition : 1st
Language : English
ISBN-13 : 9781785285837
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Sep 29, 2015
Length: 200 pages
Edition : 1st
Language : English
ISBN-13 : 9781785285837
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 128.97
Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)
$43.99
Learning Python
$45.99
Building a Home Security System with Raspberry Pi
$38.99
Total $ 128.97 Stars icon

Table of Contents

12 Chapters
1. Your First Steps with Python on the Pi Chevron down icon Chevron up icon
2. Understanding Control Flow and Data Types Chevron down icon Chevron up icon
3. Working with Data Structures and I/O Chevron down icon Chevron up icon
4. Understanding Object-oriented Programming and Threading Chevron down icon Chevron up icon
5. Packaging Code with setuptools Chevron down icon Chevron up icon
6. Accessing the GPIO Pins Chevron down icon Chevron up icon
7. Using the Camera Module Chevron down icon Chevron up icon
8. Extracting Data from the Internet Chevron down icon Chevron up icon
9. Creating Command-line Interfaces Chevron down icon Chevron up icon
10. Debugging Applications with PDB and Log Files Chevron down icon Chevron up icon
11. Designing Your GUI with Qt Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
(1 Ratings)
5 star 0%
4 star 0%
3 star 100%
2 star 0%
1 star 0%
Felix Dec 11, 2016
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
This is for python 2.7 I was looking for a book on python 3.2
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.