Raspberry Pi LESSON 3: Python Essentials in One Session

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter with top tech boy.com and we're here today with episode number three in our incredible new tutorial series where you're learning how to make your raspberry pi sing and dance what i'm going to need you to do is pour yourself a nice tall glass of ice cold coffee that would be straight up black coffee poured over ice no sugar no sweeteners none needed and as you're pouring your coffee as always i want to give a shout out to our friends over at sun founder sun founder is actually supporting or sponsoring this series of lessons and we will be using their most excellent sun founder ultimate raspberry pi kit and so what i need you to do is i need you guys to look down in the description there is a link over to amazon where you can pick this kit up and believe me your life will be easier and my life will be easier if we are working on the same hardware and so the first few lessons in this uh series of classes we focused on laying the foundation for the raspberry pi but next week we are actually cracking open this kit and are going to start using the components in it so now would be the most excellent time to go ahead and order your kit but enough of this shameless advertising let's jump in and talk about what i am going to teach you today well let's talk about the first two lessons if you remember in lesson number one i showed you how to get to first boot on your raspberry pi and then showed you how to make some modifications to the desktop so that it's not quite as lame as what it comes right out of the box and so made some improvements to make it a little bit more visible a little bit easier to use made it a one-click machine adjusted some fonts and just made it a little bit easier to use then in lesson number two you learned the essentials of linux because if we're going to go in and start working with those gpio pins and start coding things to make components work with those gpio pins you're going to need to know your way around the linux terminal if you haven't taken that lesson make sure to go back and take lesson number two and then today lesson number three in one session we are going to learn the essentials of python and so my goal is is to just sit down and learn enough about python that you can make it through the rest of this class so yes this is a pretty important lesson if you already have taken some of my python classes and sort of know your way around python hey still watch this video maybe look in the comments or look in the chat be a resource for some of the new guys who have not done python before okay sounds good so what i'm going to need you to do now is i'm going to need you to fire up i'm going to need you to fire up your raspberry pi and if you guys see me kind of looking confused every once in a while i've got two mouses and it's very hard to operate with two mouses let me show you my studio here i'll give you a quick studio view i'll get out of your way but if you see here i have two mice you're having trouble seeing them aren't you i have two mice this one operates the raspberry pi and this one operates my studio and you can imagine trying to do use two different mouse devices you end up always reaching out for the last one but i have digressed let's get the right mouse and let's come back over here and let's go to our raspberry pi and you can see i have mine here running live and then what i'm going to need you to do is i'm going to need you to come down here and fire up your linux terminal fire up your linux terminal like that boom we have ours open and ready to go okay now the first thing that we're going to learn about python is we're going to kind of compare it to how it's different than arduino because a lot of you guys have used arduino and arduino uses a compiled almost c type language and the way those work is you write all your code you write all of your code and then you compile it all at once and then you end up with this machine code that is just a bunch of zeros and ones and with that machine code you put that down in the arduino and then it just executes that compile code now a lot of times i have people on the arduino saying oh i downloaded it now i've lost the program how can i get the program back out i'm sorry the program doesn't exist anymore your program doesn't exist because you don't send your program down into the arduino you compile it into machine code and you send the machine code down into the arduino and if you didn't save it on your pc or on your ide then it is gone okay so you write the program you compile it and then you run the compile code very different for python python is an interpreted language which means it takes the commands one line at a time and then makes the machine code and then executes it but every time you run it it compiles it now the downside of that is comp interpreted languages like python are always going to be a lot slower than compiled languages like c but a lot of times they're a lot easier to debug they're a lot friendlier to work with and so if speed is not your major issue your major concern then languages like python are very nice now python is what is called execution ready it means when you go into the python interpreter it is ready for a command and it's ready to execute that command it is execution ready you don't have to compile anything okay so we're sitting here now on our terminal how do we get to python well it's pretty easy you just type python like that and boom you can see that it fires up a python shell you've got those three little greater than signs and that means python is waiting for a command but wait look at that it tells us that we are running python 2.7.16 now python 2.7 is deprecated and we really need to be on python3 okay i don't even know why raspberry pi is still delivering python 2.7 but python 2.7 is the default and so if you just type python you're going to get python 2.7 and you could get some unexpected results and so first thing that you need to know is raspberry pi as of the making of this video comes with python 2.7 and i think python 3.6 and you want to be on python 3.6 now we're going to learn something how do you get out of the python shell when it is in the terminal you do quit open close like that and boom you're now back to the dollar sign which is as you know from last week the linux command prompt it is now ready linux is ready to do what you want so now we're going to type python 3 and this will run the 3.6 version of python we click enter and boom i lied 3.7.3 i thought it was 3.6 but i guess now they're delivering 3.7.3 by the time you take this class it might be something else but it is ready for a command okay it is ready for you to tell it to do something so let's just jump in and kind of start learning uh learning python from the shell here so we could do something like create a variable and assign a value to it x equals 7 and then we could say something like y equal 2. well now i could say print x and you can see it is 7 or i could print y and you can see that it is two now normally to show variables you would say print but to make things go a little bit faster operating from the command line what i'm going to do is just say like if i just type x and enter it'll print it or y and enter and it will print it okay now one thing that you'll see here is with python you don't have to declare your variables i could just say my car is equal to a toyota okay and then i say my my car my car in boom it knows it's a toyota so it decided that my car should be a string because i set it equal to a string it decided that x should be an integer because i set it equal to 7 and it decided that y should be an integer because i set it to 2 okay but you could also see something and this is what's really important is because you think well i don't have to declare my variables anymore that's true but you got to be very very careful because you can get in a lot of trouble when you're assuming that your variable type is one type and python has made it something else and then there's just all types of very very difficult things that can happen when that happens okay but let's go along and let's play with this a little more so let's say x is equal to three and let's say y is equal to two and now let's say z is equal to x plus y okay and now let's see what c is all right well z is five it figured that x is an integer y is an integer we're adding two integers let's make it an integer all right and so it made it 5. well what if i said x is 2.0 and then i said y is equal to 3.0 3.0 and then z is equal to x plus y now when i put the point on there i'm making it a float it's like in the math world i'm making it a real number you know not the rounded off numbers like integers but make it a real number the in between numbers and so when i put the point zero it is sort of forced python to make x a uh a float so now if i say x it's a float if i say y it's a float what do you think z is going to be okay now z is a float because x was a float and y was a float so matt sorter makes sense it sort of makes sense so far but what if i said x is equal to 2 and i said y is equal to 3.0 okay and then i say z is equal to x plus y and now i say z what do you think it's going to make z because x was an nt and y was a float what's c going to be well it made it a float okay now that you wouldn't just absolutely positively know that for sure after you think about it and say well since it's since it has a float in there it better make the whole thing a float because like then if if uh if y was 3.1 then the answer would have to be able to take the point one so we better make the whole thing a float okay but you see python sort of decides what to do and sometimes it does surprise you okay so you can see that we added those things also like like like let's look at this what if i said x is 3 and i said y is equal to 2 and then i said z is equal to x divided by y in a lot of languages that would make z 1 because z would be an integer because x was an integer and y was an integer and an integer divided by an integer what it would make that it would make it an integer and instead of being 1.5 it would make it 1 because it would round it but you see even though x is an integer and y is an integer python makes x divided by y it makes it a float and so you get the 1.5 if you sort of if you sort of see how that goes okay we can also come in and like i could say my fruit and my fruit could be equal to banana like that and now i could say what is my fruit and it is the string banana okay it is the string banana and so you see we've looked at variable types of integers we've looked at variable types of floats and now we've looked at variable types of strings strings of characters and so you use strings to contain words or anything else that are characters so those are really your main ones now there's all types of other variable types but the main ones you're going to use are ins floats and strings ants are the best to use if you're counting like one two three four five if it's going to be a round number use an int it's more efficient if it might ever be an in-between number then you need to use a float okay you need to make it a float and so let's think about grades like if you're averaging grades well if you had if you're averaging grades you could get a 69.5 okay so you better make grades floats but if you are reading something from a digital port and it's reading a number between 0 and 1023 when you read a uh you know read from a gpio pin or read from a digital pin in arduino that is going to be an integer and you better make it an integer okay so what you want to do is you want to kind of keep track of your variable types and use the right type then strings are for any things that are not numbers anything not a number you would make it a string if that makes sense okay now also i don't know if i can do this let's see if i can clear i know i can clear but no you can't clear there i think i'm going to do this quit and then i am going to clear and then i'm going to go to python 3 just because i want to kind of start back at the top of the screen i'm sure there's a way to clear it in python i just don't know what it is at the top of my head so we have ants we have floats and we have strings now the other really really big one and we're going to be doing this throughout all of these lessons you need to understand arrays and an array is a list or a grouping of numbers so now what i could do is let's make an array i could say x is equal to a group or a list of numbers and i start the array with an open bracket and i could say i have the numbers 5 4 3 2 1. okay and so in this case what i have is the variable x holds five numbers so now i say what is x i look at it well it is the list of numbers 5 4 3 2 1. but what you see is you really need to get at an individual number so what i'm going to say is i want to get x in position 1. okay so if i tell it x in position one what number do you expect it to give me i want the one position of x you probably expect five but if you expected that you would be wrong because it is four because in the crazy world of programming things typically start at zero and so that five is in the zero position okay so the zeroth element is five and then the first element is uh that was bad the first element is four and then what you can see is the fourth element is one also even though i have five numbers there is no fifth position so if i say x of five it gives me an error because with five numbers starting to count at zero you end up at four right you end up at four so there is no fifth number and you get this error uh index out of range and this when you're programming in python that's an error that you get a lot index out of range so when you see index out of range you think i somehow i went one too far in the array and i tried to read a number that's outside the index range of the array does that make sense i hope that it makes sense okay so we could also i'm trying to think of what else we could do here what we could also do is we could change something i could say x of 1 is equal to 7. now tell me what you think is going to happen if i say x of 1 is equal to 7. now let's look at x in that one position in that one position now instead of having a 4 i now have a 7. so once i build the array once i build the array i can go in and i can put numbers in it once i build the array i can go in and i can change the numbers i can individually address them okay i can individually address them like i just did and so now i could say what is x sub 1 x of 1 is 7 which i just set it to so that is pretty cool well what if i set like x is an empty array so now it knows that x is an array and now if i just say what is x well it's the empty array well now i want to put some numbers in it so i'm going to say x of 0 is equal to 7 and boom i get an error okay i get an error it won't let me put a number in there so this is kind of one of the quirky things about python is you can't just go in and add numbers to an array by indexing like you would expect you can put a bunch of numbers in it first like 5 4 3 2 1 like that and then that was not good x equal would be good okay and now i could say what is x and then i could say what is x sub 1 or x of 2 let's say and then i could say x of 2 equal 27 okay i can change it and that works great to change it but how do you build an array how do you build an array well i'll show you that what i can have is let's say that i have y i've got to create create y by making an empty array and then i can say y dot append and that is y dot append a what a 4 ok and now if i look at y look at that it appended a 4. now i could say y dot append and i could append a 7 like that okay and then if i say y it's a 4 and a 7. now note when i say append i'm using the smooth parentheses because this is a function call i'm not using the square brackets which indicate the contents or an array i'm doing a function call so that is why it is the around one so i say y dot append of 7 now i have 4 and 7. now i could say y of 1 equal 23 and then i say where is y well i can go change it but the way i build it is i build it appending it and then i can go in and change it by individually indexing a given element that i am interested in so i hope that all makes sense now you can also do multiple dimension arrays you can do multiple dimension arrays and so what i could say is x is equal to that would be where it's not just a long list but you could think of having rows and columns of numbers and so let's start the array with an open okay now i'm going to start my first row with an open and i'm going to have 1 comma 2 comma 3 that is the first row i in the first row and then i put a comma and then i open the second row and that's 4 5 6 and then i close that and then a comma and then the third row i'm going to open is 7 8 9 and it doesn't have to be square it doesn't have to be square okay it doesn't have to be square i'm going to have to move further out of your way or you're fixing to start yelling at me aren't you okay and so that is the third row let's make a fourth row and that would be 10 11 12 and then i'm going to close the row i'm going to close the row and now i'm going to close the array and now boom now what is x it is 1 2 3 4 rows and 3 columns now what if i said x of 1 now if i just said x of 1 it's going to give me what it's going to give me the first row which is the second row right it's going to give me the element 1 which is what 4 5 6 if i set x of 0 it's going to give me the 0th row which is 1 2 3. all right so i can get individual rows what if i want what if i want an element in a row like if i want the first element of the first row i should say the the one position so i want row one not row zero i want row one and then i want column one well what i would do is i would say x give me row one and then give me column one and what would that be okay well this would be the zeroth row this would be row one and then this would be column zero and this would be column one so it's going to be a what it's gonna be a five i should have asked you that made you do that one that would have been a good one to have you think through so we can index things more than one and you can even do more than that like let's say that i had let me do one more example let's say i had x is the first row i'm going to start the first row and now the first element in the first row is going to have three numbers it's going to have one two 3 like that okay now i'm going to go to the next element and it is going to have 3 4 5 okay and now i'm going to go to the next element and it's going to have 6 seven eight okay now i'm going to close that element i'm going to close that row and now i'm going to go to the next row all right and i will just again now i'm going to open the element and it's going to be 2 3 4 and then the next element is going to be i i did not do that right okay i open the element i have to open the row so let's look at let's look at this again and why is this important let me tell you where i'm going that like if you have a picture a picture or pixels there are rows of pixels and there are columns of pixels so that's a 2d array rows and columns but at a given pixel if it's color there are three numbers at that pixel the r value the g value and the b value so at each intersection of a row and a column you have three numbers and that's kind of what i'm trying to show you here so we open the array we open the row and then we open the pixel we close the pixel we close the pixel we open the pixel we close the pixel now we close that row of pixels we go to the next row and then we open the row and then we open the pixel and then let me just do two rows so i'll say five six seven go to the munchkin me five six seven comma or five six six comma uh let's say uh i need one more so i'll say seven six five like that so now let's close the pixel let's close the row and let's close the array now if i did my brackets right this should take and it did okay now what if i say x sub 1. that's going to be the first row and the first row has 3 pixels okay if i want to look at an individual pixel let's say i want to look at a pixel at row 1 column 2. so i'm going to say x row 1 column 2 and that is this pixel 7 6 5. it is the pixel 765 because it is the first row which is you know the second one and then it is the second which is the third and you end up over here at this pixel well what if i wanted the red value of that pixel well that would be x1 row one column two and then which value do i want well the red would be the first one rgb r would be the first one which would be zero and then what do you expect here what do you expect you got a seven okay does that make sense so here like you can see that you can put pictures in these arrays and that's why i want to kind of play with that a little bit because a lot of python if you really understand these variables and kind of data structures everything's going to be really easily easy what else could i do let me think make sure that i'm hitting the important high points here okay let's say i could also say like i could say my fruits my fruits are equal to i could put a string apple in the stream a string orange and then comma and then start a string start a string banana banana like that i'm going to close that string now i'm going to close the array and i forgot my equal sign didn't i i forgot my equal sign okay there all right so now what if i said what is my fruits okay apple orange and banana well what is my one fruit well i would say my fruits my fruits of one and that is orange okay does that make sense what if i just did something crazy like i've got just a one-dimensional array so i can only use one index what if i added another index what do you think would happen if i added another index tell me what do you think my fruits of one zero would be it would be zero i mean it would be o why because the first the one position fruit is orange and then it says the guy wants to break it up further i'll give him letters and so o is the first letter in orange and so what if i said like 2 that would be a o r a c c you can actually even index down inside of a string which is kind of a crazy thing so we have uh we have index we've done arrays we've done okay so let's let's let's play with this a little bit more so what if i said uh print hello world okay hello world is a string right hello world is a string and so now i'm going to print hello world and it prints hello world the string okay what if i said msg is msg is equal to hello world okay like that hello world and now what if i said print msg tell me what you think it's going to do leave a comment what do you think it's going to do print msg like that if you had said it was going to print hello world you would be wrong why because i didn't tell it to print i didn't tell it to print the variable msg i told it to print the string msg so it printed msg oh you wanted hello world maybe you should have printed m s g like that and you would get hello world why do i do these things because man teaching real students in class these are the mistakes i see over and over and over and so i try to kind of make those mistakes i have been like the munchkin for 20 minutes here and i can't stand that so let's come back to more a more personal view of me and not like the little postage stamp in the corner version of me all right so that's pretty good okay so we have played around at the command line but what do you really want to do you really want to write a program right so let's quit out of this remember it's quick open close and like that and now let's do an ls to see let's see where we are pwd from last week print working directory you can see that we're in my home folder which is slash home slash pi that i'm the default user which the username is pi and so that is where i am i am if i do an ls to look at my uh my uh directories there i will have to get a little bit further out of your way okay if you look at the directories there you can see these various directories i want to play with python so what i'm going to do is i'm going to make dur make a directory i'm going to call it python and do you see how like the the standard they're using is the folders they start with an uppercase letter i want to do that in that way it's just always when i'm go i want to go to python remember well i my fault my my directories start with an uppercase letter so i'm going to make it like that make directory python there it is and now i'm going to change directly down into python you learned this stuff last week and now i want to write a program well what are we going to use we're going to use our friend that we learned about last week mr nano and so the way i'm going to do this i'm going to say i'm going to say nano and then the file is going to be called hello world and then dot p y indicating that it is a python program so we could come up here and now i could just say print hello that's not good print hello world like that and then close it and then close it and then how do i get out of here i do a control x and then it says save modified buffer i'm going to put a y for yes and then where do i want to save it i want to save it as helloworld.pi now you could backspace and change that if you want but of course i want to keep it that and then there it is now what i could do is i could say python hello world dot py like that and boom tell me what i did wrong though why did i do it wrong because i know you're going to do it wrong and so i'm going to make the mistake somebody tell me what i did wrong there okay i ran it under python 2.7 because i didn't say what python 3 hello world world dot p y okay now it runs it in python 3. does it matter when you are just writing hello world no it doesn't but does it matter a whole lot in other cases yes it does so make sure that you're running under python 3 3.7 there and you do that by running under python 3. like i just showed you okay well that is pretty good but let's come back over there now and let's go back and if i hit the arrow key i can get my old commands back quickly and so i am going to nano hello world dot pi it brings it back up i really don't want to say hello world anymore let's just see if we can write some code here so i'm going to say x is equal to 7. i'm going to say y is equal to 2. i'm going to say z is equal to x plus y and now let's see if i can do a little bit fancier print i'm going to say print i'm going to string things together and you string things together separated by a comma and being mindful of whether you're printing a string or a variable and so what i'm going to do is i'm going to print x and then i'm going to print the string okay so x comma to say it's something new and then i'm going to print the string space put a space and now the plus and now a space now i'm going to end that string and now i'm going to print a y and then i'm going to print another string and that string is going to be space equal space i'm going to end that string and then i'm going to print z like that okay so this should say like 7 plus 2 equal 9. all right so let's control x yes enter and now i'm going to nano i'm going to python 3 hello world and look at that boom seven plus two is nine okay so we did a little bit fancier formatting there let's go back and let's nano hello world again and that's kind of neat but you know what i could also do is instead of printing x plus y i could print it as a string so i could say the string x plus the string y now i'm confused the string somehow that got confused okay the string y and then comma and then the string z or the string equal so i'm going to say the string x the string plus the string y the string equal and then z okay ctrl x yes enter and then we're going to python 3 that one again and and there it says x plus y equal 9. so you see i could say 7 plus 2 equal 9 or i could say x plus y equals 9. i'm just trying to show you a little bit about how you can string things together with that print statement get some nicely formatted output on your code and so that looks pretty uh that looks pretty good you could also come up here and you could just make it easier if we'd done this from the start we would probably have just made one string and that one string would be x plus y equal like that okay and then ctrl x yes enter and then let's python three it and then you see we get the same thing x x plus y equal nine it was just a little bit easier way to do it the second way that i showed you all right and so now let's go back in there and let's play around with some more things and so what's the problem here well that's kind of a neat way to add two things together but let me just show you let me show you like if you want to multiply how would you multiply well you use the shift eight you use the star and then this would be like this this would be the star okay and now control x yes enter and then let's go python three and then in that case you say x times y is 14 7 times 2 is 14. so that looks good so that is how you multiply you use the little star okay now how do you raise something to a power so if you wanted to say 7 squared how would you do it i'll be honest with you i don't know but let's just say if you do star star that's the way it was in fortran [Music] okay i shouldn't i really shouldn't do these things on the fly ah look at that x to the y is is 49 i'm kind of i'm kind of relieved that that worked because i really hadn't done that in a while and i didn't know if that would work or not okay so that looks good let's uh nano it okay so the way you raise something to a power it is the star star or asterisk asterisk so that would be x raised to the y that would be 7 raised to the 2 and so that would be 49 well i wonder how you would take the square root of something well you kind of do it with math you would sort of take that you would do it with math let's make 9 that was just crazy what happened there okay let's make it nine okay guys what i should have told you is the mouse doesn't really work the mouse doesn't really do anything you can highlight stuff but you have to use your up and down keys to move the cursor you see i can't click up here and move the cursor the cursor stays down there so that's not good okay and so let's come here and let's uh let's kind of put the star star like we're raising something to a power well how would i take the square root of 9 well that would be 9 raised to the one half so if i said 9 and i said y is 1 divided by 2 okay then x raised to the y would be 9 raised to the one-half and if i'm thinking about this right that should be that should be 3. so let's control x yes enter and then let's run it and boom square root of 9 is 3. okay so because these are just really important things we can add subtract multiply divide divide is the slash that's pretty obvious and then what you can do is you can raise something to a power if you want to take the square root or the cube root it'd be like raised to the one half raised to the one-third if you want to take the fourth root it would be raised to the 1 4. so this stuff is pretty good let's come back in here and see if there's any other simple math and so that's kind of the the the basic math that you need there's a lot of different math functions but you can uh you can figure those out as we're uh as we're going along and so let's look at let's look at uh let's look at that divide okay so let's say that x is one and let's say that what let's make x one okay and now let's make y uh three and then let's say z is x divided by y and then let's come down here and we will say x divided by y x divided by y is z control x yes enter and now we're going to python 3 that thing and what it says is x over y is point three three three three three there what is the problem with that that's like way more precision like if you're running out and you're making a temperature measurement or something like that and your sensor is good to one or two decimal points don't print things out to 57 decimal points because your instrument that's making the measurement doesn't have that level of resolution plus it makes it really hard to read so how would we fix that we would come in and what i would do is i would come over here and i would take take z and i would print the rounded value of z and i would round z to two places so i'm gonna rnd i'm gonna round z to two places but now i still need to come in and i need to close out my print and so this closes out the round function this closes out the print and then balancing your parentheses are really important one of the hardest things to debug in coding and one of the the most common problems i see with students who are working is not getting their their parentheses balanced and when you do that a lot of times you get sort of strange errors let's do it the wrong way let's say i hadn't closed that out and let's see what happens okay let's run it the wrong way and then what it says is hello worldpile.7 unexpected end to file while parsing unexpected into file while parsing that is not exactly very descriptive and you wouldn't really know what to think so you can when you don't balance your parentheses you can get strange error messages okay let's hop on up python3 hello world round as an rnd is not defined i taught you wrong isn't that terrible i taught you wrong maybe it is round like that round ctrl x yes enter and now we'll python it and and you've got a spell round right oh man i'm falling apart here okay r o u no y in round okay now ctrl x yes enter oh i think i hit the y for yes and i think it put it in there at the wrong time okay so now we will python 3 it and there it is x over y is equal to 0.33 see that looks a lot better now the other thing that you could imagine that you could do over here you could come up and you could say something like you could say something like right here you could say something like z is equal to round of z to two places okay but you see i don't like to do that i like to carry that extra precision along with me just in case there's any meaning in it and then i just get rid of it when i print it but you see like let's say that let's say like let me show you here what would be what would be wrong like what if i said z is equal to round z of z over two and now i don't need to round it here so i would just print z like i would print z like that and then if i came in and said okay z is equal to z times 3 and then print z i control x yes okay so you see it says x divided by 3 is 0.33 but i rounded it off and then i say what is 3 times that's 0.99 so now all of a sudden i've got an error in because i actually chop that number off so i don't want to chop that number off i want to keep the the full precision and only print a chopped off version of it but i want to chop the number off itself if that makes sense okay guys we need to keep on moving here we need to keep on moving and so what i'm going to do is i'm going to come back and i'm going to go back into that program and uh let's just i'm just going to back over all that sort of stuff and let's say what was the problem with that well like if i wanted to add two different numbers what do i have to do i have to go in and i have to edit the code well what would you like you would like to be able to get input from the user so how do we get input from the user let's say that i say x is equal to and now instead of saying x is equal to 3 i'm going to get it from the user from the keyboard so i will say x is input and now inside the parentheses i need to give it a command prompt input your first num first number in the string in the quote so it's going to tell the user input your number and then it's going to put that number that they input into x okay now y is going to be [Music] input or yeah input and then i'm going to say input your second number and put your second number okay like that close it right and now i'm going to say z is equal to x x plus x plus y okay now i'm going to print again this should make sense i'm going to print uh i'm going to print x comma and then i'm going to put the string space plus space i'm going to close that string and then y and then i'm going to put uh x plus y and then i'm going to put the string i'm going to put the string space i'm going to put the spring space equal space and then i'm going to print z like that does that look good i think that looks good i did forget a comma there like that i forgot the comma okay control x enter [Music] okay so now let's try running that nano i want a python three okay input your first number well i'm going to put three okay input your second number two now tell me what this is going to do say it out loud or put it in the comments down below or on the premiere put it in the chat what is this going to do say what it's the output's going to be okay if you said 3 plus 2 equal 5 you would be wrong 3 plus 2 equal 32. somebody tell me what happened somebody tell me what happened okay this is what happened and it's what i warned you about you assumed when you input something from the keyboard that it was going to input 2 is an int but it didn't input 2 as an int it put it as a string so it was the string the character 2 and then the string the character three and then in the string world what is plus plus is concatenate so let's take a look at that let me control x out of this let's go back and just say python python3 okay so if i say x is equal to i'll make it the string three okay and then y is equal to the string two i'm making it that deliberately here and then i say x i say x plus y x plus y is the string 32 okay so the input inputs things as characters or strings and the plus and string world concatenates the plus in string world it concatenates so i could say x is equal to hello okay and y is equal to space world like that and z is equal to x plus y that plus is an addition it's concatenation now and now z is hello world okay does that make sense man you see i'm telling you the mistakes that you're going to make and these things are really terrible and sometimes you get these errors in python that are so hard to figure out what went wrong so i'm trying to kind of deliberately show you some of the pitfalls okay let's go back let's go back and say nano hello world all right so we got that figured out so what do we need to do well what we could do here is we could change them to n so i could say x is equal to int of x like that and then i could say y y is equal to int of y and now z should get it right this time because i've changed them to nts so now i'm going to do a control x yes enter and now let's python it i nanoed it okay what is it i'm going too fast i need to slow down take a deep breath take a sip of coffee i'm going too fast all right so we're going to nano it nano hello world okay that looks right we're going to control x yes now we're going to python it okay we're going to python it there it is okay enter your first number three you know your second number two and then you have three plus two is five okay does that make sense let's go back and change it i'll show you how i would usually do it rather than coming down and doing these extra lines of code i would force it when i read it so i'm going to say that x is equal to the int of input and that will take that string and right when you read it it will change it to an int [Music] okay like that and then ctrl x yes enter and now let's python that okay now we say three two three plus two is five okay that works really good now if we wanted it to if we wanted it to be python nano if we wanted it to be a float we would write here say float like that and then we would say float like that control x yes enter and then let's run it okay now we're gonna say three notice i'm entering it as an integer two i'm entering it as an integer but then it says three point plus two point is equal to five point so it's changing it to float based on that uh float command on the input so hopefully that makes sense all right guys we've gotten pretty good here we've gotten pretty good with variables but what we need to do now is we need to work on conditionals like our if statements coming from the pi coming from the arduino world you understand if statements but let me just show you how they are done in python because it's a little bit different so let's nano hello world and so let's just say uh let me get rid of all this i'll keep my first y x input and i'm going to input input it as an int and then input your instead of first number just number okay now how do i do an if statement well i can say if and then i can say if x is greater than 0 okay then in arduino the way you start your clause with an if statement you start it with a curly and then you end it with a curly okay in python it's not that way the if statement is followed by a colon and then how long are you in the if statement as long as you are tabbed over okay so i can say if x is greater than zero i tab over okay and then i would say print your number is positive okay now i could also say print [Music] i like positive numbers and tell me the mistake i made up there i did not close my string up there okay now that should be good so if x is greater than zero it's going to say your number is positive and i like positive numbers the second print statement is still part of that if clause because it is indented okay but here now this isn't indented this is breaking out of the if statement now i could say if x is less than 0 then what do i have well i'm going to tab print your number is negative like that and then close it okay and then print i don't like no negative numbers like that [Music] okay now let's control x yes enter and now let's run that python3 okay input your number two uh uh your number is positive i like positive numbers okay then i could okay you could see that the less would work then yeah let's go ahead and show it okay so now what we would do is we would uh python three it and then input your number negative four and you could say your number is negative i don't like negative numbers okay so you can kind of see here how to do conditionals we're starting to learn the conditionals and what you see is the if statement is lowercase i on the if what you can see is you need to on that line put a colon and then you stay in that if statement as long as you stay tab the tabs have to be exact and don't do it by putting spacebar spacebar spacebar you need to use a tab okay one tab to indent over like if i put another tab there the thing is going to crash it's not going to work you have to tab and you have to tab everything where it lines up how does it know when you left the if statement you've leave the ifs clause when you stop when you stop indenting okay so that is pretty good i'm trying to think okay let's try let's try what is okay what did i do wrong here okay and this is what i did wrong i did not cover the parameter space of possible answers what did i leave out i left out zero and this is why like i don't like else statements okay let me show you why i don't let like else statements this is the type of thing you do all right this is why i never use else statements and it's something that i argue with you guys about oh well instead of using two if statements i just use an i just used an else statement okay so you would say if x is greater than zero then you print that else okay else you print this okay that's great right that's why you guys would do it you guys that love the else statement okay so now let's uh run it okay and put your number three okay your number is positive i like positive numbers okay uh and then nano uh didn't mean to do that [Music] python three okay input your number negative two negative 23 that's fine your number is negative i don't like negative numbers okay so everybody's happy right everybody's happy you and your little else statements you guys are all healthy uh happy what if i put a zero and then you say your number is negative i don't like negative numbers why because 0 isn't negative and it's because you used an else statement and then you get this unexpected result in an area that you weren't thinking about okay so let me show you why i use if statements because if statements can be used to completely cover the parameter space well the number could be greater than zero okay the number could be [Music] is less than zero okay like that and then what is the other possibility if x is equal to zero okay then what do you do you print your number is zero because i'm not going to argue about whether x whether zero is positive or negative you could do that do you see how much cleaner it is let's think i can go from negative numbers all the way to positive numbers what is every possibility if x is greater than 0 if x is less than 0 if x equals 0. and in fact if i were coding that would make it even more explicit if x is less than 0 if x is equal to 0 if x is greater than 0 and i would even put my if statements in order to completely cover the parameter space now i know some of you guys are going to jump in and start saying oh well i could do it with an else statement w no you know just just use if statements just to make me happy stop using else statements causes problems all right now i have an error in this code and what is the error somebody tell me the error okay if you want to go greater than the condition is greater than if you want to go less than the condition is less than but what is going to happen when python sees if x equals 0. let's just run this okay so i'm going to run it and i'm going to say [Music] it actually found it darn it i was hoping that it wouldn't find it but it found it okay what it is is the condition on if x equals zero you have to do equal equal if x equal equals zero you have to use two equals if it sees x equals zero it thinks you're setting it to 0. you can't do that you have to use equal equal and i was surprised that python found the error usually it behaves in an unusual way i think arduino doesn't find that when it behaves in an unusual way but for an equal condition you have to go equal equal so now let's go ctrl x yes and now let's go python3 and let's go uh zero and then it says your input your number zero your number is zero okay and now it works for all the different possible cases okay let's jump back up here and let's kind of keep learning let's keep this learning going that we are doing so how so you can imagine besides greater than you can have greater than or equal to you can have less than or equal to you can have also the not like you could say not equal to and not is the exclamation point so you could have not equal to would be exclamation point one equal so those are your conditions but sometimes you might want uh you might want a compound conditional where you have more than one condition in one if statement so let's see like how would we find out if the number is even or odd well i could say z is x mod i'm panicking because i can't see there it is okay x mod x mod 2 and what the mod function does is it gives you the remainder so 4 mod 2 would be 0 because there's no remainder 3 mod 2 would have a remainder of 1. okay so mod is the remainder and that little percent is the mod okay now what i could say is if x is greater than zero and z is not or if z is equal if x is zero that means it's positive and z is equal to zero what would that mean that would mean your number is positive and even positive and even like that and then i'm going to take this other nonsense out okay and now i could also and what i'm going to do is i'm going to copy this and what you can do is you can come across and then copy and then you come back here and this is going to be almost the same [Music] and control v does not cut does not paste and so you're going to have to right mouse click and paste like that okay so now if x is greater than 0 and z is equal to 0 then your number is positive and even what if the mod is not what if the mod is not equal to 0 then what would that mean that would mean that your number is positive and odd okay like that now i'm trying to think i think i'll just change this if x is less than zero and z is equal equal zero then what does that mean your number is negative and even and then get rid of this nonsense even like that i better close that close that string okay and then here if x is 0 printer number is 0 and i'm not going to argue about positive or negative or even arrived i'm just going to say your number is 0 in that case now the one other one we have to do is say if x is less than 0 and z is not equal to zero then what do we have we have the case of print your number is negative negative [Music] and odd like that and now we've got the less than the less than the greater than the greater than and we've got the zero so i think using our friendly if statements we have the whole range of possibility covered did i get that all right okay control x enter or yes and then enter okay now let's run it python3 okay input your number three your number is positive and odd ah let's run it again four your number is positive and even uh-huh and now let's come up here and say uh minus eight your number is negative and even and then let's say minus seven what okay we got an error hopefully you guys caught it when i was doing this and what you can see is i misspelled print and that's another thing if this had been a compiled link which it would have found this but here it doesn't find it until you actually run that line of code which means you really have to be careful when you're debugging your code in python okay so now let's try that minus seven one again okay so minus seven and your number is negative and odd and then one more we'll be checking the case 0 and it just says your number is 0. okay so boom we got that done so you understand conditionals you understand variables you understand arrays you understand if statements you understand compound conditionals we are learning a lot here let me show you something i did really poorly do you see on all of these things like if you look up if you look up here how when i answered it would run my answer into the question and i really should have i should have fixed that very early on you see when i say input your number i didn't leave any space so it would have been good to do something like a colon and then like that and now ctrl x yes and let's just try the zero case again and control x yes okay let's do the python three let's do the zero and you see now because i put the space on the prompt now it's not running the answer into the question okay so now let's go back to our program python3 hello world you want to nano it there it is okay so what we did there was we got our conditionals done you know one thing is don't you kind of wish that you didn't have to run the program every time well i'm going to show you how to do a while loop okay so i'm going to come here and then i'm just going to say while okay and then while 1 equal equal 1. now how do i start the while loop just like the if statement you start it with a colon how long are you in the while loop as long as you are indented so if i want these things in the while loop i've got a tab but now i've got to tab the print so it will be inside the if statement and then i'm going to tab the if statement tab it's print you kind of see what i'm doing here okay i'm tabbing all of these things over so they'll be inside of the while loop now what's going to happen when does 1 equal 1 1 always equals 1 and so that's an infinite loop and so now we are going to run that it says input your number my number is 3. that is crazy something happened here that i was not expecting control x control c okay control c killed it ah what did i do what did i do okay the input was not the input was not in the while loop and therefore once it got a number it just kept going through printing it never got another number and so i was being careless there and that while loop should be at the very top up here and i should say while while 1 equal equal 1 there's my colon and now what do i need to do i need to tab these over to be in that while loop as well now control x yes enter and now we're going to say python3 and what's my number 3 is a positive odd number 2 is a positive even number 0 is 0 3 positive odd minus 3 is negative odd minus 28 is negative and even boom okay so you see we can do our conditionals and i've also shown you how to do a while loop that a while loop a while loop let me uh i need to do like a ctrl c ctrl c will kind of hard get you out of your get you out of your program so let's go back and let's edit this now so you see the while loop will loop as long as the condition is true so as long as the condition is true it will loop and so let's just kind of do like really just do one while loop here just to make sure you know how to do that this is really this is really amateur hour here how i'm erasing with the backspace key but let's say x is equal to 1 and then i could say while x is less than or equal to 10 okay then i'm going to tab and i could say print x okay and so as long as x is less than or equal to 10 i'm going to print x and then inside loop i'm going to say x is equal to x plus 1 and then it should go all the way up to 10 so it should print 1 2 3 4 5 6 7 8 9 10. so i'm going to control x yes enter and now let's run it nano python3 hello world and there it is boom one two three four five six seven eight nine ten a while loop it it loops as long as your condition is true it loops as long as your condition is true and so as long as x was less than or equal to 10 it looped and then when it was not you could say print and then i could print that's all all volt bulks like that close it okay so now let's control x yes enter and now what's going to happen is i misspelled print again that's twice man i need a dollar jar i got to put a dollar in every time that i misspelled print believe me i'm hitting the t it's a wireless keyboard and the t is not making it ctrl x yes enter now let's run it python3 there it is one two three four five six seven eight nine ten that's all folks let's nano it okay so why did it just print it once because it wasn't tabbed after you went through the loop then the first command that wasn't tabbed is not part of the loop now if we tabbed it what's it going to do somebody tell me what it's going to do here if we tab it over every single time it's going to print it because it's in the loop okay so now we have done conditionals we've done variables we've done arrays let's do a simple program that does a for loop and also sort of see how you can populate an array in uh in python so we are going to nano hello world we're gonna do our amateur hour back over this other stuff and let's just get a list of grades from a user okay so i'm going to say num grades is equal to int because i'm going to read it as an int and then i'm going to input it and i'm going to say how many grades do you have like that okay close the string close the input close the end all right now what am i going to do i want to go in a for loop okay i want to go in a for loop i better i better just do a for loop to begin with okay how do we do a for loop for i you could call it anything but i'm going to use my counter my index is going to be i and i'm going to say in range for i in range i'm going to start at 0 and i'm going to go to 10 and i'm going to go in steps of one okay and then i am going to tab so the for loop puts inside the for loop is what is tabbed over so do you see if statements for loops and while loops they all have this colon here and then you are in that as long as you are tabbed so now i'm going to say print i somebody tell me what this is going to do this is going to count from what to what okay count from what to what somebody leave a comment this is going to count from what to what and if you said from 0 to 10 you would be wrong because it's going to stop at 9. and let's look at that because this is one of the biggest problems in python that i see okay so you said go from zero to ten but it didn't go to ten it stopped at nine it stopped at nine now why did it do do that because it figured if you need ten numbers and you start at zero so it's sort of this crazy thing of how programmers start at zero and so zero one two three four five six seven eight nine is actually ten numbers and so it always stops one before because it assumes that you're starting with zero if that makes sense but if you wanted to actually count from one to ten how would you have to do that you would have to say go from one to ten in steps of 1 that would stop at 9 so you have to go one more than that 11. okay now let's try that okay so then it goes to 10. okay let's uh let me just show you the way that i like to do a for loop okay let me show you the way i like to do a for loop and uh the way i like to do the for loop is i don't like this range thing because i am always confused what i like to do is i like to use our friend numpy and so what i'm going to do is i'm going to say import numpy as np so what did you just learn you learned how you can import a library in that library will give you access to new functions that the normal uh that the normal python wouldn't let you have so let me just start by saying import numpy okay now what i could do is here i could use a function that numpy has that's called linspace okay and so i would say i want to use the library numpy dot lin space and linspace says where do you want to start i want to start at 1 where do you want to stop i want to stop at 10 how many points do you want i want 10 points and so it's like start where i want to start stop where i want to stop and give me the number of points that i want all right do you see that lens space i have start stop and number of steps okay so let's run that boom one two three four five six seven eight nine ten do you see why i love linspace i love linspace because i told it where to start i told it where to stop and i told it how many steps and i could come in let's say that i want to do 20 steps okay what happens there somebody tell me what's going to happen look at that it's going to go one one point okay it's kind of counting in halves but i'm kind of confused here let me see unexpected result 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20. i'm just confused why that came out kind of rounding in a strange way let's look at that okay i want to start at 1 and i want to go to 10 and i want 20. am i thinking about this wrong maybe i should have done 19 and it will go right yeah okay 11.5 to 2.5 okay so i've i got the number of steps wrong in order to make it end up even but you see why i love lens space you say where you want to start where you want to stop and then how many points you want in there okay so let me show you we learned how to import a library by saying import numpy who's your friend numpy is your friend but now every time i want to use numpy i'm going to have to say numpy.this and numpy.dat i can import a library and give it a new name so i could say import numpy as np and now anytime i want to call numpy i just call np and that makes my uh that makes my programming a lot easier so i can now say control x and then i can run that oops control x enter now i can run that and boom you see it works just the same so that's kind of a little pro tip there you can [Music] you can use linspace you can import it in numpy and you can import numpy as np as we need to wrap this thing up there's one more thing i need to show you let me show you how to populate an array using a for loop so we will come back up here now and we will nano this and what we're going to do is we're not going to use that anymore amateur hour by backing over my old code okay so now what i'm going to say is how many net grades do you have num grades is equal to int and i'm going to get it from the keyboard so i'm going to hit in put and what i'm going to input how many grades question mark space close the colon close the input i mean close the close the string close the input and close the int now i'm going to say for i in range okay i better create my array right i'm going to say x is equal to what an empty array all right x is an empty array now i'm going to say for i in range okay and where am i going to start i'm going to go start from 0 to num great my goodness what did i hit uh i don't know how to get out of this i hit a control something unusual so let's see if i can control x okay it got me out okay for i in range i'm going to start at 0 i'm going to go to num grades okay and then i'm going to go in steps of 1. now you might say well wait a minute you're not going to make it to num grades well i don't want it to go to numb grades since i'm starting at 0 i want to start one before num grade switch is how this works you can kind of see that it makes sense now so if i say 10 grades it's going to ask 10 times it's going to go through 10 times it's going to start with 0 and stop 1 before numb grades and so i hope that makes sense okay so now what you might think is you might think that you would make x is equal to or yeah you might say now what i need to do is my grade is going to be equal to my grade is going to be equal to input and i better make it an n so i'm going to make it an int and then i'm going to say input your grade and then i'm going to close the string i'm going to close the end and i forgot to say input here input open okay now that closes the string that closes the man this is amateur hour here okay so this is make an integer of the input and now close your string close your input and close your end now i've got my grade but how do i get it into x well you could think you could say that x sub i is equal to my grade and if you thought that you would be wrong because of what i showed you earlier you cannot index something that isn't there already so the way we would do this is we would have to say x dot append and what do we want to append to x my grade i really wish we could index things the way that i showed you is the wrong way some languages you can do that not in python okay and so now i should have it there now what i could do is i could say for i in range and i could go from zero to num grades in steps of 1 and then what could i do i could print and i could print x of i like that okay and so now let's run that let's see x of i let's run that and control x yes enter okay how many grades three i'll have 70 80 90. okay oh line seven l is not is it's out of range what did i do what did i do and i'll tell you why i did it because it's a real common error when you are using i an i looks a whole lot like a 1 and it looks a whole lot like an l and so you see i put a typo in here i didn't say print x sub i i said x sub l that needs to be an i actually here with the font in this terminal they really made the i and the l distinct but in a lot of programming environments the i and the l and the one all look very very similar and it is a mind numbing it is a mind-numbing problem to try to debug that because it looks so right so let's go x yes enter and now let's go python3 hello world let's say five grades i'll make a 99 a 98 a 45 and a 100 like that and so let's see that's one two three this is the fourth grade and then it is going to get a 97 that is the fifth grade so you see it counted correctly and then boom 99 98 45 197 so this crazy start at zero and go to num grades but stop one before it it actually does kind of work it does kind of make sense let's just come in here and we could make this a little nicer that did not need to do that okay let's go and nano it and we can come up here and after we input the grades we could come here and say print and then say your grades are like that okay close the string close the print control x yes enter and now we are going to python 3 that how many grades do you have let's say five i'm going to say 91 93 95 and i'll say 92 and then 97 like that and then it says your grades are 91 93 95 92 97. what do you not like i hope you guys were yelling at me what should you have been yelling at me do some nice formatting there don't do that that's bush league stuff there that's bush league stuff there let's come in and when we input something you see how nicely we did it up here okay with the space but we didn't do that here so what we say input your grade we could just uh put a colon in a space and then that will make it nice let's just try that to make sure we'll say python3 python 3 hello world okay two grades man i don't know what that was two grades 99 and 96. and it says your grades are 99.96 boom okay that's pretty nice that's pretty nice that looks pretty good guys this has been a long lesson but i really wanted to just blast through i wanted to blast through this in one sitting and so let me give your homework assignment what your homework assignment is for next week is to take this okay uh to take this program and what i want is i want you to have three for loops the first four loop inputs the grades the second for loop averages the grades and the third for loop prints out the grades and then down at the bottom it say and your average is you're going to input the grades you're going to average them then you're going to print them all out and then you're going to print the average that is your homework for next week leave a comment down below if you're able to do the homework leave a comment i am legend if you're not able to do it leave the comment i fold it up like a cheap walmart lawn chair so i need you guys to be doing this homework and averaging grades is kind of a classic problem you learn it sort of allows you to learn a lot of the stuff that we did in this lesson so let me control x out of here let's get over here now that i have given you the homework assignment uh let's go get over here to our river viewing uh friendly fisherman out there we haven't seen the fishermen in a few a few days it's a beautiful morning out there on the nile you can see that we've got a little clouds building up we probably get a rain this afternoon and uh this fisherman is using his paddles okay very nice so what have we done in three lessons lesson number one you laid the foundation you can get your pi booted and you can find your way around the desktop lesson number two you learn the the essentials of linux you can work your way around the terminal now you notice as i was doing all this stuff in the terminal today it was easy you guys were familiar with it because you learned the terminal last week today we learned python next week next week is going to be the day that we start working with the gpio pins and the gpio pins are a lot like the python digital and analog pins only i mean the arduino digital and analog pins but on the arduino it's just like you just do a pin mode and man you just do whatever you want with those pins it's a little more a little more uh subtle with the raspberry pi you've got to kind of set those things up a little bit better and so we'll start next week by showing you how to understand what all the different pins do and how to configure them and then of course we will be busting into the most excellent sun bounder ultimate raspberry pi kit and we're actually gonna gonna turn some of these pins on and see if we can hook them up to some stuff in the kit we will see if we can hook them up to some things in the kit and that will be lesson number four and then as these series of lessons go we're just going to be connecting more and more complicated components to the raspberry pi and then we're going to be doing more and more complicated analysis and data handling and more and more like graphical types of things with the data and so we're just going to start by like reading something and printing it or sending a simple command to turn something on or off and then as the lesson goes on it'll as the lessons in this series goes on we will just be doing more and more complicated things and what are my goals my goals are for you number one to learn to think like an engineer number two to learn to code number three to learn some math and number four most importantly is to have some fun i really really want to make this fun math and circuits and programming and projects can be a whole whole lot of fun it's just we sort of learn to hate math and we learn to hate things when we're in school but we're going to learn to love those things and i promise you above all this is going to be a fun class so remember next week we will get into the kit link down in the description make sure that you order it now so you'll have it next week if you like this video make sure to give it a thumbs up because that helps us with the old youtube juice to have people give it a thumbs up it will show this to more people if you have not already make sure that you subscribe to the channel when you subscribe to the channel make sure that you ring the bell so that you will get notification when future lessons come out and then share this with your friends and family share this with other people because the world needs more people doing engineering and coding and fewer people sitting around watching silly cat videos paul mcwhorter with topdeckboy.com i will talk to you guys later laughs
Info
Channel: Paul McWhorter
Views: 48,563
Rating: undefined out of 5
Keywords:
Id: Wabv5e0KQaU
Channel Id: undefined
Length: 90min 11sec (5411 seconds)
Published: Thu Mar 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.