Actually using your Raspberry Pi Part 3: teaching everyone the basics of Python
After finishing the Knight Lightning game in Scratch, I was feeling better about my Raspberry Pi efforts. After a little over a month I had become familiar with using the Pi, failed at an IP Camera project, and finally actually built something with my Scratch game. It felt like the right time to actually use a “real” programing language, and conveniently the next chapter in my user’s manual was on Python!
I have some experience with Ruby thanks to a course with Teamtreehouse.com, so I thought python would not be too much of a stretch. This turned out to be true, so I just needed to learn more of the nomenclature of Python and not relearn everything that I learned with Ruby. Python is easy to read and emphasises using plain English keywords. This means it is code that sounds more like describing what you want to do and less like techno-babble. Good for Newbies, but still powerful.
The good news with picking Python for your Raspberry Pi project is that Python comes with the starter kit, so I did not have to download anything extra. Let’s look at some simple things you can do with Python, and then I will show you how to save code and run it.
How about I show you by writing some code! The examples below should be enough to get you up and coding in Python.
I will start with
- Data Types
Now let’s look at
Now let’s check out
And everyone loves loops! Some would say they never end….
Last one, we can do this.
Wow, thanks for reading all of that! The good news is that you know most of what I know about the basics of using Python. I also want you to know that Classes and Objects are great things to go learn about, but I will not get into those here!
So, you are familiar with some principles and want to start coding. Where should you do that? If you want to jump right into it you can open up a Text Editor doc. Here is a screenshot of what that looks like on my pi.
I put together some code to take 3 variables and add them to a list one by one. You can see that I used the try: I talked about and I also add the except Error as e: to go with it. As homework you can look up more about how those work. A big thing to remember when you write code in a Text Editor or anywhere really is to save the file as .py. When you run the code this will be very important. Now how do I run this code I saved?
In the top left of your pi menu you will see a what looks like a computer monitor called the LXTerminal. This is the command line terminal that you can run files from. I saved my file on the desktop so I will need to navigate there by typing
Note that the case matter so, cd desktop will not work. Now that the command line is looking at the desktop you can enter
I saved my file as listing.py, so I used
Take a look at the results.
Try it out yourself.
If you do not have a Raspberry Pi you can still use Python! Just download Python to you personal computer and get started. You can find instruction and details at python.org
If you are interested in learning more about using Python with your Raspberry Pi there are some great resources at raspberrypi.org.
Learn more about the Python Idle IDE at raspberrypi.org.
Once I had spent some time practicing with python basics and running my python files through the command line and IDE I decided that I was ready for the big time. I was going to tackle a real project using Python and my Raspberry Pi. More on that next week. Have you ever heard of Twitter?