Developing with Python | CompTIA IT Fundamentals+ (FC0-U61) | Part 13 of 38

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Welcome to ITProTV, you're watching the CompTIA IT Fundamentals Exam For FC0-U61. I'm your host, Ronnie Wong. And today, we're gonna be taking a look at Developing with Python. And here of course to help us out is Mr Don Pezet himself. Don, welcome back to the show. Hey Ronnie, thanks for having me back. And if you've been watching our whole series, the last couple of episodes, we've been talking about the basics of programming, the fundamentals, the concepts, the theory, right? We haven't actually gotten in and really written any lines of code at all. And so in this episode we're gonna fix that and we're gonna take a look at the Python programming language. We're gonna walk through a few examples and show how to write a basic application, how to execute it, and actually see some of that. And we'll start off really basic and then we'll add in additional components as we go through and we'll make it a slightly more complex application, and show you what it takes to write your first Python application. All right Don, so when we say that we wanna get into starting to write our first Python Application, is there anything we need to know before we get started? Well, the main thing is you do need a, what's called a development environment, something to develop on. Now Python you can go and download it from their website it's free. They have it, it's actually built into pretty much every Linux, distribution that's out there. MacOS has it, as well. Windows doesn't, so if you running Microsoft Windows, you have to download and install Python on your system. But once Python is downloaded and installed, after that you ready to develop and you just need a text editor, and you can write the code in that text editor, and have Python call it and run it. So, that's really all we need as a prerequisite to get started. The only other thing that we need is just having the knowledge of how to do what we want to do. If you watched the last episode, I talked about variables and I kinda held up a red solo cup and put data in there, that was a variable. And then we saw graphically in MIT Scratch development environment. That's good, we know those concepts. But to then dive in and do it in Python, we have to know the commands that Python expects. And they're different and it's not a graphical user interface that's gonna hold your hand through the development process. You have to know the commands, type them out, and run them, and if they don't work, then you get an error and you gotta figure it out and fix it. Most developers spend a good bit of time troubleshooting and debugging code. Debugging, means they're looking for bugs, problems in the application that's making it not work right and then fixing them, getting them straightened out. Half of development is spent just trying to figure out how you wanna solve a problem, and then the other half is spent making sure that you solved it the right way. Because oftentimes there's more than one solution. Keep that in mind as we run through this whole episode that there is more than one way to kind of solve this. Now if I want to write an application, we can start off with something really, really simple. Let's start with the basics. In any computer textbook you buy, they always start with an application that generates a message onscreen that says Hello World. Right? Well in Python that's actually really easy. So what I'm going to do is I'm going to jump in here on my environment, and I'm gonna use a text editor that is called vi. Vi's not the most user friendly text editor, but it does nice color coding of the code as I write, so it looks really good on screen. It's what I normally use. You can use whatever text editor you want, whatever you're comfortable with. But I'm gonna create a file called hello.pi, and it's an empty file. It's got nothing in it, and I'm gonna write the first line of my application. Now, in Python if I wanna display something on the screen, there's actually a command I can use that's called print. I want to print something on my screen. And then I use parentheses to box in what it is that I want to print on the screen. I wanna print some text, and so I need to punch in that text. And the way I tell Python that I'm doing text is I put quote marks around it. So I'm gonna throw in a little quote mark here and I'll say, Hello World. There we go, all right. Now, as I was typing that, you might have noticed some really interesting things happening, okay? For example, when I typed the word print, it turned blue. That's because the text editor that I'm using understands Python, it understands what language I'm using. And if I use C or C++ or whatever, it understands. That's part of what a good environment development does. It helps you not make a mistake. If I typed prin and did my parentheses like that, see how print didn't light up blue cuz I spelled it wrong. I can visually spot that. And when I do a parentheses like this, it lights them up so that I can keep track of, if I open a parentheses, did I close it? So i don't forget and leave an unterminated parentheses like that. IDEs like this help us to make sure we don't make mistakes or help us find the mistakes before actual runtime. It doesn't stop you from making mistakes, it just helps you. So what I've run right here in a simple command. I said I want to print something and what I want to print is Hello World. All right, so I'm going to save that and then in my other tab here, I'm just going to execute that by using the Python command. Remember from our first development episode, Python is an interpreted language, so I'm calling the Python interpreter to actually run my hello.py. And, when it runs, I get Hello World, right. Displays it on screen, there's my message, we just wrote our first Python app. Now it's not very flashy but at least it gets us started and we get a chance to see going from source code to actually executing the application. All right Don now that you've introduced us at least to a basic application here in Python, we also in previous episodes talked about the idea of a variable. How do I add that into my program here? All right, the application I've written so far is what is called a static application. I could run it 1000 times, and it will always do exactly the same thing. It will always output Hello World. That's not a very useful program. I might want it to be a little more dynamic. What if I wanna be able to say hello to people, different people? I could create a variable and I could store a person's name in that variable. And when I run the program it can say hello to whoever's name is in that variable I just have to add it in. Right? So I'm going to come back over here to my application, and in my application, if I can stop my screen from going crazy, I'm gonna come in here and I'm gonna create a variable. Now remember when I had the red cups, I said I need a way to tell one red cup away from another one. I need a name. And so I'm gonna name this variable. And you can name it whatever you want. You could call it var1 is what a lot of text books will do because it's variable 1. I don't like to do that. I like to give it a name so I can remember what it is, right? And so when I give it a name I'll typically call it something based on what it's storing. Here I wanna store a person's name. And so I might call the variable name, or pname, or name1, something like that. I'm just gonna call it name. And that variable is gonna store some information. Now I might know that information ahead of time. I might not know it ahead of time. I might get it later. Right? So when I create the variable it might be the empty. But in this case, I wanna say hello to Ronnie. So I wanna store Ronnie's name in this variable. So I'm gonna say NAME =, and then in quote marks I'm gonna throw in his name. Anytime I do text, I've gotta throw quotes around it. So the system knows this isn't a command called Ronnie, it's a text string called Ronnie. All right. And from there, I could come in and I could say something like print and then name. So that's gonna print out Ronnie's name, and if I save that, And go over and run my program again. When it runs, I see it output Ronnie and then it output hello world. Now, using a variable by itself like that is pretty uncommon. Normally we're gonna mix these two things together. So instead of calling it out on a separate line like this, I might wanna say something like, instead of hello world, I wanna say hello Ronnie. So I want to mix some things together, I wanna say hello followed by a space, and then I want to say Ronnie's name afterwards. Well, the print command is expecting one thing to print. I'm about to give it more than one thing to print. So I need to stick a little plus sign in here, so they know that I'm adding more. I'm gonna say hello, followed by a space, and I'm going to say whatever's in the name variable. We'll display that, and that's gonna say hello to that person, okay, and I'll save that value. And now when I go and run the program, Instead of hello world, it says hello Ronnie. And I can even adjust that formatting a little bit more. Maybe I wanna add that exclamation point, it's really important to me. So I can do another plus and stick that exclamation point there at the end. And once I've got that one in there, I can save it. And now when I jump back and run it, now it's Hello Ronnie, and it's got the exclamation point. Now, the cool part about a variable is, you can change it, and you don't have to go through and change your program. You can use a variable over and over and over again, right, and have it 100 times in your program. And if I just go and change the value of that variable in one place, it changes everywhere else that it gets used. So if I decide that I don't wanna say hello to Ronnie anymore, I wanna say hello to myself, so I'll say name = Don. And when I save that, I can jump over to my other prompt and run it. And now it says Hello Don. Now, in a complex application, that variable might get used 1000 places. All 1000 of them would now reflect Don and not Ronnie. That's the advantage of a variable, we change it in one place, and it gets changed everywhere, it's kind of a core piece of getting those variables to work. Now, Don, can I only use one variable in a program, or can I use multiple variables in a program? You might be able to get something done with just one variable, but normally, you are gonna use a bunch, right, and they don't cost you money. Well, I guess they sort of do, right, because a variable, where does it actually store that information? In RAM, right, so it's storing it in your computer's memory. If you have enough of these variables, eventually you could run out of RAM. But we're talking about tons and tons, this one little variable here that's storing Don or Ronnie, your name's a little longer, I guess, so six letters, it takes to say Ronnie. That's less than a byte of data, I mean, very, very small, and you've got gigabytes of RAM in your computer. So it's not normally an issue, you can create as many variables as you want. But for me, if I wanna say hello to two people, I wanna say hello to Ronnie and me. Now I could change the name to be Don and Ronnie. But maybe later on, I wanna use the variables separately on their own. Okay, so I can come in and change this a little more, maybe I wanna have a name1 and a name2. Right, and now when I call them, I can say hello followed by name1 and, name2, like that. And I need some spaces here, because if I don't, it'll be name1 and immediately have the word and with no spaces after it. Right, so you've gotta account for things like spaces. But now it's gonna say name1 and name2. And I can go on and start using these separately and say things like, I might say, name1 and then is boring. Right, so whoever is stored in name1 is gonna be the boring one. And name2 is my favorite, so whoever's name2 is gonna be the lucky person. And remember that you can come in and change these. I can switch who's in name1, switch who's in name2. And I've reused these variables over and over again. I don't have to go and edit the code, I just have to come up here and edit the value up here. So I can come in and put in a particular person or name or whatever. And now when I go and run it, it's gonna process each of those variables and, oops, and I got an error. So remember, I said half the time we spend developing is coming up with a solution, the other half is figuring out what we did wrong? So I've got a problem here in my variable, let's see if we can spot my mistake. I do see my mistake, sometimes your IDE will call out an error and make it very visible and easy to see. In this case, the error isn't so visible, but if you look here, I forgot a plus sign. I should have had a plus right before name2. Since I left that plus out, it didn't think I was calling a variable, instead it thought I was calling a command. It was looking for a command called name2, which it couldn't find. So once I fix that, there we go, now we can try this again, and we run it, and there we go. Hello Don and Ronnie, Don is boring, Ronnie is my favorite, I'm using those variables and up they come. So you can use many, many variables, string them together, use them separately, mix and match, that's really the flexibility you get with development. Don, with Python, we can also call upon functions as well. Can you help us understand that a little bit more, and then how do we do an example of that? Functions, you'll use functions a lot, right, but we didn't really talk about functions in the earlier episodes, so let's just talk about that real quick. From a conceptual standpoint, a function is a collection of instructions that you can call different ways and have it do different things. All right, the best example that I can think of would be, one of our other educators, Justin, he always explains it as a soda machine. Right, if you go to a vending machine, when you walk up to the vending machine, it requires some input, it requires you to put money in, right. So you've gotta stick some quarters or a dollar in, whatever, you've gotta pay. Right, and then you've got a series of buttons, and you push a button with the food you want. Maybe you want Pepsi or Coke or Mountain Dew or whatever. You push the button, and then it pops out a can in accordance with the buttons that you push. Right, so that's a function, the input you provide is money and what you want. And the output it provides is the soda. But behind the scenes, there's a lot of stuff that goes on. It has to identify which little chute is holding the soda that you want. It has to turn the little gear or whatever to make the soda pop out, it drops down into a tray. And if you get change, it's gotta export the change, and you get that. That's all stuff that happens in the background. Now, do you know how that works? I don't, I've never opened a soda machine, so I don't actually know how the mechanics work behind it. But I use soda machines all the time and I get my soda, right, so I know they work. That's how functions are, when you write a program, you can create a function. And you can call the function over and over again and change what you're feeding into it. And that's really neat, because you can have it do more than one thing, and reuse that same function for different results. Let me give you an example. So far, we've been using variables to define these names, but I don't have to. Let me go back here, and we're going to delete some of this out, I'm gonna get rid of these variables. And let's, actually, you know what, let's just start from scratch here. So instead of using variables, I'm gonna come in and create a function. And the function is going to say hello to a person. Right? Now when I write a function, I need to give the function a name. And so I'm going to define the function. Whoops, I'm going to try and define the function. There we go. So def says I'm defining a function. And then I need to give the function a name. I'm gonna call my function hello, right? And then I'm gonna do an open and close parenthesis. And in those closed parentheses there is where I tell it what input I'm expecting. The soda machine, it's expecting money and a button push, right? What am I expecting? I'm expecting a name, okay? I'm expecting to be given a name. That name is a variable. I don't know what's gonna be in it yet, right? But I know it's going to exist. So the variable is not existing by itself anymore. It's existing inside of this function. And then I can tell it what I want the system to do with that name, all right? So if I'm given a name, what do I do with it? Well, I'm going to print, Hello followed by that name, whatever that name happens to be. And maybe I wanna leave it at that, or I'll go ahead and add my exclamation point. There we go. So that's what this function is going to do. I can give it any name I want, and it's going to output hello followed by that person's name. Now all I have to do is call the function. Any time I wanna say hello to somebody, I can call the function, And I can provide a name. So maybe I provide Ronnie. And then I can call it again, and I can provide my name like that. I've only got one variable, but every time I call the function, I'm changing the variable to something else. So here, I call the hello function, and the variable is being set to Ronnie. And then I call the hello function again, and I change the variable to Don. We can reuse this function without having to go and update this code, and without having to create multiple variables. If I just need something temporarily, just for a moment, there's no point in storing it in a variable and keeping it forever, right? Like var one and var two, or whatever, name one, name two, I don't need to keep them around. I just need to be able to call this function with different people's names, and I can do it right here. Now if I save that, and I go and I run it, what I'm gonna end up with is, an error, shoot. So I forgot something. I'm running Python 3. In Python 3, the syntax changed a little bit from Python 2.7. This is a challenge of programming, that you have to remember some of the subtle differences. And one of those subtle differences is that in Python 3, when you define a function, you're supposed to stick a colon at the end, which I left out, leaving out that one little colon there. Now, again, my IDE didn't flag that. It's just this part of learning the language like, I'm supposed to have a colon there. I don't need the colon when I call the function. What that's doing is it's telling Python, hey, the next commands that are coming up are actually part of the function. That and the indent that I did right here are the hands that tell it, this is part of the function. If I took that indent out, it would actually break the program. It wouldn't work. So understanding that sometimes when we put a space on the screen, it's actually there for a reason. That's another part of learning the language, and it varies from language to language. All right, so now, now that I've actually written it properly, I should be able to go back over here and call my script. And there we go, I get Hello Ronnie, Hello Don. It called the same function twice, once with my name, once with Ronnie's, and then it output that information. So that's kind of a way that we use functions, and this was really simple. The function can be very, very complex, right? Remember, in the last episode, I moved the cat to the right and then I moved the cat to the left. It can be multiple steps. I'm just doing one thing here. But that reusability is what really is gonna make a function powerful. Now Don, since we've actually got this now working in the way that we want to, is there a way that I can also say, hey, if you say hello Ronnie, it also gives me another message? And if you say hello Don, it gives me a different message. Can I do something like that? Absolutely, so what you're telling me about are logic, logic controls, that we make this decisions in the application. The application that I wrote at the very beginning just printed one message, and it always did the same thing. There was no decision logic whatsoever. But to write a really powerful program, you need to make decisions. It needs to do things that vary based on circumstances. Think about a video game that you might be winning and doing great, or you might be losing and doing bad. The program needs to be able to react to both of those. If you're losing, you get game over. If you're winning, you get the next level, right? Those are decisions that are made. So we can add some decision logic here. We can do things like what Ronnie was describing, which is called an if statement. An if statement says if a certain condition is true, we're gonna do one thing. And if the condition is false, we'll do something else, right? So if Ronnie's favorite color is blue, display blue, and otherwise display red, right? We can set those kind of conditions inside of our application. So if I go back here, the message that's being displayed in this function is always the same. It's going to be hello followed by somebody's name and then an exclamation point. But maybe I wanna follow that up with something a little different. Like if it's Ronnie's name, I wanna say something nice. And if it's anybody else's name, I wanna say something mean, right? Or vice versa if I just wanna be mean to Ronnie. So if I wanna punch that in, I need to do an if statement that looks for what that name happens to be. So I might say, let me just kinda get back inside of my function here. I might say if, so I'm gonna say hello no matter what just cuz that's how I'll keep track of this. So I'm gonna say hello Ronnie, and then I'm gonna say if the name equals Ronnie, all right? Now notice I did two equal signs there. When you do a comparison, I'm gonna compare the actual name variable with the text that I'm providing here. When you do a comparison we do two equal signs, and there's a reason for that. The reason is when you do a comparison it's normally going to be equals, not equals, less than, greater than, less than or equal to, greater than or equal to. Well, we have greater than signs and less than signs. We've got those, right? So I could come in and type those. But do we have a greater than or equal sign, or a less than or equal sign? We don't, so when we type those we would have to type less than or equal, two symbols, or greater than or equal, two symbols, or not equal. Not equal is an exclamation point and an equal sign, two symbols. So when I say if something is equal, we do two equals so that it knows that I'm actually just doing comparison. If I just did a single equal, name equals Ronnie, what does that do? It overwrites the variable. Remember, earlier we defined a variable by saying name equals Ronnie. It would overwrite it. So comparisons, you always have two operators. In this case, I've got the two equal signs. So if the name equals Ronnie, now if is a function. And when I call it, I'm doing the little colon at the end to say the next lines are actually part of that if statement. I'm gonna say, if the name is Ronnie, then I want to print. And I guess I could use the variable here, name plus is my favorite edutainer. There we go. So that's what I want to display if it's Ronnie, okay? And then what about if it's anybody else? Well, if it's anybody else I'm gonna say else, which is also a function, so I'll do a little colon on that. Else print name + is not my favorite! I'm in a very exclamation point-y kind of mood today. [LAUGH] Everything's an exclamation point, all right. So now when this function runs it's gonna look at the name. And if the name is Ronnie, then it's going to display this text right here. Yeah, you're my favorite. But if it's not Ronnie, if it's anybody else, right? So I could come in and add several more statements down here. I could do hello Bob, right? And hello Susan. I'm just going to add a few more people. When Ronnie runs, it'll say he's their favorite. When anybody else runs, it won't, right? Or I could have it where it just displayed nothing, but in this case, I wanna call it out so we can see it on screen. So once I've got that in place, now I've got a little bit of logic in my program. And I can save that, and when I come over here and run it, assuming I don't have any typos, here we go. And I can see, Hello Ronnie! And Ronnie is my favorite, but then Don, Bob, Susan, we're not the favorites. We're not cool kids. So now it's making decisions. I'm calling this one function. But depending on the data that I give it, it's acting differently. And that's exactly how a soda machine works. Depending on which button I push, I'm gonna get different soda out of it, right? That's the power of functions. If statements are really what powers that. So you'll use if statements on a regular basis, and some of them get really, really complex. You might do nested if statements and all sorts of other craziness. That's really what's giving you some of the power in the applications that you run. Don is there also a way that I can use what we call looping inside of my Python program here? Yeah that was the last thing that I showed in the scratch example was we had the catwalk back and forth then we did a loop to make it loop over and over and over again. You can do the same thing right inside of Python. Creating a loop is pretty easy but there's two parts to it and sometimes people forget the second part. The first part is we define the loop. We say these are the actions that I want to perform over and over again. And if I stop right there, it's going to perform them over and over again. Forever, right? [LAUGH] Computers are very literal, so when you tell it to loop, it will do it forever. So, the second part is defining a limit. I have to tell it a limit, to say I want you to loop a certain amount of times and then stop. And how it stops is up to us. We can do it programmatically based on how many times its looped. You can do it on time, maybe I want it to loop for five minutes. We can make it loop forever until a human presses a key. You make a decision on how you wanna stop the loop. I'm gonna stop the loop programmatically. I wanna run this and I want to display the Ronnie information, I wanna do it five times, okay? So I'm gonna go back to my application, and I'm gonna change it. First off I'm going to, let me get rid of some of these other people here. So I'm just gonna call him Ronnie. Right? And I want it to do this five times, so I need to put a loop in here. Now the loop itself isn't defined in the function. It's defined when I call the function. So the function's gonna be same no matter what. I just wanna call it five times. Now I could cheat and I could say, hello Ronnie five times. I could just type that five times. I'm gonna call the function five times, right? But if I wanna call it 5,000 times, I'm not gonna type it 5,000 times. I can type it once and build a loop around it. So the way a loop works is like this. We say, while, and then we give it a condition. So, while some particular condition is true, we're gonna take this action and my action in this case is going to be, hello Ronnie, okay? And when the condition is no longer true, then we'll stop the loop. Now if I stop right here, this is actually written properly, but it's gonna loop forever. It's going to run that Ronnie statement over and over and over again. So I need some kind of value to track this and we're normally going to use a variable for that. I'm going to create a variable called loop and I'm going to set loop equal to zero. In the loop, I want to keep track of how many times I've looped around. If it's the very first time I'm doing, then my loop is zero. I've looped through it zero times. Then, in my while statement, I'll define the conditions. I wanna loop through this five times. So I'm gonna say that while, loop > 6, go ahead and run. But if loop is 6 or higher, stop running. Okay? Now, each time I run the loop I'll add a little line here that just says, loop = loop + 1. I'm gonna add 1 to it. So it starts as a zero. The first time it runs it calls, Hello Ronnie, and then it adds one to loop. Then it runs it again. This time loop is 1. So it says, hello Ronnie and then it adds one and the loop becomes two. And then it runs again, loop becomes three, four, five and then on the fifth time, loop becomes six and hits the while statement. And the while statement says, whoops, loop is now equal to six, not less than six. And so we stop. Now we've set a stop, for how many times that function is going to run. And once it's done, it stops and the program exits. I put a limit on the loop, that's always an important thing. If you don't do that, it will run forever. Now let's see it work. So that's gonna run five times. So I'll jump over to my other tab here and I will call it and there we go it runs five times and then it stops. So what we've done is we've taken what was a fairly simple program just printing text on the screen and we introduced variables, functions and loops. And now we've got a program that's a little more complex, right? That we start to have a little more control, a little more functionality with, right? Well, we've just scratched the tip of the iceberg. Programming languages go way beyond this, right? Maybe I wanna turn the lights on in my house. Now, I'm gonna need a lot more to get this application to do what we want. But in 11 lines here we've actually introduced a little bit of logic here and seen a basic application. Now hopefully that gets the gears turning in your head and helps you understand like, all right if I want to write a program to do something, I'm starting to understand, here's that logic that we have to follow, the decisions and flow chart that we have to create. And once you have that figured out what you wanna achieve, then you just need to figure out which language you're gonna write it in and what the commands are that match for that language. Now, what I showed you in this episode was Python, wildly popular programing language, a great one to learn. But you might go work somewhere else and you get everything in C++. Well, the steps I'm taking here are going to be the same in C++, I just need to change the commands, change them to match whatever C++ uses and then it will compile and run the same way. So the concept is what's really important with a lot of this stuff, not so much the execution. Alright Don, thank you again for helping us to understand this idea of programming concepts a bit better by showing us the examples that we have just taken a look at directly right here in Python. So a lot for us to consider. Any other final words Don as we might be considering this and going, hey, this looks like an area to get into? I will say that programming and development, it's not for everybody. Some people just don't like dealing with this, you'll hit problems. You'll get stuck and you'll spend time trying to figure it out, and you're trying to solve problems that nobody's solved before, so you're having to be innovative and creative to come up with these. Development work, it's part science and part art, you know? There is a bit of artistry that goes into creating an application that is robust and yet secure, stable, and fast. That's all stuff that developers have to worry about. But if you find that you're in synch with this, if it piques your interest, it's a very rewarding career because you're actually creating something that, at the end of the day, there's a program that you made or there's functionality that you've added to a program or there's a problem that you've fixed. It's a very tangible feeling that you don't get in a lot of careers. So it's a neat career field to get into. Understand that what I've shown you is literally the tip of the iceberg. It's like learning to walk. We're just crawling here, right? We're not even up on our two feet better yet running and sprinting. So, there's a ton more to get into it, but as far as the basics, I think I've given you guys a pretty good example here of what it's like to develop an application. Wise words to consider, Don! Think about it again. Take a look at these episodes because there's a lot for you to learn just from the basics. And then of course, we'll explore other options that you have in front of you. But that will do it for us today. Signing off for ITProTV, I'm your host Ronnie Wong. And I'm Don Pezet. Stay tuned right here for more of the CompTIA IT Fundamentals show. [MUSIC] Thank you for watching ITProTV.
Info
Channel: ITProTV
Views: 6,399
Rating: undefined out of 5
Keywords: comptia it fundamentals study guide, comptia it fundamentals course, comptia it fundamentals (itf+), comptia it fundamentals fc0-u61, comptia it fundamentals training, comptia it fundamentals full course, developing with python, python comptia
Id: 7UKSN3H8uEw
Channel Id: undefined
Length: 32min 48sec (1968 seconds)
Published: Mon Apr 15 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.