2.1 Introduction to the Interpreter
One of the coolest parts of Python is the interactive shell that comes with it. With this little gem you can experiment and practice with new Python concepts before you even dig into writing Python code in a text editor.
1.Introduction2 lessons, 11:32
2.Python Building Blocks6 lessons, 1:08:07
3.Controlling the Flow7 lessons, 1:20:10
4.Common Data Structures4 lessons, 46:49
5.Application Structure7 lessons, 1:15:12
6.Collections7 lessons, 46:55
7.File I/O6 lessons, 48:51
8.Networking5 lessons, 43:48
9.Connecting to Network Services3 lessons, 34:27
10.Conclusion1 lesson, 02:08
2.1 Introduction to the Interpreter
The next important part of the Python story happens to be one of the unsung heroes that you're really not going to typically talk a lot about. You're just going to assume that it's there and after a while you're not even going to know that it's there and you're just going to wind up using it without really having to think about a very much. And that happens to be the Python interpreter. So what exactly is that? Well, it's a shell if you want that you can use to not only type in little bits and pieces of Python code and execute them and actually really write full applications should you really want to. But it's ultimately where all of your Python code is going to get funneled through to ultimately execute now that can be in the form of interactively writing code within the shell itself, or writing Python code out in another file somewhere. And then ultimately executing the Python interactive shell with that particular file as an argument. So what exactly is all of this mumbo jumbo? Well, let's take a look exactly what it is. So I'm gonna come back into PowerShell on my Windows machine here, and I'm going to simply type in Python. Now this is going to kick off the interactive shell and as you can see here it gives the version of Python, as well is the date associated with that version, and a little bit of information about it itself whether it's 32-bit or 64-bit or what have you. And then it's going to give you a couple commands that you can type to get a little bit more information about Python but then you get these little three kind of greater than signs. And this is what's basically telling you right now that you are in the interactive shell. Tell me what to do. So here's your first opportunity to start writing some very sophisticated Python code. And we're gonna to start off with one of the toughest ones to understand that just so happens to be the ever famous 1+1. Now that doesn't really look like Python code, but if you're to execute that, it executes exactly as if it were Python code. And that's because the interpreter itself is just that, it's an interpreter. So if you were to pass in data to it it's going to interpret that data as best it knows how. And as you can see here, it knows mathematics, it knows basic addition, subtraction, multiplication, and division. So I could do the same thing, I could say 1-1, that's of course 0. I could say 2*2, that's going to be 4. And I can say 4/2 is going to be 2. Now this brings up an interesting point and we're gonna touch on this a little bit later in an upcoming lesson. That we just took two integer values, four and two, and did 4/2 and it gave us this decimal, this floating point number and that's the way that Python is designed. So once again, we'll touch on that a little bit later when we start talking about some of the basic building blocks and types built into Python, when it comes to numbers. But getting back to the shell itself, this is the basic process that you're going to follow, albeit you will be touching more so on the actual Python code itself when you're writing these applications and following along through this course. But this is a very interesting point. I can just start throwing data out there and as long as the Python interpreter understands the basic data and the flow of what I'm asking it to do, it will go ahead and interpret that. Or do with it what it knows how to do and it just so happens it knows how to do basic math. Now what's going to become very interesting and very fun for you to play around in here is this is a wonderful sandbox to be able to play around with new things that you learn when it comes to Python. So let's say I wanted to learn how to create variables. I could create a variable called yes, and I couldn't initialize it to be equal to 1, and all of a sudden I see, okay, well that went off into the ether somewhere. Well I could say all right well, well I wanted to create another variable called no, and we'll set that equal to 0. Okay, well the same thing seems to have happened, but what if I were to type in yes now. What if I were to say, what is the value of yes currently? And right now it is set to 1. Okay, well that's pretty nice. So I can create variables and I can assign values to those variables. And the interpreter, the shell itself, is going to retain that information and be able to give it back to me whenever I want it. So, I can also no here and it's gonna give me back zero because that's what I designed no to be. As we move along and we get into more sophisticated topics through and within Python, you're going to be able to do the same things here. So I could even create functions. I could define a function that's called say hi. And I'm going to end that with a colon. And we'll talk about that a little bit more later on. And then I'm going to need to indent the next line. And once again, we'll talk about that a little bit later on. But now I'm defining a function called, say hi, that does not take in any arguments. And just say I wanted to return, hello, just like that. So now I hit Enter. And now I want to stop defining this function. How do I do that? Well, I just hit Enter again. And all of a sudden I'm back to the three greater than signs, but what has that done really? Well, inside this particular session of the shell, I have now defined a say hi function. So I can simply call that function and I get returned, hello. So as you can see here, this is a very powerful learning tool, as well as an actual very basic. I hesitate to use the word IDE at this point but it's really just a nice sandbox to build a play around with things. But as you can see I could build full applications in here, but because this is basically session based I'm going to lose all of this stuff as soon as I close out this particular version of or this particular session of the interpreter, so how do you do that? That's kind of the next step. We've gotten in there, we typed Python, we played around with some code, we double checked how Python does math, we created a couple variables, and we created a function. We're done with all of this, we wanna get out of here, how do we do that? Well depending on your operating system, there's a couple different ways. Now, depending on your version of Windows in this case or whatever shell or whatever basic command line utility you're using, you may be able to get right out of here using Ctrl-Z. But in my case, I am not. That's not going to work on my version for a number of reasons but I think that one of the main reasons is because I'm using PowerShell here. But the other way to get out of here is I could simply type in the word quit, open close parentheses. This is saying I want to execute the quit function. And by doing that it pops me back out to whatever location I was in before. So if I clear this out and I do this again and I say I want to go back in the Python. So I could do quit. But that's kind of a pain to type that every single time. I can also pop out of here with Ctrl+C which is basically just going to do an interrupt, and say, you know what? I'm done with everything here. Take me back to whatever location I was at. So that's the basic interactive version of the shell, the interactive portion, but I can use it in another way. So the other way that we're going to kind of play around with periodically here and there, were going to bounce back and forth, is to actually use it as a command line utility. So I can use Python. And then I can pass into it, some sort of location to a file that contains Python code, to actually execute it. If I were to play around with that or want to try that. So now, at this point, I want to do something a little bit differently. So I'm going to open up a text editor. So in this case I'm gonna use Notepad++. And I'm gonna to do something a little bit similar to what I did in the interactive shell. And what I'm looking to do at this point is I'm going to define another function. And in this case I'm going to call this sayGoodbBye since we already did the sayHello version. And in this case instead of actually returning something. I'm simply going to use the built in print function in Python. And in this case I'm going to print, Goodbye. Just like that. So then once we've done that, once we've defined that function I want to call it. And this all seems very strange and foreign to you at the moment don't worry I'm a promise you this is going to seem much more simple as we start to dig in and pick apart all these different pieces of the Python language that I'm using right now. But I'm gonna save this into a Python directory and I'm gonna call this sayGoodbye and I wanna make sure that I save this as a Python file, so I'm gonna get that py extension. So I'll go ahead and save that. So here's my little Python file. So I'll open up my shell again. So and this time, what I wanna do instead of just typing in Python and jumping right into the interactive shell, I wanna actually go to the directory where I've saved that. So this is gonna be C:Python. And so if I'd list the directory structure, as you can see I have my saygoodbye.py file. So I'm going to say Python and them I'm gonna pass to it my files. Say goodbye that py, and there you go. We have goodbye. So there's going to be your two basic options when it comes to playing around, and ultimately following along in this course we're going to be able to do either one of these methods as we go through this course. You can either do all the code samples interactively through the interactive shell, and just type things in as you go, which is definitely good to get started and play around with certain things. But if you ever kind of get into the point where you're getting a little bit more serious about Python. And you want to maybe write some more well structured code, then I definitely urge you to go the route of actually using a text editor and writing these into files and saving them and backing them up and using source control if you want to. And then ultimately passing these Python files into the interpreter. That just is a more professional way to do it. So, you can definitely play around with the interactive shell cuz it's fun and you get to learn things quickly. But ultimately, I would urge you to kinda go the other route once you starting getting a little more comfortable. So now that we're familiar with what the interpreter is and how to use it in two different ways. Let's start to actually talk about the Python language itself and start to get into some of the building blocks.