Chapter 4: Writing Our Own Python Programs in the IDLE
Now that we have had a chance to go through and look at some of the fun things that we are able to do with the Pi device, and had a chance to look at the basics of installing the operating system and more on our Pi device, it is time to get down to business and look at some of the great things that we are actually able to do with this device.
And in this chapter, we are going to introduce the Python programming language, learn how it works a bit, how to add it into the Pi device, and then start with some coding that will help us to really get the device to work in the manner that we want.
While there are many programmers who are going to stick with Raspbian and use this as the chosen coding language that they want to use on the Raspberry Pi, many beginners will find that working on the Python language is a lot easier.
Python is a really easy coding language to work with and is often the chosen option for those who have never spent any time learning coding at all in the past.
While there are a lot of reasons that we can enjoy the Python language, such as it being easy even for a beginner to learn how to use and how to code in no time, it is going also to be strong enough to get the programs done that we want in a short amount of time.
However, before you can go through and write out a program with the help of this language, we need to make sure that we are using the right IDLE editor on Pi so that it actually works.
That is what we are going to spend some time learning how to do in this chapter.
So, to make this happen, one of the first steps that have to happen is to download the IDLE editor for Python and then install it on our device.
The IDLE is something that we are able to easily download right from the Python website, which is https//python.org/downloads.
The IDLE is important for actually having a good environment to write out the codes we want, and it is going to stand for the Integrated Development and Learning Environment.
There are a lot of benefits that we are able to utilize not only with the Python language but also when we actually work with the proper IDLE editor to write out the codes that we want to do in Python.
However, some of the features that we are going to notice with this particular IDLE, rather than some of the other ones that are out there for Python, includes:
- Use the Tkinter GUI toolkit in order code in pure Python.
- It is able to work on many platforms.
This IDLE is set up to work on Mac OS X, Unix, and Windows, and it is going to work and look pretty much the same.
- It includes the Python shell window, which is the interactive interpreter, with the colorizing of error, output, and input messages in the code.
- There is also a multi-window text editor that has a lot of features that you will find helpful when you are working on your code.
Some of the features that you are able to use with this include auto-completion, call tips, smart indent, Python colorizing, and multiple undo.
- You are able to search inside any window, search through more than one file, and replace things inside the editor window.
- There is also a debugger in this IDLE that has persistent breakpoints, stepping, and viewing of both the local and the global namespaces.
- It includes browsers, configuration, and other dialogs.
Once you are on the right page to help us download the IDLE, you can then go through and choose out which of the versions of Python that you would like to work with.
Right now, we are going to pick from Python 2 or Python 3.
The differences are not much other than a few features, but most people decide that Python 3 is the best option to help them because it is the most recent version.
There are a few instances when Python 2 is going to be the best one to work with, so do a bit of research and learn which is the best for your needs.
After you have gone through and are done with the download page that we had above, and you clicked to get this over on your system, it is going to begin with a good install here.
You need to allow a few minutes to get the install done.
You can then run the IDLE by finding it on the menu for applications.
Once we have this IDLE running, keep in mind that we are then going to have two main methods to work with to write out all of the Python programs that we need in this editor.
The first one is to write out the code in our console.
The second one is to open up a document that is new, write out the whole program we want, save it, and then run the code when it is time to do so.
Both of these methods work and professionals, as well as beginners, swear by both of them.
Often the choice is going to depend more on the method that you like the best.
When we enter into the next section, we will look at both of these examples and see which one is the most likely to work for your needs so you can make the decision.
To make it simple, you will find that if your goal is to find a good way to test out a part of your code, or if you are working with a relatively small piece of code, then you want to write it out in the console to make things easier.
But if you plan to work with a long piece of code and write out a big program, then you would want to work with the document instead.