The Raspberry Pi is a small, affordable single-board computer that you will use to design and develop fun and practical IoT devices while learning programming and computer hardware. In addition, you will learn how to set up up the Raspberry Pi environment, get a Linux operating system running, and write and execute some basic Python code on the Raspberry Pi. You will also learn how to use Python-based IDE (integrated development environments) for the Raspberry Pi and how to trace and debug Python code on the device. Please note that this course does not include discussion forums.

Reviews

HB

Really nice and easy introduction to Raspberry Pi and Python. I loved the part when the professor said something about Scratch, like 'we'll not use it, we're way above it'. :)

AA

Jul 20, 2019

Filled StarFilled StarFilled StarFilled StarFilled Star

As an intro course to Raspberry Pi platform it's okay. I guess the next course in the series will have assignments that are more difficult and enable us to learn more.

From the lesson

Module 3

We present the basics of the Python programming language to prepare you for programming on the Raspberry Pi. Many languages can be used but Python is the most convenient for the Raspberry Pi because convenient APIs are provided for basic operations such as controlling the pins. Python is a powerful language with useful features that we will present so that you can use these features to control the Raspberry Pi.

Taught By

Ian Harris

Transcript

[MUSIC] When you program, you need to program in some kind of a programming environment that roughly means the Arduino IDE, so with Arduino earlier on in this specialization, we used the Arduino IDE. So that's a programming environment. Basically, that minimally entails some kind of a text editor where you can type your code in. Also either a compiler or interpreter based on the language. With Python, we'd be looking for an interpreter of some kind. So that's sort of minimally what a programming environment involves. So, with this Raspberry Pi, we've got basically two types of programming environments that we can use. One is some IDE, integrated development environment. So an IDE is basically one tool that puts the text editor and the interpreter and other things you might need together into one tool. Like the Arduino IDE, that was an IDE, right? Integrated development environment. People like IDEs. There are several IDEs you can use, Eclipse is a common one for free, there are a lot of IDEs. So you can use an IDE, or, oh, and for us, since we're doing this in Python, the IDE that we will end up using is IDLE, I-D-L-E. That's a default IDE with Python, the first one. When you get Python installed on any machine it has IDLE. IDLE's a nice simple IDE, it's convenient. So you can use an IDE or, oh, well actually, first how you invoke IDLE, if you wanna invoke that. You can go a menu, so if you look in at, so I'm assuming right now that you got a Raspberry Pi. And you got the desktop running, right? So you got the graphic interface. Up in the upper left hand corner there's a Menu button. So if you go to Menu > Programming > Python, there'll actually be two options there, there's Python 2 and Python 3. Go for Python 3 cuz that's what we want to run. So you select that and that'll start the IDLE IDE. Now, if you don't happen to wanna use an IDE for some reason, I recommend an IDE for now, but if you don't wanna use an IDE, another way to do is to use a text editor and an interpreter separately. So you can run any old text editor like Nano or Pico like we used earlier. You can open that up, type in your code, save it, then after you saved it and my example I'm calling it test.py, .py is a common suffix for Python programs. So let's say I call it test.py when I save it in my directory. Then in the terminal, in my shell, I can type Python 3 test.py and that will execute the script. So that's another way to go about it. You can just use the tools, you can use them integrated in IDLE, an IDE like IDLE, or you can use the two things separately. Any text editor you want plus the interpreter. We're going to, throughout this module, anyway, we're going to use IDLE, we're gonna assume the use of IDLE. Of course, sometimes it's useful to do the separate text editor method, because that allows you to get into super user mode sometimes, so for some things, we'll see this in the next module. There's some programs where you need to have permissions as root to actually execute the code and for that you need sudo, s-u-d-o, sudo command at the command line. So for that it'll be useful to do separately at the command line but now let's assume that we're using IDLE. So we assume we're using IDLE and we're using Python 3. All right, so IDLE, so say you want to execute Python code, you want to execute it, there are basically two ways to execute code. In IDLE, actually in general. Execute Python code, specifically. You don't have this option with a C or a compiled language, but in Python you do. You have interactive or what I'll call batch. Interactive is where you type in the lines of code one at a time. And so that's what you're seeing at the top left, that window that you see in the top of the slide. That's showing an interactive mode, right, in IDLE. It's showing IDLE's interactive window, and there's a prompt that's circled there. And that prompt is just saying look, after this prompt you can type. So you type in whatever the Python command is that you wanna execute, you hit Enter, and it will execute that command and print out any results. And then it'll give you a new prompt, okay? It's like a little terminal. It's like a shell. In fact, it is a shell just for executing Python code. So that's the interactive way. Now, that's convenient for trying things out, but eventually, you wanna write a big piece of code and run it all at once. So after you tweaked it and tuned it, then you wanna run it all at once, as a traditional program would. So that would be batch mode, and with that, you just write the whole program in one file, and then you just execute that whole thing, and I'll show you how to do that too. We'll be doing both. Mostly we'll be going with batch, but interactive is useful just to experiment with things. So if you wanna do interactive execution you need a Python shell. So how do you get a Python shell? If you start IDLE, the shell is default. The window that opens is the shell. That's what you see up in the window that you see on top of the slide. That's basically the window that you get. Something that looks like that. So just start IDLE and you'll get an interactive shell. And you can type in your instructions. Now, if you want to do that and get an interactive shell, Python Shell, and you want to do that from the terminal window at the command line, then you would just type Python 3. And it'll start an interactive shell for you right there in the terminal. In LX terminal, rather, if you're using that terminal window. Okay so, let's say you wanna execute entire programs in batch mode in IDLE, right? So you wanna, instead of just executing one line at a time that you type in, you wanna write a whole lot of lines of code and then just execute them, and you wanna use IDLE to do that. So, what you do is, you start IDLE. Then you make a new file. So, File > New File, it creates a new window like the one that you see here up in the top of the slide. And in that window, you can type in your code. So you type in your lines of code. Now I got a few sample lines of code there, from some random program that I wrote at some point. So you type in your lines of code. Then, once you type them in, then when you wanna run it, you select Run under that highlight that I've circled in red. You select Run > Run Module. Run module is an option under there. And that will interpret it and start running it. So that will pop open a Python shell and it will execute that code inside the shell. So that's how you run batch mode programs in IDLE, and we'll be doing that a lot. Thank you. [MUSIC]

Explore our Catalog

Join for free and get personalized recommendations, updates and offers.