AI on the Jetson Nano LESSON 5: Introduction to Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Applause] hello guys this is polemic order from top tech boy comm and we are here today with lesson number five in our incredible new tutorial series where you are going to learn artificial intelligence on the Jetson nano what I'm gonna need you to do is pour yourself a nice big mug of iced coffee I'm gonna need you to get out your Jetson nano gear get it booted up and get ready to rhumble no we're not gonna rumble seriously we're not gonna rumble said that to a high school class once it was a mistake they were ready to rumble no we're not gonna Rumble we are going to learn we are going to learn some Python today okay in the first lesson those of you who have been playing along at home you know that we sort of built the nano system and got it to the first boot then and I think in lesson number two and lesson number three what we did was we became comfortable with the Linux command line and the Linux terminal worked our way around the Linux system I believe in lesson number four we learned how to SSH or putty into the Jets and nanos so that we could operate from a remote terminal pretty handy thing to do and then today what we're gonna do is we are going to jump in and start learning some Python now I feel like that if I don't go over a couple of lessons on Python I'm gonna kind of leave some of you behind and you're gonna kind of get in that bad habit of just copying and pasting what I'm doing or just typing in what I'm typing in without really understanding it and so I want you to sort of understand Python so I'm going to do a couple of lessons on this at the same time my goal is not for you to become a Python wizard but just to be able to write simple programs and sort of understand some simple code in Python I am also assuming that you probably came here from the Arduino world and if you came here from the Arduino world you've already written programs and so you understand the concept like an if-statement a four loop a while loop you understand a race you understand things like that and what I'll be teaching you here is sort of like what is the syntax in Python to do some of the stuff that you already know how to do so we're gonna spend a couple of lessons for you guys that are already experts you can just sit back and enjoy the coffee and just watch the train wreck as it unfolds oh it's not a train wreck but you know watch the train wreck as it unfolds and then you know we will be getting to the more advanced stuff a little bit later on but we don't want to leave anyone behind Hey I also want to thank you guys that are helping me out over on patreon if you look down in the description there's a link to my patreon account and what you guys are really helping me by supporting me on patreon is allowing me to upgrade my equipment I hope you are enjoying this marvelous new Yeti microphone that's really improved my sound quality there's just other equipment and gear that I need and so as you guys are able to support me allows me to produce a better product for you I don't know if that's nonsense let's get in and let's start programming some Python so we are gonna switch over to a view where you can see my desktop now you could do this by puttying in or sshe in so you could do this from your Windows desktop and connect remotely as I showed you in lesson number four but I'm gonna operate from the from the Jetson Nano desktop just because I can all right yeah on these introductory Python lessons we're going to be writing Python using a text editor because that's kind of like old-school we're gonna be using Nano which is a text editor and I just think it's neat to be able to write code from just a sort of simple window where we're doing text editing and so we're gonna kind of learn the first few lessons just using the simple Nano text editor and then as we start getting into the more sophisticated programs I will show you how to install a more snazzy IDE an integrated development environment all the code is the same it just has to rules that make encoding a little bit easier but I think everyone should be able to do it old-school to begin with so I need you to call up your terminal window and let's go to our home directory how do we do that CD what easy way to home is squiggly I am now in my home directory let's see what's in there I do believe I better get out of your way and guys I have been this is towards the end of the day and I've been teaching all day which means I talk all day and what that means is by the end of the day after talking all day my voice is a little scratchy so forgive me if I clear my throat I'm not dying I'm not sick it's just I talk all day my voice gets a little tired and I will be hitting the coffee mug a little bit more than normal ok so we are now in our home directory and I let's see I'm gonna do a PWD just to see yep I'm in slash home I'm in slash p JM p JM is my username if you do a PWM you should see slash home slash your username okay so in teaching you Python I don't expect you to perfectly remember every single thing I'm going to show you so I'm going to suggest that we write a series of simple little programs and we give them good names and we put them in a good directory and then as you are coding later if you if you think and how did he do that formatted print you can go back to that program and you can look at the code and it can kind of remind you the syntax of these things so I am going to make a directory how do we make a directory mkdir and then I'm gonna say my Python okay let's do an LS boom there's the folder my Python now you know that based on what I taught you in what was it lesson number two or three I showed you how you could be in one folder and work in another folder understanding the paths but just to make things simple let's CD down into that so we don't have to worry about paths we're gonna CD into my Python okay LS it's empty as we would expect and now what we're going to do is we are going to write our first Python program so how do we open the text editor Nano and now we got to give it a file I mean we got to give it a file name so I'm going to call it Python print never put a space in a file name never put a space in a file name even though Windows encourages Microsoft encourages sloppy habits like that never put a space in a file name I like to use the bumpy fonts you can see Python print the second P is uppercase and that makes it easy to read and then this is a Python program so good habit is to end your Python programs in dot pi okay now we've opened up our text editor so let's see what we can do let's just write some things here I'm going to say x equals seven right off the bat you see something that's different in Python than in Arduino in Python you don't have to declare your variables you want to use X man go for it you don't have to declare it a lot of people like that I don't like it so much because it allows you to kind of get yourself in trouble because you can be like you can miss type a variable and find yourself using a variable that hasn't been declared and so you can kind of I've run into problems with that before so I really like programming languages where you declare your variables but you don't have to in Python so let's say X is equal to seven let's say Y is equal to three let's say Z is equal to X divided by Y now if you look at this because I say x equals seven it's gonna it's gonna define X as an integer because I said Y is equal to three it's gonna divide define Y is an integer because I say Z is equal to X divided by Y that is a float so it's gonna make Z afloat so it's kind of crazy how it's a little loosey-goosey and allows you to sort of Mitch mixture variable types here that's a that's some people like it some people don't like it so much okay let's say my greeting is equal to hello my greeting is equal to hello world now my greeting is going to be a string and it's going to have the value in it of hello world now how did I create the string hello world you create a string by putting it in quotes so my greeting is a variable the contents of that variable my greeting are going to be a string and what is the string going to be it is going to be hello world does that make sense okay let's do our first print command and I am just going to say print and you use parentheses what you want to put you put in parentheses okay I am going to print just in quotes so I'm printing a string I'm not printing a variable I'm printing a string so I'm gonna print hi guys all right hi guys and then I'm gonna enter and then I'm going to write out and run this so how do we write out or how do we save ctrl o and then enter and then ctrl X all right how do we run the program well I guess I should say that let's just do something real quick let's just type tight on to go to the Python interpreter if I just type Python on the jetson nano as its configured with the image that I had you download in Lesson one you can see that python the version is two point seven point one five the default version of python is two point seven point one five on the image that you downloaded and that means if you just type python it's going to run Python 2.7 to get out of the interpreter you hit ctrl D okay Python 2.7 is being deprecated and so what we really want to do is we want to learn Python 3 so let's just bite the bullet and learn Python 3 so how do we run Python 3 will you type I mean I think it's 3.6 but you just type python 3 and then boom you see it's going to run three point six point eight now that's the default version of Python three that is installed on the Jetson Nano image don't go upgrade one of the newer ones because to get this whole enchilada to work we've got to be using the right version of Python with the right version of OpenCV with the right version of numpy and all of this so you really need to be going along with me and if you go off to a different tutorial and they have you upgrade some of these things you could come back to my tutorial and find that you're not operating those 128 cores on the Jetson nano you might have disabled that parallel processing so to keep the parallel processing in the real artificial intelligence horsepower there you need to be very careful to be using the version of programs that I'm showing you to use now I know that some of you are impatient because my lessons are coming out one a week and you want to move ahead what I suggest is format a different card and as you're out playing cowboy and going rogue and coming off the reservation all that stuff use your other SD card and then when you come back here use the clean SD card for this series of lessons I think you would be happy if you did that I think you would see that that would be the smart thing to do okay how do we get out of this Python interpreter ctrl D alright let's go back and look at our program again because I've been pontificating here a little bit so let's look at not Nano Python print top height how am I getting these commands back by hitting the up arrow by hitting the up key on the keyboard I get these old commands so we're just going to print hello guys so I'm going to say ctrl o enter control X how do I run it Python 3 Python 3 and then the program was Python print dot pi alright that's how you run a program boom it says hi guys it printed the string hi guys alright let's go back up up and I get back to nano path and print this time instead of printing hi guys let's let's print in quotes my breeding now if I print my greeting in quotes what's it going to print I want you to think in your mind what gonna print alright control Oh enter control X up up Python 3 it prints the string my greeting let's think about that let's go back to nano ok I told it to print the string my greeting so what did it print the string my greeting if I wanted to print the variable my greeting the variable byte my greeting what would I print I would print my reading without quotes alright ctrl o enter ctrl X up up and run ok and now it printed the variable my greeting which contains the string hello world I hope I am not boring you of this is important stuff to know alright so now if I say print in quotes X what's it going to print ctrl o enter ctrl X it is going to print the string X the character X the string X right what if I wanted to print the variable X ooh up up I would not put quotes I would just say print X okay ctrl o enter ctrl X up up Python 3 it printed 7 which is the number the integer number that is contained in the variable X ok so we've done a little math here what I've said X is equal to 7 y is equal to 3 Z is x over Y and so let's print Z and what I predicted is is that X is an int Y is an int if you did this type of nonsense in Arduino it would give you an intricate to make Z a floating-point number so let's see what happens control oh right out control X up up boom two point three three three three three three five aha floating number let's edit it again alright I ran it again up up nano okay so what if I wanted to print you know let's get a little formatting here you can print a string of things I shouldn't say string because that means something very very specific I can print a sequence of things by putting something comma something come with something so I think that it might be interesting to put to print the string in quotes X space plus y space equals space and then in quotes that's the first it's going to print a string and then comma because we want to separate the parts of the sequence with a comma and then Z okay control o to write out control X to leave up up Python three Python print Oh do you see that X plus y is equal to two point three three now this is something I really this is something that I really like in Python how you can make a nice complicated formatted line with one print statement right in Arduino you would have to say print print you'd have to say print something print the comma print something print the comma and then print L in okay I like to string it all together in one print statement okay what if instead of x plus y what if i wanted to know the values so let's think about that what I could do is I could come here and I could say print the number X and then print the string space plus space and then comma Y and then comma what do I need their space equal space and then comma guys if you have errors in print statements it's usually because you either don't balance your quotes or you forget a comma but I think I think I've done good here so let's do control Oh enter control X and then let's say Python 3 then oh I'm sorry I boomed too soon I didn't had I divided and in fact I divided earlier I hope you guys were screaming at me that was my bad okay so what this actually was we had divided okay we had divided so let's do like that all right control o inter control X okay seven divided by three is two point three three three five all right you're getting a little feel for printing I hope so okay now if you look at that what's the thing that you don't like about it you don't like how it's giving you 27 bazillion decimal points so how would we format this man I could spend six months about formatting in Python but to me the big one is just stop all the digits stop all the precision and like give me two digits so let me show you how to do that so we're gonna come back over here and we're gonna nano it okay now we still print the variable X we still print the string space divided space we still print why we spilt still print the string space equal space and now Z is where the problem was right because it's giving me way too much precision in Z and so this is what I do I put a quote and then I put an open curly and now what I want to tell it is that I want : that I want to format it as a dot too and what that dot too says is give me two characters of precision or two digits of precision to the right of the decimal so it's kind of like a bunch of nonsense if I lost my mouse know it's a bunch of nonsense but what matters is the dot two means two digits of precision to the right of the decimal okay and now we are going to need to close our quotes I mean close our curly bracket close our quotes okay and then dot format and what we're going to format is Z okay so Z man could they have made it any more complicated like I could think of a million better ways to write this but I am NOT one to question why they did it I'm just one to do it this way so basically this is when I tell you to save this program so if you ever need to format it you can come back what you're saying is open quote open curly colon dot - I want two digits of precision and then I want you to apply that format to Z ctrl o to write out control X up up and then away we go boom man that is not good seven divided by three is two point three what do I not understand about this I have made some sort of grievious error okay let's see okay believe it or not there's a 2f okay so I say open quote open curly colon dot 2f close curly close quote dot format Z and so just understand that's what you do ctrl o enter ctrl X I'm not going to try to give you an explanation about what that means okay let's go to degrees two digits of precision let's just show you that this really works we'll come back and you can imagine if you want four digits of precision it would be for F control o enter control X boom all right so now you know how to print things that's enough to get in the game right you can print a formatted line and you can control the precision of the numbers I think that's a pretty good pretty good start okay save this program so that you can use it in the future right and so I'm going to start with a new program so I'm going to say nano and so kind of like one of the fundamental things that you want to do is you want to be able to print one of the other fundamental things that you want to be able to do is to get user input so we're going to say user input okay user input dot py because we're gonna be good programmers and now what I'm going to do is how do you get things from the Hat what is this nonsense okay how do you get input from the user from the keyboard like you know in Arduino we do three things we ask we wait we read well Python is a little easier really you kind of just need to do one thing and so let's say X is equal to and what you say is input okay and then what you say is I quotes please please it's your prompt now please oh man okay please enter first number okay I think it's good practice to always put a space so that when you answer it it's not jammed up against your last letter so I'm going to put a space okay so X is equal to input and please enter your first number let's say Y is equal to input you guessed it please enter sec number okay : space this is our prompt that we're going to give the user end it end it okay then let's say Z is equal to X plus y alright and then let's say print and then I'm going to use some nice formatting here so I will say x equals close the quote comma X so let's just see if we can get input from the user control o enter control X to get out now this time we are going to python 3 and it is going to be user input dot pi okay please enter your first number 3 please enter your second number 2 and boom it says X equal 3 so we're getting input from the user and we're printing it out okay so let's nano this again okay so now let's say print X let's say now Z is X plus y so let's say X plus y equal I'm going to kind of do the shortcut here instead of printing each individual number I'll just print the string and then this time that would be Z right because Z is X plus y so we're going to ctrl o enter or ctrl X and we're going to run this puppy Python 3 x + o python 3 user interrupts hi okay oh no ok I'm sorry this is working so first number is 3 ok second number is 2 3 and 2 and it says 3 + 2 is 32 houston we have a problem something has gone wrong so let's look at this ok let's edit it okay this is what you just learned what you have to see is if you use the input command it reads what you type as a string so if I say X is equal to user if I say X is equal to input and I put in a three it doesn't take in the number three it takes in the string the character three and it makes X a string and then the same thing with Y is a string and in string land the plus is concatenate so it adds one string to the end of the other string so it's it puts the two strings together as opposed to adding the numbers so what we have to do is if we are inputting a string we just say input if we want to input a number we have to float it FL oh 80 and then open in the parentheses and then we've got to close them out here so you kind of start at the inside and what it's doing is it's giving the prompt and then it's inputting from the keyboard a string and then it's converting that string to a float and it's putting that float into X and it's making X a float because on the outside of the float what's coming in is a float now we need to do the same thing here and so you would be surprised how much grief this causes when people forget to do this okay so we're gonna float this and then we are going to come over and now Z is X plus y so let's see control o enter control X let's try it again ladies and gentlemen really it's more gentlemen I was looking at our demographics for this channel we're like 97% male and 3% female and we don't even know if the three percent female are really female it might just be some guy over at his girlfriend's house or what are using her computer we have a demographic problem on this channel I will tell you okay enter the first number three do you enter the second number two boom x plus y is equal to five and notice it said 5.0 why because we floated it so it made X 3 zero it made wife 2.0 and then the sum is five so I think that is pretty cool you feel like you're learning a lot here man we've learned to print we've learned to format print learned a little bit about how variables worked and now we have user input where do we want to go now I think we're okay let's save this program because you're gonna need to remember how to do the input now the float what the syntax is so let's save this let's look at what we have we have Python print we have user input I think those are pretty good names right so I think what we want to do now is if statements okay so let's do a Python if dot PI I guess I should nano it nano and then Python if dot py all right so let's let me think here what I'm going to do is let's just get a number from the user so number is equal to how do I get it from the user I want to float it because it's a number and then I want to input and then I want to say please input your number okay space and then close the first parenthesis or close the inside parenthesis close the outside parenthesis now this is how you do an if statement in Python if little if little I F space now in parenthesis you want to put your conditional the easy conditionals are less than greater than less than or equal to greater than or equal to okay and not is exclamation so if you want to say not equal its exclamation equal but I don't use that one very much so let's just say if the number if number is greater than zero so that's our condition our if our condition now you start the clause with a coal one so if number is greater than zero then you execute this clause that comes after the : now you remember in Arduino you would have an open curly and a closed curly and everything between open curly and closed curly were part of that if clause Python doesn't do it that way Python does it hit enter yeah let's see hit enter Python does it with indentation so you have to tab over and everything that is underneath that tab that's tabbed over and it's got to be tabbed over exactly the same not a character less not a character more you have to keep perfect vertical alignment and everything in that perfect vertical alignment is going to is going to be part of the if statement so what I can do I'm going to backspace one tab just to make sure I am over one tab I'm going to say if number is greater than zero then I'm gonna say print and then I'm going to say your number was positive okay and then I'm going to tab over and say print thank thank you for playing I'm just doing this so that you can see everything that is indented everything that is tabbed over will be part of the if statement okay now I'm not indenting so now I have left the if statement so I'm now I'm going to say if and then number is less than zero less than zero then I am going to colon to start my if Clause tab print your number was or your number was negative I guess I should say is your number is his Wallace sounds like it used to be negative all right okay Wow that better end the quote and in the parentheses okay and up here we'll make it will be will be good here and make this one is as well okay your number is positive thank you for playing and then you know guys I can drag over this and I can rap right mouse click and copy and then I can come down here using the arrow down arrow down and then tab and then right mouse click will paste in Nano okay write masculine peso thank you for playing now I can say if number equal equal to equal signs you will not believe how much grief and pain I've seen in programming because it looks so right if he looks use one equal and it is so wrong this is a mistake that drives people into like business school or you know some other part of engineering making a mistake like this because it's very very hard to find and people just get frustrated and give up so if it's equal to zero then we're just going to say tab over print your number is zero okay I'm not going to argue with people about whether oh is positive or negative you know people will argue that those things whether zero is even or odd or positive or negative I don't get into such it arguments and then we should be able to still have that on our clipboard so right mouse click paste okay I actually think this will run you notice how we haven't even been holding our breath like we used to and you know Arduino ok control o inter control X and now we are going to run that and I'll just back up here in say Python 3 because I think this is the first time for the Python if ok please enter your first number 3 your numbers positive boom your pop number is positive let's play again up up yeah went up to my Python 3 Python if how about negative 3 Oh your number is negative let's go up up OK or let's go up Python 3 let's put in 0 boom your number 0 ok so it's really if you can do something is greater than something something is less than something you could also imagine that you could go the other thing just just so you see you could say that it is greater than or equal you could say less than or equal or you could say equal equal or you could say not equal okay so those are the big ones right greater than less than greater than or equal to less than or equal to and not equal to those are the big ones alright so now let's take out these and make it back the way it was ok so now the only other thing is you need to know how to do and and you need to know how to do or because you might want a compound conditional so let's say your number is greater than 0 and your number hmm your number this is a dangerous one to do but number mod 2 so that's remainder if you mod it by 2 that's your number divided by 2 and what's the remainder if number mod 2 is equal equal 0 the reason this is dangerous is because number is a float and sometimes floats have round all fair so we'll try it if it doesn't work we'll go back and make it huh we'll go back and make it an integer okay so if number is greater than zero and number mod two equal equals zero well if number mod two equal equals zero that means when you divide it by two the remainder is zero what would that mean that would mean your number is even so then we're going to say print and your number is oh my goodness what did I do you know what happens right where I expect the shift key to be there's a print screen' thing this is kind of a doofus keyboard and it really makes me very angry when that happens okay so your number is positive and your number is even okay and then we'll close the quotes we'll close the parentheses okay then here what we need to do probably is take all of this so yeah you can use the mouse for this and I'm going to come like this and I'm going to say copy and then I'm going to come here and I'm going to paste I'm going to come here right I got to go up with the arrow keys so I'm going to come here and I'm going to route right mouse click and I am going to paste okay so if number is greater than zero and number mod 2 is zero then it's even if it is not zero so all this nonsense I'll come here and change this to not zero if number is greater than zero and number divided by 2 is not 0 then your number if there's a remainder your number is odd ok now we come down and say if number is less than zero and number maude to equal equal zero then we're going to also have it even so I'm going to speed things up by coming up here and copying this and then coming here staying tabbed over okay and then right mouse click and paste so your number is negative and even and I we would also need to take all of this and do the other case which would be we will take all of this I'm wondering yeah I'll just take all that copy and then we're going to come down and then right mouse click and paste and in this case if the number is less than zero and the number mod two the number mod 2 is not equal to zero then again this is odd and then if the number is zero your number is zero okay and I will put tab print i/o pinyon open opinion is zero is even I think people will probably argue with that some people will find an argument with that but I need to close my quote all right man let's try to run this this is kind of scary control Oh enter control x nath nano python ooh not nano control X okay we want a Python three Python if please enter your number two okay your number is positive and your number is even let's try three okay Oh got a run it again I'm sorry okay let's try three your number is positive and your number is odd thank you for playing okay that's pretty good so let's go ah Python three let's put zero okay my opinion is zero is even okay that's good let's go up and say Python three Python if let's say minus one ma your number is negative your number is odd okay let's go Python three Python if let's say minus 240 to thirty-two like that let's see what happens your number is negative your numbers even hey that is pretty cool let's try one more okay let's try one more let's do a big minus two for two three three negative or not okay gasps man we really learned a lot here right that we we've got to gone through the conditionals right we've done greater than less than we've done not and we done ands and ORS okay now I guess I haven't done an or but it's just to do an or you use the word or remember in Arduino and and was the and symbol what is that like shift seven it was shift seven shift seven it was the and and was and in the bar bar which is like a shift slash above most in turkeys the shift slash was the the bar bar was the or okay I kind of I kind of like and and or better so there are things that I like about there are things that I like about Python okay I hope you guys aren't getting tired but I don't want to do twenty lessons on this so I'm gonna keep going if you're okay if you're getting tired pause and then come back and finish the lessons but there's a couple more things I want to get through the four loops and the file loops in the arrays okay let's move on so we are going to come here and we are going to control X out of this and now we are going to do a four all right we are going to do a for loop so I'm going to say Nano and I'm going to save Python for dot pi no no no never put a space in a file name okay so we are going to use our bumpy font for Python four okay so this is the way you do a for loop I'm just going to say four and then I is my index notice I don't have to declare I when it sees I was when Python sees idle just use it for I in and then there is a command that you're not familiar with probably and it is called range for I in range and now what we do is we say go from one that's the starting number to 11 and then go in steps of one so for I in range 1 to 11 and then in steps of 1 alright what starts the for loop a semicolon and also you tab okay the for loop you're in the for loop all the commands that are tabbed will be in the for loop so I'm going to say print I okay and then just to show you I'm going to say print that that's all folks like that like that okay so you start with one you end with 11 and then you go in increments of 1 so now what's control o enter control X and now let's Python oh man I better do a Python 3 here what's going on Python I hate that key ok python 3 and then Python fo r dot pi ok and look at that it says one two three four five six seven eight nine ten a little bit not what we expected right so let's go back and nano it and this is the peculiar thing and actually when we get to a race you'll see there's a reason for it but if you say go from 1 to 11 it loops as long as your index is less than 11 so it will go 1 2 3 4 5 6 7 8 9 10 and 10 is the last one through so it stops one before 11 okay that'll make sense later but it's a little bit annoying to me okay it is a little bit annoying to me but we wrote our first for loop and you see that the print that's all folks that was inside the for loop why because it was indented so I'm going to take the indent out ctrl o enter ctrl ctrl X and let's run it now okay now it goes 1 2 3 4 5 6 7 8 9 10 and then that's all folks okay so it stops one before your end number keep that in mind because that causes a lot of confusion that causes a lot of confusion so let's come up here and let's say well what if I wanted to do a let's go from 1 to 11 in point 1 steps in point in steps of 0.1 let's go ctrl o enter ctrl X up up and let's run it oh it crashed it said float object cannot be interpreted as an integer so what is that saying that that range command is expecting that range command is expecting an integer so you can't do a for loop in Python using range and use integers and man sometimes I like integers I want to go like if I'm calculating a curve I want to go like from 0 to 2 pi and steps 0.01 in this does not like it let me show you a way that you can fix that ok so we're going to nano it and now we're gonna import our first library okay we are going to import a library see we're learning things as we go here so we're going to say m port the library is a math library called num pi and you can sort of say like numbers in Python if you really want to work with numbers you use numpy and will still do the for I in range but now what we are going to do is we are going and also it's it's okay to put blank lines it makes it a little easier to read so we're gonna go for I in in P oh I should have done this okay import numpy every time you use it you would have to type in numpy dot this and numpy dot that so I would say import numpy as in P now if I want to use numpy I just type in NP so now for I in NP that's the library what's the function in NP a range okay and I want to go from let's say minus 0.5 to 0.5 in steps of 0.0 0.1 okay and now I will close that and I will go like this and then I will say tab print I and then outside I will say print that's all folks and the quote in the parenthesis ctrl X yeah I accidentally said ctrl X I'll say a yes save okay in and enter now let's run that whoa oh I still had the error from earlier okay so I need to change this back to one because we know that doesn't work ctrl o enter ctrl X up up and run oh wow ooh okay I don't have oh man I don't have numpy on this thing I do not have numpy on this thing okay I'll come back later and I'll tell you about numpy let's just do the for loop now because probably let me just see you see I thought it came with it but it might not okay I cheated a little bit and I ran back and I went back and I ran Python 2.7 and you can see that Python 2.7 has associated with it numpy and so later on I will come in and I will install it but let's cheat a little bit in your shoes Python 2.7 just so you can see how to use numpy on Python 3 numpy will be the same we just have to install it under Python 3 we'll worry about that later but let's just run Python for now so look at that it went 1 2 3 4 5 6 7 8 9 10 ok then it went minus 0.5 you notice the big number minus 1 point 1 e to the minus 16 that's because it's a floating point and the floating point that's like not quite exactly zero because of the roundoff errors in the math but you can see 10 to the minus 16 is practically zero I went from minus 0.5 to point 4 so what you can see is happening is that we're getting that same nonsense so what do we like about in P dot or range what do we like about in P dot arrange more than we like about just play no range is we can do floating-point numbers we can go from point 1 to point 5 but if you look at it it's doing that same nonsense and it's stopping one before the one we want and so if we wanted to go from minus 0.5 to 0.5 we would come and say ok go from minus 0.5 to 0.6 and then the last one it would do would be 0.5 so control Oh mmm inner control X up up and boom so we go from minus 0.5 to 0.5 we go from 1 to 10 so you see that we can do a for loop with integers and we can do a for loop with flow okay that is pretty cool but I will be honest with you and say that I don't like this trying to figure out if I want to stop at ten I want to stop at ten I don't like this nonsense of having to go one more and then stop stop the one before it because that causes me confusion so I'll show you another one and this is my go-to for loop this is a little bit of a pro tip here not really but it's something I really like and for the way I'm doing math like you know this is the way I think I want to go from 0 to 2 pi and I want a hundred steps I don't want to think about like I think I'll get a nice smooth sine wave if I put a hundred steps between zero and 2pi I want the first one to be zero I want the last one to be 2pi not one before 2pi I want to go from 0 to 2pi and I want a hundred steps so let me show you how to do that so let's run it I mean let's edit it and let's do another for loop and what this is going to be is for I in who's your friend numpy imported is NP for I in NP dot and this is a strange thing but linspace kind of like linear space I want to start at one and I want to go to ten and I want 25 steps okay so the first number is where you start the second number is where you end and then how many steps do you want and so I will then say oh I forget got my semi color or my colon and then tab and then I'll say print I all right and then what we are going to do is print that print that's all folks and quote in okay now let's go control X I forgot the control oh but if I say Y for yes it will save it okay now remember we're going to do Python because it's got the the numpy with it and we will add numpy to Python 3 later but we will run it with Python okay and so let's look at this oops let's look at this so we did my we did 1 to 10 that's expected minus 0.5 to 0.5 we made that work because we went to 0.6 but then just flat out from 1 to 10 boom it included that as the endpoint so that's usually how write a for loop the first number the last number and how many points I want that is what works for me ok man that was for loops we are cruising through this I think this is probably turned out to be a long lessons but I hope you understand we have two more things that we want to do and the next is a while loop okay so let's look and see what we've done over here LS and we have done Python for Python if Python print and user input so let's do a Python Wow so I'm gonna say Nano and Python while dot py okay so what I'm gonna do this is how you do a while loop I could just say let me do a simple one okay let me do a simple one while while one equal equal one right now if when does while when does one equal equal one one always equals equals one right you could also say like while true and that would loop forever but you know how in like Arduino you have the void loop that loops forever you don't have that in Python so if you want to just keep looping you can say while 1 equal 1 and then it will loop forever all right and so I'm going to now say count CNT is equal to float and then input and then ah what do you want to count to all right like that how do you start no this is just an input I'm sorry and then I got to close that other parentheses and then I'm going to say J is equal to 1 and then I'm going to put another while loop I've got a indent over because I want to be inside the first loop so this is a nested while loop so I'm going to indent over so I'm inside the first while loop and I'm going to put another while loop while J is less than or equal to C in J is less than or equal to C NT and what do I do to start the clause I'll put a colon now watch this you've got a tab twice because now this is the inner while loop and the inner while loop is now indented twice and it keeps going like that if you were to put an if statement then you would have to end it again ok so I'm going to say print and I am going to use the fancy little thing we learned earlier I'm going to say : dot 2f and then in my quote and then I didn't do my open curly so open curly and then I am going to close my curly and then close my quote so open quote open curly colon dot 2f close okay and then dot format and then J okay close my format close my print okay now I'm going to say J is equal to J plus 1 why because each time through I want J to increment and you know in a for loop it does the incrementing for you if you're gonna do a while loop you have to manually do the increment but I'll be honest with you I like while loops because man I could say you know as long as J is less than 10 keep looking keep looping and then I could say J is equal to J plus point one and so I don't have that goofy problem with integers and having to use numpy but I digress okay and now I'm going to come over one and say print and then I'm going to say that's all all folks are right like that okay boom we're gonna go ctrl o X ctrl X okay now we are going to we can Python three this one we can pass on through this bad boy because we didn't use numpy oh yes what is that nonsense oh did I run the wrong one I think I ran the wrong one that scared me when I got that error I'd run the old program okay there you go what do you want to count to I want to count to three one two three that's all folks what do you want to count to 6 1 2 3 4 5 6 ok you know what is really annoying me do you see when I type something there's no space see you really we want to use better formatting than that so we can control C to break out of the program up up also did you notice that outside while loop it was looping forever and so that was what we were trying to do okay so you notice that out that first while loop pile 1 equal 1 you see that kept running the program over and over we'll talk later in a later lesson about how to gracefully exit the Python loop without having to you kind of crash it I think with the ctrl C okay so what were we doing here okay so J is J plus 1 print that's all folks this looks pretty darn good oh I remember it was the formatting I didn't like what do you want to count to I'm going to put a coal in this space so now ctrl o enter control X up up in then Python 3 Python while I want to count to 7 okay I want to count to 12 ok I want to count to 23 boom you see that okay man we have done print we've done user input we've learned about variables we've learned about Python for we have learned about while loops we are gonna keep going one more thing Scott has one more thing then I'll give you a homework assignment okay one more thing I'll give you a homework assignment okay so we're gonna come back over here and to get out of this thing we're gonna crash it with controls see that's really like a very ugly way to get out of a Python program but that's what we're gonna do right now okay so now we are going to do Nano and I'm going to show you about Python arrays so Python and then array dot py okay so this is how you make an array in Python and this is a little bit different than Arduino there's several things that are driven different if you want to make an array you do it this way like I am going to make it grade array and then I'm going to say it's even to open bracket close bracket so I say the variable grade array and then it's Python wants to know what it is so I just say it's equal to open bracket close bracket and that would make it you know that would make the array that would create the array all right now let's just try something now let's say how do I put numbers into it well this is the goofy part okay this is the goofy part so what I would do is I would say grade like in Python I mean in Arduino you would think oh well I would just now say great array open parenthesis three closed parenthesis equal to seven and that would put seven into the third slot or actually it's a four slot since it starts at zero the fourth slot of the array but you can't do that you've got to build it sort of one at a time in Python so grade a ray dot append and then I'm going to append let's say five point five okay and then I'm gonna say grade Ray dot append and I'm going to say three point two and then I'm gonna say grade Ray dot append I'm gonna say minus two point seven and I hate it when I live dangerously but I'm gonna do this even though I'm gonna regret it I'm gonna just see if I can print grade array and I am not sure if I have to put some opens and close on it or something let's just live dangerously control X enter yes I want to save it okay so now I'm going to Python 3 this I'll probably get an error no ok it works so what did it do it printed the whole agreat the whole array the first one was five point five the second was three point two and the third one was three point seven so now let's let's edit it and what I want you to see is I want you to see that I could also print now the index so I could now print let's say the first one okay but if I say print grade array one what do you think it's gonna print and think about it okay so now ctrl o enter control X okay so let's go up up what's it gonna print ah three point two a little bit of a surprise right you might have said well the first one is five point five no that's the zeroth one the index starts at zero so the that array zero would have been five point five that array one is three point two in that array 2 is minus two point seven does that make sense let's go back and look at it okay so grade array one is the one element which comes after the zeroth element okay now what I want you to see though is I could come in now that I have created it now that I populated the array I could say grade array and then notice that you use square brackets not parentheses okay the array indexes in square brackets I think that's different than Arduino okay grade array one which was 3.2 I'm gonna say now is equal to 255 0.5 okay control Oh enter control X up up to run okay CeCe after you populate the array after you populate the array using a pin then you can come in and change the things that are there but you can't go beyond what you populated you can only put in what you have populated I know there's other ways to do this I know that you can do things that will create a great big empty array but I'm showing you one way to do it that will work and I admit there's other ways but I'm trying to get through this lesson so I'm going to show you every way to populate it right I want to show you one way that works I would rather you know how to do one way that sort of be familiar with five different ways okay so now we are going to nano this okay so you can sort of see how that works but what I'm going to do now is I am going to get rid of this nonsense hopefully that sort of showed you how it would work and what we're going to do is we are going to input some grades okay so what I'm going to say is num grades is equal to it not float I want this to be an int and then I'm going to input and then how many how many grades do you have question mark it went up oh I hate it when that happens okay have question mark space in quote in parentheses in the nth parentheses okay now for I in range okay where do I want to start I want to start at zero and then I want to go to num grades now you might say oh you need to go to num grades plus one no because if I start at zero num grades is one before does that make sense so since I'm starting at zero I don't have to go to number eights plus one I'll go to num grades and then one less than num grades is really where I want to be stop and pause and think about that make sure that you understand it if I'd started at one I would need to add one to number eights okay now one man that's going to confuse you guys but stop and think about it we're starting at zero so we have one extra one so I don't have to add one to num grades okay for I in range zero to num grades comma one and then I'm going to say tab over because I'm in the for loop grade dot or grade array how do I get it in there dot append I got to read it first hold on so now it's going to read it so I'm going to say grade the specific grade is equal to float input input the grade beause it close it okay so I read it into this holder variable grade okay does that make sense so great I'm going to get it from the user and then I'm going to come down here and then grade array to grade array I'm going to append what am I going to append the grade that I just read all right okay now I'm going to say for I in range from 0 comma num grades comma 1 I'm going to tab oh I forgot my colon okay and now I'm going to tab and I am going to say print let's get fancy here I'm gonna say print I I'm gonna say your your and then comma I that's a comma I comma space grade is space and then comma and then it is going to be what grade array and this time we're going to use the index I so this should the first for loop should step through and populate the array and the second for loop should step through and print the array okay and now I will close that and now I will come down and I will print I will print thank you thank you for playing okay control o inter control X up up Python three Python a raise okay what what is this nonsense denied okay what did I do grade a right what did it not like I said grade array grade array do you guys see it grade array dot append grade grade array man that looks so right to me that looks so right to me great array dot append great in grade is float input great array is defined man that is a hard one to find because I was thinking you know what I was thinking I was thinking that somehow I had not done the append right but what did we do did you guys see it we didn't put that and so that was a goofy error message which was hard to find so that is the problem with live programming control o entered control X we go up up and now how many grades do you have I have three okay input the grade 2396 and 45 okay and your zeroth grade is 23 your first grade is 96 and your second grade is 45 okay what are you not like you don't like that I didn't leave a space on the second input so let's fix that all right right when we were in inputting down here we got a little sloppy it says input the grade and I'll put a colon in a space and that'll look better and also like as programmers we start counting with zero in the real world you start counting with one so if you tell somebody your zeroth grade is something that's gonna goof them up so what we're gonna do is we're going to come down and say your I plus 1 great so we're still working in eyes and starting in zeros but when we interact with the common folk we need to show them the courtesy of telling them their first grade is their first grade not their zeroth grade so control Oh control of control Oh enter control X up up Python 3 how many grades do you have 3 I think you guys have done good 99 197 ok boom your one grade is 99 years to grade is 100 year 3 grade is 97 you could even get fancy and if it's the first one put one and then an ST and that if it's the second one put to put an ND and if it's the third one an RD and then I think this six seventh I think you get to the point then that you can just put a th on it so that that's a little nice formatting that you guys could do Wow we've done a lot okay this is your homework number one write a program that will input a list of grades okay input your grade so you got a first of all if you're going to input grades you got to input how many grades do you have that I want you to input the grades then I want you to print the grades then I want you to Everage the grades and then I want you to print the high grade and the low grade guys I know you can do these a bunch of them kind of together in a smaller number of four loops but just for clarity of your program first for loop is going to input the grades second for loop is going to print the grades third for loop is going to average the grades and then the fourth for loop is going to go in and figure out high grade in low grade and then you're gonna print high grade in low grade yes you have homework okay you have homework and if you can do that it'll get you pretty comfortable with Python it'll get you pretty familiar with Python okay men and I can say men because our demographics don't include women how do we get some girls how do we get some women on this channel man what does it take to get women into programming is it that I'm a grumpy old man or something you know but I don't understand how do we get women into programming I think that women are a lot smarter than men they're a lot more organized than mentor a lot neater than men I think they would be a lot better programmers than we guys are but for some reason we are not getting any women on this channel okay guys again appreciate you guys that are helping me out over on patreon you guys give me a thumbs up if you give me a thumbs up also subscribe to the channel and share this with people let's try to get more people involved in this most excellent artificial intelligence on the Jetson Nano okay this is palma quarter from top tech boy calm I am going to talk to you guys later
Info
Channel: Paul McWhorter
Views: 25,995
Rating: undefined out of 5
Keywords: Jetson Nano, AI, Artificial Intelligence, Python
Id: _MEEjko2lhA
Channel Id: undefined
Length: 76min 39sec (4599 seconds)
Published: Sat Sep 28 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.