Python Tutorial 1: Introduction to Python for Absolute Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter from toptechboy.com and we're here today with lesson number one in our incredible new tutorial series where i'm going to teach you how to code in python what i will need you to do is pour yourself a nice strong cup of black coffee that's straight black coffee no sugar no sweeteners none needed and i'm going to need you to get ready to learn some cool new stuff since this is a new tutorial series i should probably give you a little background on it and that is is that this series of lessons is geared towards the absolute beginner what does that mean that means that i am not assuming that you already have your phd from mit in computer science what i'm assuming is is that you can listen and that you're willing to work hard and if you have those two characteristics i will teach you to code now we're going to go as we go through these lessons we're going to end up being able to do some really sophisticated and some really advanced projects this is going to be a project based class but i'm going to start with the very basics and the very fundamentals now if you do already have your phd in computer science from mit don't complain that this lesson is starting with the basics you can either go find a different set of tutorials that moves faster or you can sort of skip ahead to some of the later lessons but i don't want to leave anyone behind so i am starting with the absolute basics does that make sense i hope it does okay so if we're going to learn python we're going to start with the very very basics and that is going to be how to install the python software good news is it is free so it won't cost anything doesn't cost you anything to take this class and doesn't cost you anything to get the python software so you should be good to go your budget will not keep you from being successful in this series of lessons so what i will need you to do if we are going to do this i am going to need you to go ahead and call up a browser and i think i will get out of your way a little bit here go ahead and call up a browser and what i need you to search on is i like the most excellent duck duck go but you can use whichever search engine you like i'm going to search on install python okay and this first result here for me is download python from python.org that sounds like a pretty good uh a pretty good place to be and i am operating on a windows machine so i will be downloading the windows version i am on windows 10 now if you happen to be on linux you might have to poke around a little bit here and like maybe go to the linux unix installation or the mac os installation but you should be able to find your way to it now because this might be online for some time i want you to see that i am installing python 3.9.1 the safest thing probably for you would be to download the same version that i am on if you're looking at this four years in the future and they're up to python 6.7 or something if you want these lessons to work flawlessly you probably need to come back and you need to find an earlier version so that you are using exactly the same version that i am using but enough of this talking let's go ahead and see if we can download this so i will click on download python 3.9.1 and what i can see down here which you probably cannot see is let me resize my windows a little bit here so you get my same view that looks good you can see in the chrome browser down here on the left it is giving me an exe file which then will allow me to do the installation so by clicking on download python 3.9.1 i now in my lower left corner here have the executable that will actually install the software so let's click on that hope for the best okay it is now asking me do you want to install now now this is really really important you want to install the launcher for all users at least you know that's useful but what's really really really important is you need to click on this little thing that says add python 3.9 to path now in windows you has you have a path that helps windows find its way to your software and since in the future you might be having other programs that might be using python you want to make sure that that python file is in your path name so you want to yes put the check in add python 3.9 to path and then with that i think that we want to do the install now which includes all the bells and whistles with it so i will click install now then it asks me do you really want to install i said yes i wouldn't have asked if i didn't want to and now we just sit back and wait for a few seconds while this installs it's looking good so far we can just sit and chat as this is installing because i don't like to pause start pause start if i gotta wait you gotta wait too so we'll just wait and let this thing install this is pretty easy so far right told you it was for the absolute beginner and we are working our way along here not having any problems at all so far and we are almost there almost there again i'm on windows 10 and you can see that it's installing the 64-bit for me okay new to python start with the online tutorial we don't need no stinkin online tutorial because i'm going to show you how to use this and now we're going to go ahead and click close that looks good now i need you to go to the lower left in windows in your little search bar and i need you to type python okay and you can see that there's this python 3.9 that would open python in like a command window and there is a little bit nicer environment and that is your python idle okay do you see that i'm selecting idle python 3.9 64-bit so i am choosing to open idle okay and now the idle has opened up which is the python shell okay this is the python shell and i'll get it full screen here and you can see that i've got a pretty large font there so you won't have any trouble reading as i'm typing if you want to adjust that you can come in and say configure idle that was under options and then you can set your font size here under size and that will allow you to have something nice and big and easy to read i think the default installation the font is too small so i set it up to a big uh up to a big value okay we are ready to start playing around with python that went pretty fast that went pretty easy right now one of the first things that you're going to notice is okay again i assume that you don't have any programming experience but i assume that you probably got here from my arduino lessons and you've probably done a little coding in arduino i'm assuming that now if you haven't that's okay you can learn this without doing the arduino lessons first but i will sometimes compare and contrast python to arduino because i think a lot of you guys are going to end up here from my most excellent arduino series so one of the first things that i will say about python is it is execution ready now what does that mean well if you're programming in arduino you write the whole program and then you run the program and when you run it it goes and compiles it and then it runs that compiled code okay python is not like that python executes code one line at a time now you can stack the code together and run it but it compiles and executes one line of code at a time and what that means is is that you can just sit and type in python commands and it will run them as you type them later on we will write a formal program where you write it line by line by line and then run it but sometimes when you're learning some of the stuff it's fun to just play with it one line at a time which we are going to do now if you look here we have these three greater than signs that means that we are in the python shell and it is waiting for a python command okay it is waiting for a python command so first difference between python and arduino python is execution ready so we are going to write our first line of python code and as is the custom we are going to say print and then an open parenthesis and then it will print what is in quotes and so we will say hello world and close those quotes and then we will click enter boom first line of python that we have written you're only like five minutes into this and you've already written your first line of python code we said print hello world and it printed hello world now if we wanted to be a little bit fancier we could say we could say greeting is equal to and then in quotes hello hello world and then close those quotes and then we could say print reading boom hello world so now we have our first variable type we created a variable called greeting and that greeting contained the string hello world when we printed greeting it printed the string hello world now what you got to see is this then shows us a second difference between python and arduino and that is you don't have to declare your variables so like a lot of people who have been in the arduino world and come to the python world they say oh man i love it you don't have to declare your variables you just start using your variables well you're going to love it until you end up getting yourself in trouble because you're thinking you have one variable type and python is giving you a different variable type and so i must warn you this feature where you don't have to declare your variables will give you enough flexibility that you can kill yourself and so i say that since you don't declare your variables in python you have to be very very mindful of what type of variable you're using okay so let's play around a little bit more with it well what if we said print and in quotes reading like that what is going to happen well it is going to print the word greeting why because you printed the string greeting as indicated by putting it inside of the quotes instead of the variable greeting okay so don't confuse your variable names with a string itself let me i got to do a little windows maintenance here so that you will be able to see everything that i am doing give me just a second give me just a second here to get this straight okay i don't want my little title down there to cover up something that you see and this is giving me all kinds of trouble it is not letting me actually resize my window the window is staying there it is okay the window was saying staying the full height no matter what i did and at that full height my little title bar at the bottom of the screen would cover up what you were seeing okay so that should work so when i say greeting is equal to in quotes hello world because i'm setting greeting to something inside quotes it is making greeting a string type variable okay and then when i say print greeting it prints the greeting hello world it prints the string variable greeting which is hello world now what i could also say is i could also say like msg or what i could also do is i could say print oh let's do this let's say your name is bobby bobby okay and then i could print name what am i going to get i'm going to get bobby well i could also do a little bit nicer and i could say print and i could put the the string hello and then comma and then name and then comma and then welcome to python all right does that make sense what do you think that is going to do i think i'm going to need to get out of your way a little bit more so you can see all the code okay there it is so i'm going to say print hello comma name comma welcome to python let's try that and see what happens okay hello bobby welcome to python all right so do you see what's kind of cool here we can print more than one string by separating it with a comma and you can see that this was just printing the string hello this was pre printing the variable name that contains the string bobby and then it prints welcome to python so that is pretty cool we could also do it like msg1 is equal to hello and then msg2 is equal to well come to python like that and then we could print we could print msg1 we could print name we could print msg2 like that close it up and it says hello bobby welcome to python okay so you see we can print by stringing things together separated by commas we can print a string itself or we can print a variable that has a string in it now we are not limited we are not i like being a little bit larger i just feel like it's a little personal i don't like to be postage stamp in the bottom there like to be a little bit bigger a little bit more personable with you hope you guys are enjoying your coffee okay we can have more than just strings we can also have numbers and so i can say x is equal to 7 and then i could say print x and it prints seven pretty cool i could say print and then i could say your number is and then i could print x and then it would say your number is 7 that's pretty cool i could say y is equal to 2 okay and then i could say print y print y like that okay and then it prints two now watch this z is equal to x plus y so what are we doing now we're now starting to do some mathematical operators right we can do operations in python so i can say z is equal to x plus y and then i can say print z print z and it says nine because z was x plus y 7 plus 2 is 9 and then it prints c pretty cool right i could also say that z is equal to let me just try something else here uh i want to show you because like if i have print x you can see that x is an integer how do i know that it's an integer because it doesn't put a decimal point there it has made it an integer notice that if i said x is equal to 7.0 and then print x it prints 7.0 the floating point number so you've got to see that python is assigning variable types to your variables based on how you enter them oh he put in a string greeting should be a string oh he put in seven x should be an integer oh he put in 7.0 x should be a float okay but now what i want you to see is if i say z is equal to 3 divided by 2 if you did that in arduino it would say 3 is an integer 2 is an integer 3 divided by 2 is going to be an integer it would return the number 1 because it would run round 1.5 to an integer but here i say z is 3 divided by 2 and i say print z oops print z and it prints 1.5 so python sort of figures things out on the on it says hmm 3 is a integer and 2 is an integer but 3 divided by 2 is a float so i'm going to make z a float so it sort of defines decides on its own how what type of variable to make the what type of variable to make the uh your variables when you use them and that makes things really easy but remember this okay i want you to listen to me when you have unexpected behavior in python many times it's because you think the variable is one type and python thinks it's something else variable mismatch is one of the number one most difficult things to debug in python so you've got to keep that in mind okay so let's uh let's see what do we have print x x is seven print y y is two and so another thing that we could do is we could say z is equal to x divided by y and then i can say print z like that and then i see that i have 3.5 i can also kind of do some better formatting so i could say print and i could say in a string i could say print uh let's see let me do something else let me say z is equal to x plus y like that and then i could say print and then i could print x plus x plus y close that comma and then i could put x plus y equals and then close that and then i could put the z in there and then it says x plus y equal nine so we can start putting some formatting in those print statements what else could we do we could say print and i could do it a different way i could say print x and then i could print the string space plus space and then close that and then comma put in the variable y and then put in the string the string space equal space and then close that string and then comma and then put in the answer which is z let's see if that's going to work okay what do you guys think just stop and tell me like above it said x plus y is 9 which is true but now what it's going to say is 7 plus 2 equal 9 boom okay so you see we can start getting kind of fancy with our formatting on the print you see how easy it is how friendly the python is makes it really easy to get some of these formatted things okay so we can also do other operators we've done plus we could also say we could say z is equal to and uh let's see we would make it x minus y and then we could say print z and then that is 5. 7 minus 2 is 5. so that's good we can also multiply we can say z is equal to x times y and then print z okay z is fourteen seven times two is fourteen all right so i want you to see that the way you multiply is you use the little asterisk the star the shift eight for multiplication so we have added we have subtracted we have multiplied we can divide z is equal to the way you divide would be x and then forward slash y so z is equal to x divided by y print z and we get 3.5 so we have learned already we've learned how to print we've learned a little bit about variable types we've learned how to add subtract multiply and divide okay so that's pretty cool now there is another operator that is really i think very cool it is the mod operator and so like i could say z is equal to x and the mod is the percent side x mod y and what does that do that takes x it divides by y and it tells me what the remainder is so tell me x mod y remember x is 7 and y is 2 what is x mod y it is 1 because 7 divided by two is going to be three with a remainder of one okay because two times three is six and then you need one more to get seven so the mod function gives you the remainder okay so let's say that x is 9 and let's say y is 3 and z is x mod y what is z you tell me what is c z is zero there's no remainder because nine divided by three is three no remainder and so the mod is zero so what you could do is you could see if a number is is divisible by 3 you could test that it would be true if the mod of the first number by the second number is 0. if the remainder is 0 it is divisible okay hopefully that makes sense so what have we done here we have added we have subtracted we have multiplied and we have divided and we have learned a little bit about mod now there's one other thing that you might want to do and that is to do powers and so let's say that we have x is equal to 9 and let's say that you want to square that well what you would do is you would say y is equal to 2 and then you would say z is equal to x and then to raise it to a power you go star star so that is x raised to the y x raised to the y and then print z and what do we get 81 because 9 raised to the 2 is 81 we could also just do something like print 2 raised to the 3 what is 2 raised to the 3 well that's 2 times 2 times 2 that is 8. okay so we now know how to raise things to the power what if you wanted to take the square root of a number we could say print and then we could say 4 and then we could go star star and just to be safe i am going to use quotes here 1 divided by 2 to the one half right because taking the square root of the number is the same as raising it to the one-half and so 4 raised to the one-half is the square root of 4 which is 2. so we can take any root of a number if we wanted to make that let's say the the cube root of let's say the cube root of 27 print 27 how would how would i do cube root that would be raise it to the one over three and what times what times what is 27 it is three because three times three times 3 is 27 so we just took the cube root okay guys this has been really a fast lesson but i hope i didn't lose you because we kind of made it pretty simple that you have installed python you are operating in the execution ready mode which means it's executing your code one line at a time and you have gotten familiar with basic mathematical operations including add subtract multiply and divide the most excellent mod feature and the raising to a power the star star to raise to a power so that's pretty cool okay i want you guys i'm going to give you a homework assignment pause the video do the homework assignment then come back and i'll show you the solution all right do it yourself just so you can show that you've learned the things and then i will show you the solution because you really you've got to learn this and it looks really easy when i'm doing it but you need to do it without me doing it first in front of you and so the assignment is to set a radius and then like a circle with a certain radius and then calculate the circumference and then calculate the area of that circle and then print the answer out nicely formatted maybe you don't know how to calculate the area or the circumference of a circle go look it up right how do i calculate the area of a circle how do i calculate the circumference it's okay if you don't know it you can find it in two clicks on google but then come back and do the assignment okay pause the video do it on your own okay leave me a comment down below how many of you guys were able to be successful how many of you were able to to be successful and write your first little kind of assignment on your own without my help well let me show you how to do it i can set a radius i'll say radius is equal to two okay and then i can say circum circumference is equal to two times i'm sorry the circumference is 2 pi r what is pi 3.14 and then times r what is r it's the radius okay now understand this 2 is not the radius it's just the equation for the radius of a circle is 2 pi r pi is 3.14 so it's 2 times 3.14 times the radius okay and then i'm going to print the circumference and it says the circumference is 12.56 now it would be much nicer if i said print and then i said a sir of radius and then i'm going to close that and i'll get out of your way here a little further a circle of radius and then what do i put here ray d ray d us a circle of radius radius comma has circumference of and then i'll close that put a commas and then sir come for its okay let me move up this up so you will see the whole thing okay so i said a circle of radius comma radius has a circumference i'll put a period there of circumference and so let's look at that ah you know what i did i forgot to close that i forgot to close that out so i'll copy it i'll paste it okay dc i forgot to put my closing parenthesis and now look at that a circle of radius 2 has a circumference of 12.56 okay now let's calculate the area area is equal to pi 3.14 pi r squared 3.14 pi times what radius and then how do i square it star star 2 pi r squared now i'm going to print again a circle of radius and then i'm going to end that comma and then put in radius the variable has an error uh of then in that string okay in that string and then comma and then what area like that all right let's see if that works a circle of radius 2 has [Music] what did i do wrong area is equal to 3.4 you know this is just one of those really really quirky things where uh it comes out the same because the the because of the two so that was a really poor example let's do ray d us three okay radius equal three okay and now i'm going to just copy and play paste this area command okay area and now i'll print the area i will ctrl c to copy ctrl v and then it says a circle of radius 3 has an area of 28.26 and then i'm going to do the circumference as well from up here 2 pi r just to show you that you're going to get a different answer this time circumference is 2 times 3.14 times radius and then now i am going to print the circumference command so i will copy that one just to show you that the area and i copied a bad line of code so i will paste and then close the parentheses okay so the the a circle of radius 3 has a circumference of 18.84 and it has a radius of 28.26 so that's just to show you that in fact you do get different uh different circumferences and areas for most circles except the peculiar one that i used as an example which was a radius of two okay guys we have learned some pretty cool stuff today we've learned how to install python we've learned how to run python programs one command at a time we've learned how to print we've learned how to format our prints and we have learned how to do basic mathematical operators of add subtract multiply divide the most excellent mod function and raising something to a power using the star star also how to take square roots and cube roots raising to a one over the root that you want to find and so i would say that we've had a pretty productive day today and hopefully you've had as much fun taking this lesson as i have making the lesson and what we're going to do next week is our lesson next week is we are going to play around with another variable type and that is the variable type of lists or arrays and the way to think about an array is so far like if i have x x is like a bucket with the name x on it and then you put a value in the bucket so if you look at x it tells you what's inside of x but you can only have one number inside the bucket and that bucket is has a label on it and you can only have one number at a time inside of that bucket well what we're going to learn next week is we're going to learn about arrays and arrays would be like a variable name like x but instead of a bucket what an array has is it's more like a filing cabinet with slots and you can put lots of different numbers in that one variable because there's different slots there's the first slot second slot third slot fourth slot so i could say show me x and show me what's in the fourth slot so that's just really really powerful because if you think of something like you want to keep track of grades you wouldn't want to have grade one grade two grade three grade four you'd have one variable named grades and then you had multiple slots and you could say grade slot one grade slot two grade slot three and so very very powerful thing is being able to deal with arrays and most programs you do in the future you're going to want to be able to use arrays and so i'm really trying to lay the foundation here we learned about variables and printing today how to install the software next week we are going to learn about a raise okay guys really appreciate you tuning in we're going to have a lot of fun in this and again we're going to get some really really sophisticated stuff but i'm taking you there step by step paul mcquarter from toptechboy.com i will talk to you guys later
Info
Channel: Paul McWhorter
Views: 79,708
Rating: undefined out of 5
Keywords:
Id: XCKWZAtKTnU
Channel Id: undefined
Length: 36min 3sec (2163 seconds)
Published: Wed Feb 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.