Preparing for Python development on the Raspberry Pi

May 4, 2013 | In: Python, Raspberry Pi, Source Control, WNYLUG

This post will be the first of many that will be following my adventures with developing code in Python on the Raspberry Pi. The intent of this series is to provide myself with motivation for making progress with my Raspberry Pi projects but also to provide material for a series of WNYLUG meetings. During this series I hope to gain more knowledge of Python development and share that knowledge with the WNYLUG members and anyone else that cares. Other topics will get discussed along the way such as Mercurial usage, Emacs usage, basic circuit design and whatever else comes up during the journey.

Let’s begin!

Part 001 – Hello World and source control

To start with I needed to refresh my Python skills with some basic tutorials but before any coding could be done I needed to setup source control tools and an editor. There is nothing worse than manually versioning code, well losing your code is worse so source control!

My preferred source control tool is Mercurial. A quick apt-get installed the tools for me.

$ sudo apt-get install mercurial

And there was no way that I would be doing extensive coding in nano so I needed to install Emacs. (Yes, I am an Emacs guy, sorry, deal with it, I love it, blame UB, thanks UB!)

$ sudo apt-get install emacs

Some quick Mercurial setup, I created the Mercurial configuration file in my home directory and added my username.

$ emacs ~/.hgrc

Added the following text to the file.

[ui]
username = Neal Chapman <my email address>

And then saved the file and exited Emacs by pressing ctrl+x ctrl+c and pressing enter when prompted to save my file.

I then created a series of folders in my home directory to hold my code and the repository that would be storing my code history.

$ mkdir ~/projects

$ mkdir ~/projects/python_playground

I then initialized a repository in what would be the root folder for my code files.

$ cd ~/projects/python_playground

$ hg init

And finally, it was time for some Hello World action!

I prefer to write Python in script files and execute the script files as opposed to just playing in the command line interpreter. So I created my Hello World script file with Emacs.

$ emacs hello_world.py

In Emacs I entered a very simple single line of code.

[python]
print("Hello World!")
[/python]

I then saved my file by pressing ctrl+x ctrl+s and then closed Emacs by pressing ctrl+x ctrl+c (this will be my last mention of how to save files and close Emacs).

And the moment of truth, execution. To execute the my newly created Python script I ran the following command.

$ python3 hello_world.py

And the output was amazingly.

Hello World!

Simply put, the print function writes to standard output so this Python script is writing the string “Hello World!” to standard output. Working code is a beautiful thing but I couldn’t bask in my glory just yet, I had to commit my newly created file to source control. To commit my newly created, untracked file to the repository I first ran the add command.

$ hg add

I then committed the file with a commit message.

$ hg commit -m “Committed hello world file.”

OK, the code is safe in the repository until the next tutorial…

Part 002 – Tutorials Abound

So back into the tutorials and basics needed to be covered. Math, control flow, data structures, IO, exception handling and what not.

I started with some basic math. I created a file named basic_math.py which contained the following lines.

[python]
print(2)
print("+")
print(2)
print("is")
print(2 + 2)
[/python]

I then executed the file which generated the following output.

2
+
2
is
4

After adding that file to the Mercurial source control repository I made a small change to the file changing the addition to division.

[python]
print(2)
print("/")
print(2)
print("is")
print(2 / 2)
[/python]

Which when executed gave me an output of.

2
/
2
is
1

So we can see that the print command can take more than just strings, it cause take expressions and evaluate the expression. To consolidate the code in preparation for several math samples I changed my script to this.

[python]
print("math sample – addition")
print("2 + 2 = " + str(2 + 2))
print("math sample – division")
print("2 / 2 = " + str(2 / 2))
[/python]

Which when executed gave me.

math sample – addition
2 + 2 = 4
math sample – division
2 / 2 = 1

In this code the str function evaluates the passed in expression and attempts to convert it to a string. The static string and the string returned from str are then concatenated together and printed to standard output.

The next set of samples used more advanced math functions that required the import of the math module. Changing my math script to the following.

[python]
import math
print("math sample – addition")
print("2 + 2 = " + str(2 + 2))
print("math sample – division")
print("2 / 2 = " + str(2 / 2))
print("math sample – power")
print("2^2 = " + str(2**2))
print("2^3 = " + str(2**3))
print("2^4 = " + str(2**4))
print("math sample – ceiling and floor")
print("the ceiling of 2.375 = " + str(math.ceil(2.375)))
print("the floor of 2.375 = " + str(math.floor(2.375)))
[/python]

Produced the following output.

math sample – addition
2 + 2 = 4
math sample – division
2 / 2 = 1
math sample – power
2^2 = 4
2^3 = 8
2^4 = 16
math sample – ceiling and floor
the ceiling of 2.375 = 3.0
the floor of 2.375 = 2.0

Enough math, onto control flow. Control flow is code that allows your code to make decisions. The following script asks the user for input, converts that input to an integer and saves it in a variable named x. It then evaluations that value and prints a string based on the value.

[python]
x = int(input("Enter a number between 0 and 100: "))
if x < 0:
print("You failed and entered a number less 0, smooth move.")
elif x > 100:
print("This number is greater than 100, what are you doing?")
else:
print("You can follow directions! You entered: " + str(x))
[/python]

Here is one possible output path based on the user inputting a value of 34.

$ python3 control_flow_basics.py
Enter a number between 0 and 100: 34
You can follow directions! You entered: 34

I then added another form of control flow, looping using while. The following code will ask the user for a number and return the square root of that number until the user presses “q” to quit.

[python]
import math
x = input("Enter a number to return the square root, press q to quit: ")

while x != "q":
if x.isdigit():
print("The square root of " + x + " is " + str(math.sqrt(int(x))))
else:
print("You entered an invalid input ‘" + x + "’ please try agin")

x = input("Enter a number to return the square root, press q to quit: ")

print("Thanks for playing!")
[/python]

Output of a sample execution.

$ python3 control_flow_basics.py
Enter a number to return the square root, press q to quit: 81
The square root of 81 is 9.0
Enter a number to return the square root, press q to quit: 33
The square root of 33 is 5.744562646538029
Enter a number to return the square root, press q to quit: break it
You entered an invalid input ‘break it’ please try agin
Enter a number to return the square root, press q to quit: q
Thanks for playing!

Comments are closed.