RASPBERRY PI: Step-By-Step Guide To Mastering Raspberry PI 3 Hardware And Software (Raspberry Pi 3, Raspberry Pi Programming, Python Programming, C Programming) by Richard Ray
Author:Richard Ray [Ray, Richard]
Language: eng
Format: epub, azw3
Publisher: UNKNOWN
Published: 2017-09-05T21:00:00+00:00
Chapter 8
Python Programming for Raspberry Pi
What you will learn in this chapter
Start programming with python
Using python for automation
Drive the hardware with python
What you will need for this chapter
Raspberry Pi Board
Resistors, LEDs
Introduction to Python Programming
In this part of this chapter you will learn how to use python to develop some basic encryption, user input, and creating graphical user interface
Let’s start with the hello world example as in any programming language
Create file named hello.py using nano text editor
Nano –c hello.py
Within the file write the following code
#/usr/bin/python3
#hello.py
Print (“Hello World”)
After writing the code, save and exit. You can run the file using the following command
Python3 hello.py
You should know more about strings if you want to start with python A string is a sequence of characters stored together as a value. We will write code to get the user’s input, using string manipulation to switch the letters and then print encrypted message of the user input. You can maybe use text editors that can be directly on your Raspberry Pi or via VNC or SSH. There are many text editors you can choose from
● nano: You can work with this editor from the terminal
● IDLE3: This editor includes the syntax highlighting, context help, but this program requires x-windwos or x11 to run remotely we will use python3, so make sure that you run IDL3 not IDLE
●Geany: this editor is an Integrated Development Environment (IDE) that supports many programming languages, syntax highlighting, auto completion and very easy code navigation. This is a rich editor , but not for beginners and It will be slow on the Raspberry Pi. If you want to install Geany, write the following command
Sudo apt-get install Geany
To make sure that the Geany editor use python3:
Click on the Execute button to run the code
, you will need to change the build commands. Load the file
And click build and set build commands and then change python to python3
Let’s cerate the program
#/usr/bin/python3
#ecryptionprogram.py
#takes the input and encrypt it
def encrpytText(input_text,key);
output=””
for letter in input_text:
#Ascii Uppercase 65-90 lowercase 97 -122
Ascii_val = ord(letter)
#now write the following code to exclude non characters from encryption
If(ord(“A”) > Ascii_val) or (Ascii_val > ord(“Z”)):
Output+=letter
Else:
#write this code to apply the encryption key
Key_val = Ascii_val + key
#make sure that we use A-Z regardless of key
If not((or(“A”)) < key_val < or(“Z”)):
Key_val = ord(“A”) + (key_val-ord(“A”))\
%(ord(“Z”) –ord(“A”)+1)
#add the encrypted letter to the output
Output+=str(chr(key_val))
Return output
#Test
Def main() Print (“please enter any text to encrypt”)
#get user input
Try:
Us_input = input();
Sc_result = ecryptText(us_input, 10)
Print (“output: ”, sc_result)
Print(“to un-scramble , pls press enter”)
Input()
Un_result = ecryptText(Sc_result, -10)
Print (“output: ” + un_result)
Except UnicodeDecodeError:
Print (“this program supports ASCII characters only”)
Main()
#end of the program The preceding code implements a basic method to encode the text using character substitution called the Caesar Cipher. Named after the emperor Julius Caesar, who used this method to send his secret orders to the army
We have defined two functions, encryptText() and main(). Also when the code is running, the main function contains the user’s input using input() command. The result is stored as a string int the us_ input
Download
RASPBERRY PI: Step-By-Step Guide To Mastering Raspberry PI 3 Hardware And Software (Raspberry Pi 3, Raspberry Pi Programming, Python Programming, C Programming) by Richard Ray.azw3
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Linux Device Driver Development Cookbook by Rodolfo Giometti(3932)
Embedded Programming with Modern C++ Cookbook by Igor Viarheichyk(3588)
Implementing Cellular IoT Solutions for Digital Transformation by Dennis McCain(3525)
Embedded Linux Development Using Yocto Project - Third Edition by Otavio Salvador & Daiane Angolini(3355)
TinyML Cookbook by Gian Marco Iodice(3293)
Simplifying 3D Printing with OpenSCAD by Colin Dow(2839)
TinyML Cookbook by Gian Marco Iodice & Ronan Naughton(2601)
Fusion 360 for Makers by Lydia Sloan Cline(2219)
Networking A Beginner's Guide by Bruce Hallberg(2209)
Hands-On Linux for Architects by Denis Salamanca(2051)
But How Do It Know? by J. Clark Scott(2030)
Computers For Seniors For Dummies by Nancy C. Muir(1995)
Raspberry Pi and MQTT Essentials by Dhairya Parikh(1950)
Arduino Project Handbook, Volume 2: 25 Simple Electronics Projects for Beginners by Geddes Mark(1947)
9781803246888-ENHANCING DEEP LEARNING WITH BAYESIAN INFERENCE by Unknown(1898)
Hack and HHVM by Owen Yamauchi(1884)
31 Days Before Your CompTIA A+ Exams (Shanette Luellen's Library) by Benjamin Patrick Conry(1860)
MicroPython Projects by Jacob Beningo(1728)
Hands-On Internet of Things with MQTT by Tim Pulver(1704)
