Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
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
Python Programming with Raspberry Pi
Python Programming with Raspberry Pi

Python Programming with Raspberry Pi: Build small yet powerful robots and automation systems with Raspberry Pi Zero

Arrow left icon
Profile Icon Sai Yamanoor Profile Icon Srihari Yamanoor
Arrow right icon
€8.99 €23.99
eBook Apr 2017 312 pages 1st Edition
eBook
€8.99 €23.99
Paperback
€29.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Sai Yamanoor Profile Icon Srihari Yamanoor
Arrow right icon
€8.99 €23.99
eBook Apr 2017 312 pages 1st Edition
eBook
€8.99 €23.99
Paperback
€29.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€8.99 €23.99
Paperback
€29.99
Subscription
Free Trial
Renews at €18.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

Python Programming with Raspberry Pi

Arithmetic Operations, Loops, and Blinky Lights

In the previous chapter, we discussed printing a line of text on the screen. In this chapter, we will review arithmetic operations and variables in Python. We will also discuss strings and accepting user inputs in Python. You will learn about the Raspberry Pi's GPIO and its features and write code in Python that makes an LED blink using the Raspberry Pi's GPIO. We will also discuss a practical application of controlling the Raspberry Pi's GPIO.

In this chapter, we will cover the following topics:

  • Arithmetic operations in Python
  • Bitwise operators in Python
  • Logical operators in Python
  • Data types and variables in Python
  • Loops in Python
  • Raspberry Pi Zero's GPIO interface.

Hardware required for this chapter

In this chapter, we will be discussing examples where we will be controlling the Raspberry Pi's GPIO. We will need a breadboard, jumper wires, LEDs, and some resistors (330 or 470 Ohms) to discuss these examples.

We will also need some optional hardware that we will discuss in the last section of this chapter.

Arithmetic operations

Python enables performing all the standard arithmetic operations. Let's launch the Python interpreter and learn more:

  • Addition: Two numbers can be added using the + operand. The result is printed on the screen. Try the following example using the python interpreter:
       >>>123+456 
579
  • Subtraction: Two numbers can be added using the - operand:
       >>>456-123 
333
>>>123-456
-333
  • Multiplication: Two numbers can be multiplied as follows:
       >>>123*456 
56088
  • Division: Two numbers can be divided as follows:
       >>>456/22 
20.727272727272727
>>>456/2.0
228.0
>>>int(456/228)
2
  • Modulus operator: In Python, the modulus operator (%) returns the remainder of a division operation:
       >>>4%2 
0
&gt...

Bitwise operators in Python

In Python, it is possible to perform bit-level operations on numbers. This is especially helpful while parsing information from certain sensors. For example, Some sensors share their output at a certain frequency. When a new data point is available, a certain bit is set indicating that the data is available. Bitwise operators can be used to check whether a particular bit is set before retrieving the datapoint from the sensor.

If you are interested in a deep dive on bitwise operators, we recommend getting started at https://en.wikipedia.org/wiki/Bitwise_operation.

Consider the numbers 3 and 2 whose binary equivalents are 011 and 010, respectively. Let's take a look at different operators that perform the operation on every bit of the number:

  • The AND operator: The AND operator is used to perform the AND operation on two numbers. Try this using the Python interpreter:
       >...

Logical operators

Logical operators are used to check different conditions and execute the code accordingly. For example, detecting a button interfaced to the Raspberry Pi's GPIO being pressed and executing a specific task as a consequence. Let's discuss the basic logical operators:

  • EQUAL: The EQUAL (==) operator is used to compare if two values are equal:
       >>>3==3 
True
>>>3==2
False
  • NOT EQUAL: The NOT EQUAL (!=) operator compares two values and returns True if they are not equal:
       >>>3!=2 
True
>>>2!=2
False
  • GREATER THAN: This operator (>) returns True if one value is greater than the other value:
       >>>3>2 
True
>>>2>3
False
  • LESS THAN: This operator compares two values and returns True if one value is smaller than the other:
       >>>2<3 
...

Hardware required for this chapter


In this chapter, we will be discussing examples where we will be controlling the Raspberry Pi's GPIO. We will need a breadboard, jumper wires, LEDs, and some resistors (330 or 470 Ohms) to discuss these examples.

We will also need some optional hardware that we will discuss in the last section of this chapter.

Arithmetic operations


Python enables performing all the standard arithmetic operations. Let's launch the Python interpreter and learn more:

  • Addition: Two numbers can be added using the + operand. The result is printed on the screen. Try the following example using the python interpreter:
       >>>123+456 
       579
  • Subtraction: Two numbers can be added using the - operand:
       >>>456-123 
       333 
       >>>123-456 
       -333
  • Multiplication: Two numbers can be multiplied as follows:
       >>>123*456 
       56088
  • Division: Two numbers can be divided as follows:
       >>>456/22 
       20.727272727272727
       >>>456/2.0 
       228.0 
       >>>int(456/228) 
       2
  • Modulus operator: In Python, the modulus operator (%) returns the remainder of a division operation:
       >>>4%2 
       0 
       >>>3%2 
       1
  • The floor operator (//) is the opposite of the modulus operator. This operator returns the floor...

Bitwise operators in Python


In Python, it is possible to perform bit-level operations on numbers. This is especially helpful while parsing information from certain sensors. For example, Some sensors share their output at a certain frequency. When a new data point is available, a certain bit is set indicating that the data is available. Bitwise operators can be used to check whether a particular bit is set before retrieving the datapoint from the sensor.

If you are interested in a deep dive on bitwise operators, we recommend getting started at https://en.wikipedia.org/wiki/Bitwise_operation.

Consider the numbers 3 and 2 whose binary equivalents are 011 and 010, respectively. Let's take a look at different operators that perform the operation on every bit of the number:

  • The AND operator: The AND operator is used to perform the AND operation on two numbers. Try this using the Python interpreter:
       >>>3&2 
       2

This is equivalent to the following AND operation:

   0 1 1 &amp...

Logical operators


Logical operators are used to check different conditions and execute the code accordingly. For example, detecting a button interfaced to the Raspberry Pi's GPIO being pressed and executing a specific task as a consequence. Let's discuss the basic logical operators:

  • EQUAL: The EQUAL (==) operator is used to compare if two values are equal:
       >>>3==3 
       True 
       >>>3==2 
       False
  • NOT EQUAL: The NOT EQUAL (!=) operator compares two values and returns True if they are not equal:
       >>>3!=2 
       True 
       >>>2!=2 
       False
  • GREATER THAN: This operator (>) returns True if one value is greater than the other value:
       >>>3>2 
       True 
       >>>2>3 
       False
  • LESS THAN: This operator compares two values and returns True if one value is smaller than the other:
       >>>2<3 
       True 
       >>>3<2 
       False
  • GREATER THAN OR EQUAL TO (>=): This operator...

Data types and variables in Python


In Python, variables are used to store a result or a value in the computer's memory during the execution of a program. Variables enable easy access to a specific location on the computer's memory and enables writing user-readable code.

For example, let's consider a scenario where a person wants a new ID card from an office or a university. The person would be asked to fill out an application form with relevant information, including their name, department, and emergency contact information. The form would have the requisite fields. This would enable the office manager to refer to the form while creating a new ID card.

Similarly, variables simplify code development by providing means to store information in the computer's memory. It would be very difficult to write code if one had to write code keeping the storage memory map in mind. For example, it is easier to use the variable called name rather than a specific memory address like 0x3745092.

There are different...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • This is the first book on the market that teaches Python programming with Raspberry Pi Zero
  • Develop exciting applications such as a mobile robot and home automation controller using Python
  • This step-by-step guide helps you make the most out of Raspberry Pi Zero using Python programming

Description

Raspberry Pi Zero is a super-small and super-affordable product from Raspberry Pi that is packed with a plethora of features and has grabbed the notice of programmers, especially those who use Python. This step-by-step guide will get you developing practical applications in Python using a Raspberry Pi Zero. It will become a valuable resource as you learn the essential details of interfacing sensors and actuators to a Raspberry Pi, as well as acquiring and displaying data. You will get started by writing a Python program that blinks an LED at 1-second intervals. Then you will learn to write simple logic to execute tasks based upon sensor data (for example, to control a motor) and retrieve data from the web (such as to check e-mails to provide a visual alert). Finally, you will learn to build a home automation system with Python where different appliances are controlled using the Raspberry Pi. The examples discussed in each chapter of this book culminate in a project that help improve the quality of people’s lives.

Who is this book for?

This book is aimed at hobbyists and programmers who want to learn Python programming and develop applications using the Pi Zero. They should have basic familiarity with electronics.

What you will learn

  • • Configure Raspberry Pi using Python
  • • Control loops to blink an LED using simple arithmetic operations
  • • Understand how interface sensors, actuators, and LED displays work
  • • Get to grips with every aspect of Python programming using practical examples
  • • Explore machine vision, data visualization, and scientific computations
  • • Build a mobile robot using the Raspberry Pi as the controller
  • • Build a voice-activated home automation controller

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Apr 28, 2017
Length: 312 pages
Edition : 1st
Language : English
ISBN-13 : 9781786469151
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 : Apr 28, 2017
Length: 312 pages
Edition : 1st
Language : English
ISBN-13 : 9781786469151
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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 95.97
Python Programming with Raspberry Pi
€29.99
Raspberry Pi By Example
€32.99
Raspberry Pi Zero Cookbook
€32.99
Total 95.97 Stars icon
Banner background image

Table of Contents

11 Chapters
Getting Started with Python and the Raspberry Pi Zero Chevron down icon Chevron up icon
Arithmetic Operations, Loops, and Blinky Lights Chevron down icon Chevron up icon
Conditional Statements, Functions, and Lists Chevron down icon Chevron up icon
Communication Interfaces Chevron down icon Chevron up icon
Data Types and Object-Oriented Programming in Python Chevron down icon Chevron up icon
File I/O and Python Utilities Chevron down icon Chevron up icon
Requests and Web Frameworks Chevron down icon Chevron up icon
Awesome Things You Could Develop Using Python Chevron down icon Chevron up icon
Lets Build a Robot! Chevron down icon Chevron up icon
Home Automation Using the Raspberry Pi Zero Chevron down icon Chevron up icon
Tips and Tricks Chevron down icon Chevron up icon
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.