Python 3 Crash Course: Learn Python 3 in 1 hour.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to yet another Python crash course just a heads up this crash course is very fast-paced it's mostly designed for people coming into Python from another language such as C Java PHP JavaScript and you are already familiar with your primary coding language but you want to learn Python so in this course I'm actually not covering every single topic known to humanity this is after all a crash course not a let's go into detail course so in this very fast-paced crash course we're going to skim a lot of subjects here so we're going to look at where Python is used how to install Python how to run Python code loading an experimental Python shell ipython how to do some math how variables work we're gonna look at some Python syntax we'll use indentation it's a little weird at first if you're brand new to Python but it is lovely once you get used to it we're going to look at different ways to format your strings conditional statements like if else statements basically telling your program what to do based on certain conditions we're gonna look at loops functions scope classes packages error flow with try and accept and a bunch of stuff in between so if you're looking for a fast-paced Python crash course this is it this is the course that you want now if you're brand new to Python I would highly suggest not watching this this is going to be too fast and too high level to really understand too much again this this crash course is really designed for people coming in from Java or C or some other language where you've been fairly involved and you already know about things like functions data types and loops and things like that so once more just as a warning if you're coming in from another language that you're pretty confident in this is good for you hello and welcome to the Python 3 crash course now I've mentioned this before I'm going to mention it again and over time I'm gonna mention it more but this course is not a super in-depth course this is a crash course you're not going to learn everything how but you're going to learn everything that's important very very quickly it is a very high paced course so if I ever go too fast you might want to do a little bit of research on your own and that's ok so first things first where is Python used Python is used in all sorts of places Python is used in let's make that a little smaller it's using things like machine learning artificial intelligence data science websites video games you know self-driving cars pretty much anything desktop programs all sorts of database management not necessarily to create databases but to work with databases there's all sorts of things that Python can do so basically if you're thinking hmm is this software related is this software related if the answer is yes then chances are Python is involved in that industry somehow so self-driving cars is Python involved probably what about data science you know how everyone hates the fact that Facebook is collecting so much information on us all the time data science python is used for a lot of that as well Google is using Python I believe YouTube was originally created using Python Instagram was definitely created using Python used a Python framework called Django so basically where is Python used the answer is basically everywhere just about everywhere okay if you don't have Python installed on your computer actually you probably do but if you don't or if you want the newer version of Python you can open up your browser and go to python.org so you'll see a website that looks basically like it's never changed throughout time click on downloads and you can always just click the latest version it will try to detect your computer for you so if you're using Linux or Windows or Mac you can just click this button and it will download the latest version for you and then you can install it that's really all there is to installing Python all right in Python you are going to see files that end in dot py and there is a particular way to run these files so once you have python up and running and it's installed on your computer you can open up your command line I'm just using vs codes I've got my command line open it open here my terminal but you don't have to use that you can also use terminal commander power shell bash anything else and really all you have to do is I'm just gonna close this because I don't wanna use that one all you have to do is type Python and then your file pi so let's create an example here I'm gonna create a new file and just save this as how to run a Python file dot PI ends in dot PI and in here I'm just gonna say print hello world and that is literally it nothing fancy now to execute this file first flocking to Lela see I've got a file in here called how do you run a Python file hi so Python and then I just type H o W hit tab and autocompletes for me if it doesn't autocomplete you can copy and paste the name or you can type the whole name out by hand and it says in here hello world Python comes with this thing called a shell it's actually got a few different names the most common one that I've seen lately is called a shell and basically all it is is an interactive place where you can play with Python and so you can open up your command line program could be terminal could be bash it could be anything and literally just type the word Python so if you if you take Python - V this will show you which version of Python you're using and you can just type Python and it gets you into this Python shell and in here you can write regular Python so you do things like 5 plus 11 is 16 you can also print hello fella and it prints it for you so you can actually type straight to Python in here you don't necessarily have to type your Python in a dot 2 pie file and then execute it every single time like we saw just a little bit earlier so that's how we get in if we want to get out we can literally just type the word exit with parentheses it's a function helps us get out all right let's talk about some basic math and python and this is pretty global for most programming languages so if we just get into our Python shell here we can do some simple addition so you know 10 plus 10 is going to be 20 now we can do some subtraction as well so addition we used the plus sign and I just made that a little bigger so if subtraction is just a dash the minus sign so let's say 91 minus 45 it's 46 to multiply you can do 8 and then an asterisk 8 so that's the star symbol that'll give us 64 8 times 8 is 64 I to divide we could do 64 / 8 and that's how we divide and that's going to give us 8.0 and it's 8 as well we can do exponents as well so if we wanted to do 3 to the power of 3 we could do 3 star star 3 so 2 asterisks side-by-side means 3 to the power of 3 so this is going to be 3 times 3 is 9 times 3 again is going to be 27 lastly we have this really interesting one called modulus and that basically just gives us a remainder so you know how you do like 10 divided by 3 and you get this remainder that point 3 3 3 3 3 3 3 5 computer science quirk for you but if you wanted to get the remainder you could do % that's modulus 3 and what this says is 3 goes into 10 three times doesn't care about that throws that answer away and this will return the number one because there's a remainder of 1 so 3 6 9 and then 3 can't go into the remainder of whatever that's going to be so 10 minus 9 is going to be one that's your remainder alright let's talk about variables variables are pieces of data stored in your computer's memory basically we give pieces of memory names in computers because humans just can't remember everything computers are made to remember everything human brains suck at it so if we get into Python here we can create a variable name is equal to Python crash course and basically what this did was it took this value in here and it said okay allocate a little piece of memory behind the scenes in my computer and then just give me like a a pseudo name like a code name called name and it doesn't have to be called name we could have named it anything else but down the road we can then access it by just typing name we could print name as well we could see the type of name we'll get into these a little bit later you can see that's a string but basically we can manipulate this now we have stored this value in something that's easier to type okay let's talk about indentation in other languages you see us use these curly brackets all the time I mean once you learn indentation with Python you're going to look at these curly braces in JavaScript and other languages like PHP and you're gonna think wow how did I ever live with that but at first it's going to be a little bit weird so in Python let's just get into our shell here we don't write things like if name is equal to Python and then a curly brace that gives us a syntax error Python does not like that what we do do in satin is let's create a name here called Python just a regular variable we do say if name is equal to Python and then a colon and then we can do some indentation so we could indent 1 2 3 or 4 typically we indent 4 in Python and then we could say your name is Python and that executes for us then prints your name is Python so we don't use curly braces around this we don't have to instead we use colons we use a lot of colons and we also use indentation so often the tab key will work or just four spaces typically I'll use the tab key but I'll set my editor up to understand that tab actually means 4 spaces it doesn't have to but generally that's what you're going to see now one thing to note is anything that's indented under sort of like this line that has a colon in it that means this belongs to this so this cannot run if this does not return true this whatever this is maybe it's going to create a new file maybe it's going to do something different we don't know yet but whatever this is means it is reliant on this either being true or being executed or anything like that so we can get into that a little bit later I think all right you know what I've been doing this a lot and honestly this is gross because there's like no syntax highlighting or anything so what you can do is you can do pip install ipython I should already have this in here yep and then I can just type ipython and look at that I am inside of ipython and it has like nice little syntax highlighting and works with indentation and all sorts of stuff isolates from now on start using ipython I'm going to use ipython if you don't want to that's fine you don't have to but I'm going to just because like gives me something nice if name is equal to high print hihihihihi and just looks a little nicer so I'm going to start using that from now on now let's take a look at data types so let's open up ipython or just regular Python if you don't want to use interactive Python that's cool too we have these things called data types so we have different ones we've got strings lists dictionaries tuples sets boolean's and many many many more but these are probably the most common ones you're going to work with day in and day out so it's important to know these so a string is basically a sentence named Caleb Colleen it has a space in there it has basically regular characters that you would normally type on your keyboard and it starts with an opening and closing quotation it doesn't have to start with that it could also open and close with an apostrophe but whichever one you use they have to be the same so has to start with an end with the exact same one then if we did type name we're going to see that this is in fact a string and inside of a string you can put anything in there now unlike a variable you can't just name a variable anything you can't just say like a t4 underscore name is equal to thing this just doesn't work that's not a good variable name just stick with regular boring variable names and variable characters and that's really all you're going to have to do to use variables so we looked at name that's a string next we have lists so you know a list could be like your groceries is equal to and it opens and closes with hard brackets like this and we could do I don't know milk eggs and ice cream because I'm an adult and that's what I want for breakfast I guess then we could do groceries and this stores several different dairy data types in here so it says three items milk eggs and ice cream and we can see that it comes back as a milk eggs and ice cream then we can loop over that a little bit later and if we do type groceries we'll see it's a list we'll talk more about loops and things like that a little bit later next we have dictionaries so we could have a dictionary and this takes a key value pair and it opens and closes with curly braces this is one of the times you're going to see curly braces in Python so we have this thing called a key value pair and we could say the name is Caleb and the language is Python of an interesting syntax we can hit enter person we can also get details out of this so we can say person was a hard bracket and then get that name and that's going to be Caleb or if we do the same thing with language this is going to be Python so it's sort of like a variable inside of a variable with a naming convention added dictionaries are also super fast and Python so it's a really good way of storing similar data next we have these things called tuples or tuples not really sure people seem to say that differently but basically we have groceries but a tuple would be like a list that you cannot change so your grocery list when you go to the store maybe you see a chocolate bar that you like maybe you want to pick up oranges or bananas or something well you just change your list but a topple cannot change and you have two kids and that's all you ever want so you've got one called Nathan and you've got one called Zephir so that is a tuple it starts with and ends with regular parentheses and if we do type kids it's a tupple now the thing is Topol cannot change we cannot remove an item we cannot add an item it is there forever until we remove the variable or overwrite the in for the variable entirely whereas a regular list we can totally change it next up we have these things called sets now a set is a lot like a list and a lot like a tupple in some degree it's some form in the middle so a set can have items added and remove just like a regular list not really like a couple at all except for the fact that it holds multiple items but a set is totally unique so if we said foods is equal to and a set looks like this it looks a lot like a dictionary but there's no key value pair in there so Foods is equal to pizza tacos ice cream and let's say we had pizza in there twice and pizza in there three times because my diet is made of pizza and tacos apparently I hit enter on that and I saved that into a variable called foods and when I take foods in here you're going to see that ice cream pizza and taco show up now this is interesting because ice cream was not the first item in there it did not remember my order tuples and lists do pizza showed up three times a set said no you don't need it three times this is a unique list so you can have pizza once taco showed up twice but in the set it was like you don't need tacos twice it's the same item so just make sure it's unique so it only showed ice cream peace and talk was once each despite there being numerous items in there last but not least we have these things called boolean x' and a boolean is literally just a true or false with a capital T or a capital F so m and am I an adult that's true is adult type is adult it's a bool short for boolean until I can ever only be true or false you now that's data types in a nutshell there's actually a lot more and there's a lot more that comes with each one but again remember this is a crash course a fast-paced crash course and we're not going to be going in depth too much about how all of these work individually all right let's take a look at indexing so I'm going to open up ipython or regular Python if you want to use regular Python indexing is a way to basically pull out a particular item from a list of things so let's say we have courses equal to Python 3 crash course we can see that this is in fact a string and let's say we just wanted to get a particular letter let's say we wanted to get number 3 well in computers we start counting at 0 so we go 0 1 2 3 4 5 6 7 so let's go ahead and type the course 7 and we get the 3 in there now let's say we wanted to just get the word crash we could do 1 2 3 4 5 6 7 8 9 and then 10 11 12 13 14 and then up to 15 so let's go ahead and do a course and what I say 10 and 15 nope that was wrong 9 in 14 there we go I just counted poorly the first time I probably just missed something in there how but that is basically indexing on a string so you can get an individual character just by counting the numbers in there again it starts with zero or you can get a whole range of characters by using a colon so where it starts and where it should go up to next we also can index lists and tuples so we could do foods is a list we've got pizza we've got dr. pepper popsicles and we have I don't know chicken I guess so let's say we wanted to get the first item we would do foods 0 that's the first item oh it's Pizza computers start counting at zero for the most part and that's what we're gonna see here let's say we wanted to get this item all the way to the end we could do number one because as one two three and just leave that empty and that gives us the rest of them so it's saying start at number one and then go to basically the end we could do the opposite as well we could do foods start anywhere and then go up to do number one so it's going to say okay we'll get everything up until item number one and last but not least we can also do a full range in here so we can get like just dr. pepper popsicles and chicken if we wanted to with foods 1 & 2 nope that was a lie foods oh I'm making so many typos here we actually want foods 1 2 3 because it goes up into that point so 0 1 we also want number 2 and go up to number 3 number 3 doesn't exist so it's just going to hit the end of the list for us and there we go okay let's talk about code comments code comments are really really easy in Python we don't really have too many different ways to make code comments we have two primary ways I so you could have some code here but if you wanted to comment this out you just use a number sign in front of it and Python will not execute it versus some code here and if we hit enter it gives us invalid syntax so if ever you want to leave some notes which you probably should in your code you're going to want to leave comments next you can also leave comments in the form of a doc string so this is sort of getting ahead of ourselves for the moment but if we have a function called thing we could add a doc string in here hello world also notice the indenting so everything under here belongs to the function called thing this is called a doc string it's a form of commenting you can use a three quotation marks you can use three apostrophes as long as it ends and opens with the exact same thing so quotations or apostrophes you can have whatever you want in the middle and it works on multiple lines let's take a look at string formatting quickly I'm gonna open up ipython you can open up regular Python and string formatting there's so many different ways to format strings in Python it's actually pretty annoying it's one of the things that drives me nuts about Python but modern Python has some really nice things so python 3.5 so python 3.5 has this feature called format and we can format strings so we could say the course is equal to some course for everybody and then we could format that with Python or any other variable so actually even as a better example let's do this name is equal to Python and let's go to the end here and let's not put another string in here with formatting let's put a name in here so of course is equal to and whatever the opening and curly braces are again this is one of those weird times where you'll see curly braces is in string formatting it's basically going to run this format function on it and it's going to inject name which is Python into here name is still Python courses Python for everybody now that's Python 3.5 and up in python 3.6 and up we have these things called f strings f string is really really nice so we've got this name called python and we could say print and we put an f in front of our string and then we open and close our string we put whatever we want in here the crash-course language is and then in here we can just put an opening and curly brace opening and closing curly brace with the variable name inside of it so it automatically knows that this belongs to the name variable up there and we can just hit BAM hit enter and this crash course is language Python basically it just took the name and it safely injected it in there for us now there's actually a lot to unpack in there the he F string and formatting can get very very complicated but from Python 3.5 and forward you're going to see a lot of that you also might see something like print hello s and then world you'll see this in older versions of Python as well where basically we said here's a string what this whole thing is a string but we're going to use a string in there and the first value using the percent sign is going to be world we could do the same thing with a variable hello Python now again this is older Python but you're still going to see that and again there's a lot to unpack in there really we're just scratching the surface here but you're going to see F strings a lot in Python 3.6 and newer you're going to see the dot format method a lot in Python 3.5 and newer and you'll see this in older versions of Python let's go ahead and create and read some files now so you can actually see that I'm running this in my folder called Python 3 crash course inside of my Dropbox and I've got one file in here we can verify that in here so when I execute Python or ipython it's going to run in this same folder and if I run PWD from ipython you can actually see that that's exactly where I'm at so let's go ahead and create a new file we're going to create a new file using the with a keyword and the open keyword and we're gonna create a file called file txt actually know what let's do something cooler let's create a file a Python file that we can actually execute a little bit later so try me PI that's the file name and we're going to open up this file with the the right permissions so basically we're going to create a new file and we're going to write to it as file handler and that file handler is not the actual file itself it's just a handler to help us manage the file so then we can save file handler dot write print hello world lalala lalalalala just to show us something in here file underscore handler dot close and let's go ahead and run that and you can see my file try me dot pi showed up let's just move that down and it says print hello world actually that print should have been a lowercase P print hello world lalalalala now if we wanted to read this we could do the same thing we could put this into a variable in our Python as well so we could say with open try me PI this file needs to exist already with the reading permission as file handler or F H for short F H dot read actually we want to do content is equal to FH read and then we always want to close that file hand there just a good practice to do that anyways so FH close let's try that again f h dot close nailed it and let's type content print content we can see that in there as well so that is opening a file creating a new file and reading a file if we wanted to append it to this file we could do that as well so we could say with open try meat pie a for a pen so add this to the back of the file as file handler FH dot write testing line number to FH close and I made a little mistake in there I forgot to make a new line actually with a slash n for new line so let's go ahead and this is going to be lying to originally mm-hm that was what my example was supposed to be but I forgot that slash n which is an invisible character you don't actually see it in your files but it tells your computer to make a new line so let's go ahead and execute this and there we go so I messed up there because I forgot the hidden slash n and it should have gone to a new line there but you know regardless it's still appended to the file twice so the example still actually pretty good all right let's take a look at comparison operators I'm going to open up ipython again and a comparison operator really is just checking to see if something is or is not true or if something can be executed so we can say the Lang is equal to Python 3 let's make this a string though so we have Lang is equal to Python 3 now we can say if Lang is equal to Ruby print you're using Ruby we can also say else if L if the Lang is equal to Python to print a print so my god please upgrade kay thanks bye because python 2 is old we can also say else print anything else the value was and we can use an F string in here lang now we just want to make sure that that is an F string with the character F in front of it let's go ahead and execute this code and we're going to see it's anything else with the value that was Python 3 so basically this is going to say if the code is equal to Ruby then print this it wasn't if the Lang is equal to Python 2 then so my god please upgrade kay thanks bye but it didn't match either of these and so this is the one that was executed now if we go ahead and change laying again we can actually up laying is equal to let's do Python 2 and let's rerun this again and now it runs the Python 2 line and if we go all the way up again and let's change this to Ruby which is alive we're not using Ruby this one is going to say your using ruby which it is and so basically this is an if statement a conditional or a comparison operator and it's saying that it has to be an exact match this isn't else--if saying it also has to be an exact match so this is comparing whatever the variable value of Lang is which we know in this case was Ruby 2 this value here now if that isn't a match Python says skip it don't run this indented code go to the next one says okay well we've got out dented code if the Lang is then equal to Python 2 didn't run this well in our first example it was not that so it skipped it then a sudden for every other scenario just print it we don't really care what it is we just need to have like a catch-all behind the scenes now we have all sorts of comparison operators in here we have we can do like age is equal to 20 yeah I wish and we could say if age is equal to 20 print it's nice being 20 okay so that's an exact comparison we could also say if age is greater than or equal to 10 print you are older than 10 now that's going to execute but what if we said no the age has to be higher than 30 the age is currently 20 which we can see up here and this is not going to run for us it says basically if 20 is greater than or equal to 30 then run this code but that's not true hmm number 20 is not greater than or equal to number 30 so Python says nope that's not true don't execute that we also have a few other comparison operators in here so we've got the equals to so make sure it's the exact same greater than equal to that's good for numbers just a standard greater than standard less than a standard less than is equal to and we have a is not operator so let's see what ages ages 20 so we can say if age does not equal to exactly 30 print you are not exactly 30 we can see that executed because age does not equal 30 20 does not equal 30 is that right 20 and 30 are not the same that's correct I know it's weird logic to sort of think that way but that's computer programming in a nutshell print you are not exactly 30 there's a bunch of other comparison operators that Python comes with as well such as is which is a really cool one we're not going to get into that because this is a crash course but you can also you know really try like total is equal to none so if we did this type total it's a nun type it's a data type let me say if total is none print is none is none so there is a difference between is and the two equal signs but we're not gonna get into that right now for more explanation on that however I do have a Python for everybody course it is a larger course that goes over a lot of the stuff that we are just flying by right now so if you're interested in a larger Python course definitely check that out it goes a lot lot slower I talk more about these things more in depth as well okay let's talk about for loops you're going to see these everywhere these are very very important so we could have a list like groceries which doesn't currently exist in this instance there we go so I just did a reverse search there with ctrl R let's clear that make some room groceries so we have this list of groceries and in a for loop we can actually loop through each one and just print them out or or do anything we want so we can say for item in groceries and that's going to basically assign item to milk and then when it's done is going to assign item to eggs and then when it's done it's going to assign item to ice-cream and so we can say print F the item is and then whatever the item is going to be and that's an F string it says the item is milk the item is eggs the items the item is icecream now basically a for loop is just going to go through an iterable as much as it can until it hits the very end now an iterable could be a set could be a set it could be a couple strings are also iterables could be a list there are all sorts of iterables out there so we're not going to get too much into all the intervals because that's like getting into all the data types there's a lot out there and if it's an iterable you can loop through it so we can actually say name is equal to Python 3 crash-course and there's a space in there so that's kind of annoying but what we can do now is name do a trim and it's not trim I'm pretty sure that was just JavaScript its name don't strip nailed it so now it doesn't have spaces I just stripped the space from the ends of either side of that string so that's a cool little thing we can do for letter in name print letter okay I turned out kind of gross but it does say Python 3 crash course one letter at a time including all the spaces so let's do one more example where we can break and continue so let's do 4 letter in name and whatever that letter is going to be where it's going to take letter dot lowercase just going to turn like the PC and C all into lowercase letters and then we're going to put it in a variable called L then we can say if L is in some sort of list so let's list so let's list out the variables AE I owe you and we'll also include Y just for funsies sake and then we can say print F string vowel is and then whatever l is going to be and then continue so basically we're saying if the lowercase letter is a vowel then print it out and just continue the loop so let's see what this looks like now okay and I actually made a weird little error there I don't want to look for it in a list I want to look for L in the string AEIOU NY so let's go ahead and run this one then says Val is why there's a wine Python there's an O in Python there's an A in the word crash-course and in the word course there's an O U and E so basically it's just printing out all the ballots for us we can also go ahead and say if L is equal to exactly 3 because we had the name Python 3 crash course break and we could do a print in here print breaking on 3 and so it's going to go through all the vowels it's gonna try to print them out and then it says you know if that letter is actually three then break basically quit the loop quit working just exit out of the loop and keep running regular code so continue is how we cycle through another loop iteration so that we don't have to actually you know continue all this code continue says okay once that code is triggered don't worry about this stuff break on the other hand is as soon as break is hit it actually exits the for loop entirely and both continue and break are useful on the next type of loop called a while loop which we'll talk about in just a second okay let's take a look at some while loops here in the last video we did for letter and name and then print the letter that's cool and it's going to go through every item in an iteration or an iterable rather about a while loop is going to go on forever so a while loop you actually need to tell it when to stop so we could basically say while something is true print a thing and this will run forever until you tell it to stop which is not generally what you want so I'm going to cancel that and I'm going to create some sort of stop or so number is equal to zero and we're gonna say while this is true so while that number is less than let's do ten print num and then we're also going to say num is equal to num plus one and what this is going to do is start at zero say 0 1 2 3 4 5 6 7 8 9 and just like that and then it stops and that's because at some point num was actually the number 10 so this only works if number is less than 10 now it doesn't know when to stop you have to tell it when to stop you can use numbers you can use boolean's you can use all sorts of stuff but essentially it's going to run forever while you have this running and you can actually see that gonna continue to run forever until I actually hit control-c and cancel okay let's take a look at list comprehensions list comprehensions are basically how we generate a list in a single line of code and we can do all sorts of stuff with this these are really common in Python you're gonna see these everywhere so do we still have groceries we do but now maybe let's not do that let's create a new list of nums and we've got one two three four and let's skip over to seven eight nine and ten you can see that we have 1 2 3 4 6 7 8 9 10 let's say we wanted to do something here we wanted to get all of these numbers times 10 so times 10 we could do this for numb in nums just like a regular for loop but over here on the left we want to produce an answer so this is just going to return each number and maybe let's take a look at this times 10 this is going to be the exact same as nums at this point so nothing is different between out 37 and out 39 but we could say num times 10 times 10 and now it's multiplied all of them put each one of those numbers into a list for us so it's now 10 20 30 40 70 80 90 and 100 that is a list comprehension we can also if we want it to be a little more fancy with this at an if statement so we can also say only do this if the number is going to be even so whatever that number is going to be so if num has a modulus divided by 2 is equal to 0 so 5/2 does that have a remainder yes it does has a remainder of 1 so that's not going to work but for 4 divided by 2 does that have a remainder no because 2 goes into 4 2 times very smoothly so there's no remainder and this is going to only work for even numbers and times 10 and now we have 20 40 80 and 100 so that's list comprehensions in a nutshell let's talk about functions functions in Python are used with this particular syntax called de F like you're defining a function and then it just has some sort of name and parentheses and then it returns something returned as a keyword you can see that it's bold and green as well same same as def return a thing so now we can say name and because we're in basically a Python shell it's going to show us what this value is but it's actually not going to put it into a variable we can with the return keyword run this function just like that and put it into a variable so we call it my thing is equal to name as a function my thing is now equal to a thing whatever that returns now that is a function in a nutshell a very very basic nutshell we could also make this a lot more complicated we could say def greeting which is actually not super complicated but it's going to take a name and we're gonna say print name hello to you good sir and that f string and now we can do greeting Caleb and because that's a variable that's an argument we can now use that inside of this function and we can just pass it in as like a regular string or a variable or a list or anything like that now the thing about this is this is not using the return keyword so if I said greet is equal to greeting zephyr who is my cat it still prints this out for me but if we do greet there's nothing and if we do type of greet this returns a none type so by default functions return none now that's one argument let's go ahead and add another argument Def greeting we're just gonna overwrite the old one name is some name but the greeting itself is going to be a default of hello so now we can say return an F string with name in it and then greeting to you now let's run greeting Henry who is my other cat and I'm not going to specify a greeting so this one is just going to take the name but greeting is saying okay well if you don't specify this is going to be the default so let's run that and it says hello Henry hello to you and because we're using the return keyword we can put that in some sort of variable so let's put it as X is equal to greeting Bob we see nothing X is now I string we can do type of X is a string so a function can take in is 0 or more arguments it can have every arguments can have some sort of default value this is called a keyword argument and this is called a regular argument how you'll often see them called args and starstar kwargs but the idea is basically you have this function and you can give it any number of parameters it can do some sort of logic in here and it's supposed to spit out or return something by default it will return done but you can return a string a number float but float is a number an integer dictionary lists set tupple frozen set you name it you can basically return anything from a function in fact you can actually return a function from a function in Python all right let's take a look at scope variables are searched from within and then they move outside in the note side in which kind of a weird way of putting that but let's say we have a variable out here called name Tim we could have a function in your def hello print Tim and when we run hello it's not Tim okay the name was Tim the variable name was the name and when we run this it'll now say Tim so let's back up a step and let's do that properly name is equal to Tom def hi print name that's going to print this value whatever this is going to be dead and and we say hi and there we go Tom shows up now we didn't specify this name inside of here but if we did it would overwrite it so name is going to continue to exist outside of this function let's overwrite this function now with a new name a default name and instead of Tom we're going to use Michael and when we run hi it's going to run Michael by default so basically what it's saying here is there is a variable inside of your function called name by default it's called Michael but up here there is no variable in EUR being passed in as an argument into your function called name so it's going to look outside of it so your function looks inside of itself for the variable it's looking for if it doesn't find it it's going to look outside of the function for it now if it doesn't exist then we're going to run into a problem def test a thing print a variable that does not exist in here does not exist variable test a thing you can see that Python didn't actually execute a Python just said okay register that test a thing is a function but now that we're going to execute it we're going to get a name error test does not exist as not defined but let's do this let's make it exist does not and I spelt it as exit so we're just gonna go with that and we're gonna put Caleb in here and let's rerun this and now it works even though in our function we didn't specify that variable it's looking outside of it it said doesn't exist in here look outside the function okay it does exist in there now we can use that originally it said this function does not exist in here it also does not exist outside of this function I have no idea what you're referencing cannot figure it out okay let's take a look at some classes so in Python basically everything has an object and we can create our own objects so like how we saw before with some string dot format string is an object it's a class and dot format is a method so we can actually do something with that in Python we can create our own classes as well so we could say class person Pass and this is how we create a regular class and then we could say Caleb is equal to person with parentheses around it Caleb type of Caleb we're gonna see that type of Caleb is a person that's what I defined it as and when we just type Caleb it doesn't even have a name it knows that it's running this current script it's using the class called person and this is where it's actually stored in our memory inside of our computer classes can also have basically variables inside of them called attributes and functions inside of them called methods so we can create a new class called person and inside of it the default name could be Caleb and then we can say Caleb is equal to a new person and then if I do Caleb name it shows up with Caleb now this is referencing up here this was actually terrible example but this is referencing this part of this data point here it's not actually referencing this scene you tell the difference between them because one is a capital K and one is a lowercase K classes also have functions called methods so we can create a class person and we can give it a method in here of speak now in a class by default it always takes the first parameter as self so you're going to want to make sure you have it you can think of it as whenever you add self in there it's attaching itself to this class and that gives you access to other properties and methods and things like that inside of this class we'll talk about that in just a sec but basically we just want to say print I need tacos so now we can say hungry person is equal to a person and then hungry person dot speak and says I need tacos now let's create a better example of an actual person class let's create a person in here this comes with a dunder method a magic method called init and you can pass in anything but it always takes self as the first parameter so it can take a name you take an age and it can take a food that it wants to eat and then we can say self that name is equal to name this allows us to use this later self dot age is equal to age this allows us to use it later self dot food is equal to food just allows us to use it later then we can say def speak self and it's going to say in here print feed me more self dot food and this is an F string so we're going to want to make sure that we're using F at the beginning of that and let's PluralEyes that by adding an F we can also say def get name self print the this person is and we can just use a different type of print basically this only works the prints not necessarily with strings so we're gonna use a comma in here to use a second parameter in the print function and self dot name okay so let's create in me Caleb is equal to a person oh look at that three positional requirements and needs a name age and a food so let's go ahead and create Caleb xxx pizza mmm no tacos taco taco taco Caleb mmm sila class but we can do Caleb dot hit tab we get all sorts of information in here speak Caleb dot tab get name and says feed me more tacos eyes that's double pluralized I should have just wrote regular taco in there without the s and then get name the name of this person is Caleb now why is this important because I don't know where all that wins let's just hit up until I find it in it took name agent food and then we assigned it name agent food so that we could use a self-taught name self to age self dot food throughout this entire class and speak we said self dot food so once it was set here we could use it in here because we have access to self get name is using self name same thing we assigned to self that name is equal to whatever original name we put in there and then we can use self dot because it's taking itself as well last but not least there is one in here that I want to go over called string STR self and that indenting was weird so that executed it for me and in here we could say a return statement of self name so caleb is equal to and let's go get rid of that okay but if I do print Caleb and now actually prints the self name whereas regularly it just says that this is executing from the main file whatever file this is he's using the person class had some memory location but when you print the actual name with underscore underscore string underscore underscore or add under string it's going to return self dot name let's do a quick little demo of packages so in Python there's this thing called pip you can see your pip version by doing pip - V pip - V will show you your pip version I'm using Python three point seven point two and pip 18.1 technically both of those are older versions at time of recording but you know I'm aware and that's okay so to install a package you just do pip install and then some package name and you can get the package name from pi pi org so you do pip install and let's say you wanted to install Django you do pip install Django with a capital D and then you could do pip show Django when this shows me that the latest version that I can get is three point zero point five now let's say I want to get rid of this I'm gonna do pip uninstall Django mmhmm yep I don't want to delete that basically just get rid of the excess files in there and now I can do pip show Django again and nothing shows up because there are no packages called Django if you ever want to see all of your packages you could do pip freeze and this will show you all of your packages these are all the packages I have on my computer not into virtual end of if you're familiar with those these are just all the things that my computer tends to use with the packages that I use globally okay let's take a look at try and accept so let's hop back into ipython and try and accept is basically saying you're going to try some code and if it doesn't work you're going to then accept an error except it stands for exception so you're going to X except I'm saying accept the exception and then you can handle that error so if I try to do something that does not work in Python such as 1/0 I get this thing called a zero division error and if I try to run any code below this so if I wrote one divided by zero and then print hello world hello world is not going to show up because basically my program died at this point it said there's an error I don't know how to handle it and so in Python you need to learn how to handle that and you do that with a try and except so we can try 1 divided by 0 and then we can accept any sort of exception as E and we can print whatever e is we can also print typeof e and we didn't get that error anymore in fact we could do one more in here print this still runs and we can see that this still runs even though it got a division by zero error so why is that good well because now we can try basically any sort of complex things something we're not totally aware of so if we said hey user give me two numbers and the user says number one is 10 is the second number is zero and we try to divide those numbers we're going to get an error and what we could say is except this particular error we can say except zero division error print cannot do that sorry print program still runs normally and look at that basically this died and the code was not able to run now actually a better example of this is print show me and this is not going to work for us whatsoever so we can see that it actually died on line two here doing 10/0 this was not executed Python said there was an error on this line there was a zero division error specifically on this line don't execute this just hop out of this section and go straight into the exception area and then execute that code and because there was no other errors in here we can continue to run our program now why is that important users are dangerous that's why users are dangerous we try to add malicious data all over the point we try to break things especially as programmers were notoriously bad for this we try to break every single program that we work with we try to break websites we try to break programs to try to break Python code it's just sort of what a lot of us do and the thing is some people don't even know they're trying to do it so if I asked you for two numbers and I was just going to divide them and I said give me number one and you said 100 and then I asked you for number two and you said zero well my program is going to try to divide both of those numbers but it didn't know you're going to give a zero and that actually caused a problem and so by catching that error we can make sure that our program does not die all right so that's not everything to do with Python there's actually a lot more to do with Python such as importing modules and packages and things like that there's unit testing nested functions decorators generators virtual environments all sorts of things then we get into things like working with sockets databases frameworks like Django or flask or working with data science such as the pandas package or the numpy package there's a lot that we did not cover in this crash course and again it's because this is a crash course this is not meant to be a super in-depth course now for more details on pretty much everything we've learned about here and probably about another 80% more I do have a course called Python for everybody you can go to Python for everybody calm to access that it is hosted all over the place but check out the links on Python for everybody calm
Info
Channel: Coding For Everybody
Views: 20,740
Rating: undefined out of 5
Keywords: codingforeverybody.com, python, python 3, python 3.7, learn python, python course, python tutorials, python 3.8, crash course
Id: DcEfZeR71qg
Channel Id: undefined
Length: 59min 58sec (3598 seconds)
Published: Tue May 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.