Python Tutorial for Beginners #3: How to use outside libraries and loops!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome clarity coders in today's video we're going to cover learn python the right way chapter 3. now this chapter is really important we're going to start working with outside libraries and loops and visualize it as we're doing it now as you know if you're following along you can check out the book version in the description below as well and i want to thank replit for sponsoring this video and bringing it to you ad free let's not waste any more time and jump right in [Music] all right so as you know if you've been following along you don't have to do this in replay i'm going to not just because they're sponsoring this video but it's a really easy way for us to all be on the same page in the same environment so if we work in replit together we're going to follow the same installation process for outside libraries we're going to have none of the issues with different python versions or anything like that so this is the environment that i recommend you following these videos with just for ease of use but if you're following in some other version or some local version of python that's fine as well so what we're going to do today is we're going to pull an outside program now this program may seem a little silly at first it's called turtle and it uses turtles to help us visualize on the screen different parts of our program now it's very important because it's just like working with any other outside library so if you're working with another outside library for gathering files from a website or whatever computer vision or ai or anything else it's going to follow the same pattern so i'm going to teach you how you can work with some of those libraries so the first thing we want to do is do an import statement and we're going to import that outside library so what we're going to do is import turtle now very important here capitalization does matter so make sure when you're typing along with me that you're following the same capitalization here now with replit when we do our import like this it's automatically going to pull in that library if you're in another environment you might have to pip install turtle in order to follow along with us now we're going to write a couple other lines so to use turtle we have to create a window in our program so we're going to set a variable and you can call this whatever you want but if you're going to follow along line for line you want to call it the same as us i'm going to follow along with the text which uses wn and i'm gonna set it equal to turtle dot screen now i'm using turtle.screen because i'm following along with the documentation which i'll get to in a minute so this outside library has a method on it or a function that allows me to create a screen using this exact line here now the capitalization obviously matters and blah blah blah now on that screen we're going to create an instance of a turtle so we're going to create an actual turtle object and we can name this variable whatever we want in the text they name it alex so we'll follow that we're going to say alex equals turtle dot turtle now again you'll notice some mistakes i've made already i forgot an r here so remember spelling obviously matters and then here you'll notice i use a lowercase turtle and then an uppercase turtle we'll get to a little more of that in a minute but you want to make sure you're following the exact same format so what we've done here is we've created a variable that points to our screen and we've created a variable that points to our turtle instance now we're going to do some simple functions with that turtle we're going to do alex dot forward and we can do 50 and then we're going to have him turn so we're going to say alex dot left and we'll do 90 to turn 90 degrees and then we can do another forward of 30 say okay so what we're doing here is we're using an outside library to create two instances of objects so we're going to create a window object and a turtle object now these variables don't matter at all what we call them the wn or the aleks they'll work as long as we're consistent and using them throughout we can call them whatever the other side creating our instance we're using the library's methods and functions and whatever they've built in it so we have to use the exact spelling and capitalization that they use so let's go ahead and run this and just see what it looks like and you can see here it was really quick but our turtle started it moved 450 it turned 90 degrees left which makes it go up and then it moved 30 you'll notice that line's a little shorter awesome so that worked as we expected so at this point you're probably saying all right that makes some sense to me but it kind of looks like you're just making this up how did you know that the function was called forward and not move or something else well this brings me to our first professional tip here and this is very obvious and you may think that it's a little cheap that i call this a professional tip but you need to get into learning to use documentation right away so if an outside library has a nice documentation page which turtle does you can see that you can pull it up and really look into how exactly it's working here so if we pull up and this is on python.org so turtle's built into python itself so you probably didn't have to do a pip install but you can see that it has really good documentation here on the introduction page and i just googled this on the introduction page you can see it's covering things that we were already doing we imported turtle we moved turtle forward 15 we turned turtle right so on and so forth they mentioned the turtle screen class and then if you keep moving down you can see it even says turtle methods so you can see there's a forward which we used a backward a right a left a go to all these others now some are pretty self-explanatory right you could have guessed by using forward we probably know what backward means it's probably going to move backwards but some of these might not make sense to you so like the speed one so let's look at speed and you can see that we can use turtle.speed and we can have an integer range from 0 to 10 or a speed string c below so we can do 0 to 10 0 being fastest and 10 being fast and then normal being a slow being a three and slowest being a one okay so that makes some sense so let's try that just off of the documentation let's try and use the speed function so before it moves we remember how fast it moved last time let's say alex dot speed and we'll set this to 1. so now if we stop this and run it again you'll notice that this time it was much slower when it drew it on the screen so we can add a hash mark in front of it and that'll comment it out so now it's not going to run so let's look at how fast it runs now and then with that speed you can see it's much slower all right perfect so that's the first tip that we're going to have here is using your documentation all professionals use documentation it doesn't mean stealing someone else's code it means referencing how to use other libraries and code awesome so let's do another instance of a turtle so we have one turtle on the screen right now called alex we can actually create another instance of a turtle object so let's call this instance cat so we'll set cat equal to turtle dot turtle and this one let's say cat dot color so let's pass in blue now let's do something similar that we did with alex so let's say that we move cat remember we said we can move backward let's say cat dot backward and we can move it back 50. so you'll notice i'm using uh k-a-t this is my wife's nickname so uh i will use cat like that so i'm saying cat.color is blue and then cat backward 50. so hopefully we see a instance of another turtle on the screen and then it moves backward instead of moving forward awesome so now you'll notice one of the turtles move backwards you notice it's still facing forward and the other one move forward the other way cool so now we have two instances of an object and those are both unique objects so even though they're both turtles they're both unique and you'll notice that we could have called this whatever we want instead of cat we can go back and we can call it bob but now we're hanging references here right because now there's no cat turtle instance so we're going to have to name this bob and then this bob now if we save and run again you'll notice it still works so remember these variables are changeable we we created them we can call it whatever typically in programming you want to make as much sense as possible uh so i probably wouldn't use name references i might have called it turtle one and turtle two or whatever you're doing with them but in this example we're just highlighting that you can use whatever you would like cool so now let's kind of do something neat with both of our turtles let's say that we want to make a square with one of them so with our alex we can move forward 50 turn 90 degrees then we can move 450 and then turn 90 degrees and then we can move forward 50 and turn 90 degrees so what we're trying to do here is we're trying to make a square with our alex turtle we're gonna leave cat alone for right now let's stop this we'll run it again and you'll notice that we've made a square now that was really cool but you might also notice that it's kind of repetitive how we did it you see how we did we had to copy these exact two lines of code over and over one two three four times to make the square that's where we can use loops to make our programs a little more concise and easy to read and understandable and reusable so one of the loops that we can use in python is a for loop so a for loop is going to execute a certain amount of times now typically when i use a for loop i know how many times i want it to execute because either i know exactly how many times i want it to run in this case four or i'm having it iterate over a list or something however many objects there are in that list so in this case we can say or i in range and we can say four because we want it to execute four times now you can see here that there are a couple things that are i don't like that i have eight indents there instead of four let me change that real quick change indents to four i try to make this just a little bit more readable for you guys when i do these okay that looks good so you can see here that after we type this line 4i in range for colon you'll notice that it tabs inside now anything inside of this tab is inside of the for loop you'll notice these other lines are not tabbed inside of the for loop so they're not part of the for loop right now so let's do print i and then we'll talk about what's happening so if we run this again our turtle is going to behave exactly the same but if you look down in our console you'll see that we printed out 0 1 2 and 3. so right here our for loop is executing four times and each time it's assigning a different value to this i variable so this i variable is assigned zero the first time it runs one the first second time it runs to the third time it runs and three the fourth time it runs so in programming a lot of times our accounting starts from zero instead of one which is a little different but the important thing to note here is this happened four times which is exactly what we needed to do to make a square so we can actually take two of these lines and put them inside of our for loop so remember we're going to have to tab them inside of our for loop and now we can delete these other lines and we should be able to make our square so now what's happening we're no longer using our i variable so we're going to do nothing with that nothing's going to show up in the console because we're not printing it out what is going to happen is these two lines are going to execute four times which as we know if we do that it's going to make a square so let's stop this and we'll run it and you'll notice it makes the square just like we did before that's really cool right now you can imagine in a project where you're doing something a thousand times or two thousand or even a million that you can get a lot more advantage than we did from this small improvement but it's going to be very useful to you now one thing you'll sometimes see and you'll notice here is we didn't use i at all in our program right so they'll sometimes replace this with an underscore if you're not actually using that variable for anything so if we run this again nothing would change just something you want to look out for so what else can we do with a for loop well with a for loop we can also iterate over things so say we had a list of names so we could say names equals and then do a square brackets and in here we could give some different names we could say bob we could do cat again jake and bill now if we save this we can now iterate over this so instead of using a range here we can delete that out and we can put in names so now each time we go through this loop it's going to do it one time for every value inside of this list and this time we might want to use it right so maybe we create a variable here to hold each name so this is something that you'll see sometimes is the plural version being the list and then an individual being the name so each time we go over this loop now we can print out the name and if we run this our turtle behaves the same but if we look at our console you can see that each time it prints out bob cat jake and bill sweet so that's pretty cool so it's still executed four times why did it still execute four times because there's four values in our name list okay well how could that be useful in our program let's try and make this a little cooler and remember we want it to execute four times so we're going to keep four things in our list let's call this list colors in here we can use colors from the documentation so we can say blue green red and yellow awesome so now this doesn't exist anymore so we got to use the new variable colors and here we can call this color instead now this is still going to execute four times right one two three four and each time we're going to get a variable assigned so the first run it's going to be blue the second is going to be green the third it's going to be red and the fourth is going to be yellow now each time we do that let's use that to set our color for our turtle so the function name is color and our variable that we're going to use is color so it should change the color of the turtle every time it goes through the loop cool right wow that yellow is really hard to see but you can see that now we changed our color each time we went through the loop so the first one was blue as we expected second green red and then finally yellow so you'll see here how we can do some cool things with for loops in general to make our code simpler or more useful now what would happen if i added another color here try it and find out that's another tip here as we go through this documentation and these programs don't be afraid to try things out look at them and experiment a little bit so if we add in another color we could call it uh let's call it black so now what's gonna happen you might know so our loop is now gonna execute how many times five times right one two three four five and each time it's gonna go forward fifty turn 90 degrees 450 turn 90 degrees so you can see that it's gonna go to where it landed before then turn 90 degrees and it's probably going to draw a black line over top of our blue line here awesome so it happened as expected so my next tip is sometimes programs don't have great documentation so we went to the turtle documentation page and we saw all these cool functions and how to use them and you can experiment and play around here as you go but what if there isn't a good documentation page so what i like to do sometimes is print out some data about the class itself that i'm using if you've watched our other ones you know that we can do type and then pass in something into this type function so let's pass in our reference to alex so this is going to give the class that alex is if we stop and we run this you can see it's a class of a turtle well what if i want to know some objects and methods and different things i can do with that specific type so instead of type here i'm going to use der now when i use der i'm going to see all the attributes and functions that i can use on a turtle class let's run this now you'll notice this is way messier than the documentation but some libraries don't have documentation so sometimes this is what you've got to use but you can see here that we got a lot of different methods on the turtle itself and you'll notice that they're the same as on the documentation it's just a little messier you can see there's a forward and a bunch of other stuff backwards around here turtle turtle size all kinds of different things so that's my second tip is don't be afraid to print out stuff to the screen and actually take a look at what you're using and how to use it awesome so as i said before the documentation that we're following is great well i'm not referencing the book learn python the right way line by line so if you read that you're going to get more out of this that i didn't cover and different things like that and vice versa so it's very it's a good thing to reference both materials but they did have an example in there that i wanted to show you because i thought it was cool so we're going to do that as well so what i'm going to do here is i'm going to highlight all this and i'm going to push control and then slash and it's the slash underneath your question mark so the forward slash so what that's going to do is it's going to comment out all this code so it doesn't run anymore so it doesn't mess up our new program you can see nothing happens now but i also want this to be here if you need it later so this will be here if you would like to uncomment it so we're going to use a couple other methods just to show you how a for loop can be kind of cool so what we're going to do is we're going to create a spiral pattern with our turtle so you can see up above we've already declared our turtle so we got an alex and a bob i'll actually work up here instead so we can see that now with the documentation here we can change the background color of the project itself so we can say wn dot bg color we can set this to whatever we want we can set it to blue will just be different than the book itself and now we're going to do something different we're going to do alex dot pen up so you'll notice a lot of times when we use functions we pass things into it and this one we didn't pass anything into it and you'll notice we get a nice little pop-up in replit from time to time and if you read this you can see pen up it's going to return nothing and we're not passing anything into it and it gives us a nice dock string so it says pull the pen up no drawing when moving okay so that we might already kind of know what's going to happen right it's not going to draw any line as it moves so out of curiosity let's just change that method let's control and then we're going to hit that forward slash again and you'll see now we have the part that we did before but now we've pulled our pen up so if we run this you'll notice it looks really weird right it's not drawing the lines so it's just moving the turtle okay so that's one of the differences here i'm going to comment this out again so we're moving our pen up so we're not going to draw any lines and we're going to also create a variable called size and we're going to set our size equal to 20 and then we're going to use a for loop so we're going to say 4i in range and here we can decide how many times we want to loop through this we're going to say 30. so remember it's going to execute 30 times but it's going to give us values in a range from 0 to 29. now each time we're going to say alex.stamp another method that you can look up in the documentation or you can see what it's gonna do here and then we're gonna say size equals size plus three so each time we're going to increase the amount we're moving here so they actually use size i'm going to say i'm going to call this move so we're going to move 20 we'll set move equal to move plus 3. doing the same thing i'm changing the variable name just because i think it makes more sense to me and then we can do alex dot forward and now we can pass in how much we want it to move forward we have a handy dandy variable here so we're going to say move and then we can say alex dot right this is how much we want him to turn and we can turn 24 degrees so that is a lot here but we're pulling the pen up we're setting our original move distance to 20 and then we're executing this loop 30 times now each time we're increasing our move distance by three we're moving forward that amount and we're turning 24 degrees so let's take a look at what that looks like cool so you can see here that we're making sort of a spiral pattern from that we expand it so now you can see we can kind of play around with this a little bit right so if we wanted to try something different i made my turtle screen too big i had to refresh here i got a better setup now so we could change the values and see how this changes it as well so one thing that i might want to do is maybe i want to change alex's color to be white and i want him to start out moving a little smaller so we'll start it out at 10 and then maybe every time we only want to increase the move size by two okay so we can play around with this code a little bit to make it work differently and you'll see that we got a little different output there so let's review a little bit of what we did today so in today's video we talked about some methods on side of our turtle class so we used an outside library to begin learning about how to use libraries outside of your current code and we did that using the turtle library we also talked about for loops and how they can make our code more concise and do kind of unique things as it goes through the code so instead of maybe hard coding every single line we can make our programs more concise and readable by using for loops now if you're following along with this series i'm going to try and get the next video out quicker so stick with us here but until then make sure that when you're writing this code you're following along you're typing the code and don't be afraid to break stuff if you're like wow why didn't he use i in this for loop for the move value instead of setting a move value each time okay let's delete out that move value and let's use i here instead and run it who cares start seeing what things look that was almost cooler so don't be afraid to try things out do things differently you're not going to break it and until next time keep coding
Info
Channel: ClarityCoders
Views: 795
Rating: undefined out of 5
Keywords:
Id: VLTJEOVG-vw
Channel Id: undefined
Length: 28min 14sec (1694 seconds)
Published: Fri Nov 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.