Learn Python - Course for Beginners [Accelerated Tutorial]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone and welcome to the channel this video is a full course designed to take you from a beginner in Python to the intermediate level my style for this tutorial and all my other ones is very quick into the point so if you like this type of content feel free to subscribe to my channel or drop a like on this video with that being said I hope this course teaches you Python in the most effective and efficient way let's begin our first video of downloading Python hello everyone in this video we'll talk about how we can download Python and start using it you might already have Python installed in your system if you're using a Mac like I am so if you open up a terminal you might be able to type in Python and see a Python show that's because a lot of Mac books already come pre-installed with Python on them we see that this version though is Python 2.7 and this isn't what we want to use Python has two different versions Python 2 is the legacy version of Python and Python 3 is the new version we want to be sure to use the new version because this is what will be updated in the future so let's go ahead and close out of this and then we'll open up a window and pull up the page python org slash downloads and we see that the newest version of python right now is three point seven point three let's go ahead and download this and we see it downloading in the bottom left corner whenever it finishes we'll open it up we'll go through the installer a few moments later it looks like we've successfully installed Python three point seven we'll close this and move it to the trash now that we have Python 3 installed we should be able to open up a terminal again and this time instead of typing Python we'll type in Python 30 so this is denoting the Virgin Python three instead of the default factory Python version two we'll hit enter and now when we enter this into the terminal we see that we're running Python 3.7 and that means we're good to proceed we'll go ahead and close out of this and close out of this one as well now the next step that I want to do is to get a text editor let's go back over to our window and we'll type in atom IO and this is the text editor that I'll be using in this course so atom is just an open source text editor so that means that the community can build packages and themes for it to improve our experience using it we'll go ahead and click through and download this I already have it so I'll just open up my atom text editor this is what the default view of atom looks like we'll go ahead and click atom and then we'll drop down to preferences and there's a few packages that I would recommend to install for this course the first one is autocomplete - Python which is just an auto completion tool that we can use when were coding in Python and then platform IO - ID e - terminal what this does is it gives us this little terminal down here in the bottom left corner that we can open whenever we want this by far is one of the most helpful packages for me whenever I'm coding so I would encourage you to get those - and then the themes that I'm running if you want to follow along completely with how mine looks is one dark in the user interface theme and then for the syntax theme I use one dark as well you don't have to choose these themes and there's a lot of creative themes from the community as well that you can use but these themes are very nice for me because they show up brightly whenever I'm recording videos I'll go ahead and exit out of the settings and jump back up and create a new file I'll increase my font size first thing we should do is hit command s and we'll save this as a Python script so every Python script ends with the extension py will say example dottywot and click Save I already have one so I'll replace it now Adam automatically gives us a directory here as well so since I'm working in my desktop everything on my desktop appears here this is very useful for navigation whenever we have large projects but for now since we just have one script I'll go ahead and close it let's go ahead and create our first Python script so the very basic one that everyone does and that you have to do as well is using the print function a function is just denoted by the parentheses after a word so here we have the function print and we know it's a function because of the parentheses this function takes in whatever you pass here of the same data type and prints it to the terminal so let's say hello world well save her file we'll go ahead and go down to that terminal and then we'll type in Python through and then the name of the file so ours is example dot pi hit return and we see that the return to the terminal is the hello world that we passed through the print function here and congratulations you just wrote your first python script well keep on building on this but for this video it's important that we have Python downloaded and we have a text that it is ready to go I'll see you in the next one hey guys welcome back in this video we'll talk syntax and calm eating let's get started so back in our text editor here we have the print hello world statement from the last video now let's talk about how we can make a comment in our Python script a comment and a Python script is just the line of code that isn't executed so these are often notes for developers or programmers so we can leave notes in our Python scripts throughout to let us know what we were thinking at that time if we wanted to put a comment above our first line of code we could drop that line of code down and then use a number sign this number sign just two notes a comment and this line is not read whenever we execute the Python script what we'll do is we'll put the number sign and then try to always put one space after your comment then we'll pass in the comment that we want here so this is our first Python script and that's how we would make a comment in Python block comments in Python are done the same way so he wanted to do a block comment we could drop down put another pound sign and then put in another line of text this is a comment the more comments you put in your script the better it usually is because this means that someone else can pick up your Python script and understand what you were thinking whenever you were writing it since Python emphasizes the ability to be able to quickly look at a Python script and understand what's going on comets are always a good thing now let's talk about the syntax of Python Python is one of the few programming languages that uses whitespace let's say that we have a block of code that looks like this we have a variable X is equal to 1 and then we want to make a simple function so if x is greater than 0 we'll put a colon here now watch what happens in the text editor whenever I press ENTER it automatically spaces over for spaces so whenever we have an indented block of code we know that the control flow happens here we know that anything that happens here belongs to this if statement well use our function print and then we'll say X is greater than 0 we'll save your file we'll drop back down to our terminal well execute the script again by pressing up and then we'll hit return and now we see we get the return x is greater than 0 this is because X is equal to 1 so therefore it's greater than 0 so we get this command we see that if we were to change this different logic statement so X is less than zero hit save and execute again we see that we don't get the return in this case but we can drop down and put an else statement so else here we see is lined up with the if statement that just means that this new block of code doesn't belong to this if statement it's actually its own statement but whenever we drop down to the next line it should be indented which it is we see that anything we put here now belongs to the else statement so this is a new block of code well use the print function again X is less than zero now when we execute we see that we get that returned indentations can be thought of as just blocks of code here we have two blocks each control statement is at its own indentation level we see that the if is lined up with the else each block of code following the logic statement is at its own indentation level each indentation level is just four spaces from the last one here we may be tempted to hit tab whenever we're indenting but tabs may not be consistent across operating systems so it's encouraged that we use four spaces to do this indentation it's okay if you don't quite understand the logic of the if and else right now all that's important for this video is that we understand how we can make a comment and also how we can indent blocks of code in the next video we'll dive into variables and data types I'll see you then everyone welcome to the next video and this video will talk about variables and data types let's get started back in our Python script where we left off close the terminal and go ahead and delete everything in our Python script so let's go ahead and drop down and create a variable we already did this once where we set X equal to 1 right but variables in Python need to be descriptive right now since we're just starting out X as a variable is fine when the future a variable name should be something that actually makes sense this could be something like age may be name or something along those lines right now let's go ahead and use the age variable the first thing about variables is that we should type them with lower cases but here I put age with a lower case instead of age with an upper case with the capital a this may confuse people that have been using Python for a while because we tend to leave the capitalization to the class objects let's go ahead and change this to age with the lower case and this is now a variable the cool thing about Python is that we don't actually have to explicitly state what type of variable we have all we have to do is type in what we want this variable to equal you already saw how we set the last variable X equal to 1 we can do the same thing with age here let's save our file and then we'll drop down and use the print function again and now let's introduce a new function let's use the type function and then we'll pass in the variable that we want to this function will say age here what the type function does is it returns the data type of the variable that we pass to it let's open up our terminal again and type in Python 3 example dot pi make sure you have your file saved and then we'll hit execute and now we see that we return to class of an integer Python has a lot of different data types but let's just talk about three main ones right now integers are just positive or negative whole number values here since one doesn't have a decimal place it's an integer if we were to change this to 1.5 and execute our script again our class now changes to a flow a flow is just any number that includes a decimal point lastly let's talk about how we can make a string instead of typing in a number let's put in something called a delimiter which can either be a double quotation mark or a single quotation mark and then we'll pass in text drop down and executing and we see that we now have a string class this is something that a lot of beginners really enjoy about Python we didn't need to know anything other than what we wanted the value to be for a variable and other programming languages you would typically have to state what type this value is whenever you assign it to this variable in Python it does all the heavy lifting for us and automatically places a type to this value this can get us in trouble sometimes though because what if we want a float number and Python thinks it's an integer value let's drop down and say number we'll set this equal to the number one but let's say that we wanted this to be returned as a float number instead of an integer value we could just use something called casting in Python what we'll do is we'll say float and then use it as a function and pass in what we want to be a float number now instead of this returning as an integer it should return as a float type will drop down and say print type and then number when we execute we see that we're giving a float even though the default behavior in Python would have been an integer we have access to do this with string int and also float lastly let's talk about how we should create multi-word variables in Python let's drop down and say number and then for the second word we should put in an underscore to separate the two words so number and then let's say and age we'll set this variable equal to a tuple which we'll talk about later on in the course and we'll say number and age now we can drop down in print number and age when we do this we get the return of both of these in a tuple to our terminal don't worry if you don't quite understand this tuple yet we'll talk about that later on what we should take away from this lesson is that variables can be uppercase but we should keep them lowercase and separate multi-word variables using an underscore we also have access to a lot of different datatypes and were able to explicitly state those data types using casting only if we want to lastly Python makes it very easy for us to create variables because it automatically assigns a type depending on the value of the variable if you got all this you're good to move on to the next lesson we'll talk about composite data types I'll see you then everyone welcome back let's talk about composite data types in this lecture the first thing we need to talk about is an immutable and immutable data type simply put a mutable data type is something that we can change once we create it as a variable an immutable data type is one that we cannot change once we create the variable using that data type we'll talk about this more whenever we get to the specific data types but we just need to remember mutable means changeable and immutable means that we cannot change it but for now let's talk about the composite data types that we have available to us we can create a list a tuple a dictionary a frozen set and also a set let's go through the structure on each of these we'll start out with the list so we'll create a variable and say our list we'll set this equal and now a list is denoted using square brackets so we'll use two square brackets and everything that we put in between these square brackets belongs to the list so let's say that we want to pass in a and then put a comma B and then C well each of these composite data types do is allow us to put multiple values to one variable each one of these just allow us to do it in a different way for a list we're able to pass in multiple values and we're able to keep the ordering of the values in the list so the first value will always be a and the second value will always be B a list is also able to store multiple data types so if we were to put another comma and put 1.5 drop-down and say print our list save a file again and then we'll execute this script so python 3 and then example dot pi and we see that our list is returned in square brackets of multiple different data types we have strings here because we know the delimiter means a string and we also have a float number because we have a decimal place here a tuple is almost the same thing as a list except that it's immutable we'll talk about that later on in our list video but for right now we'll just look at how we create them so we'll say our tuple equals and then instead of square brackets here we'll use parentheses now we can do the same thing as we did before so we'll say a B and then also C will drop down and print our tuple execute it to the terminal we see that we get the same thing except it has parentheses instead of the square brackets now let's talk about a dictionary so we'll say our dictionary and then we'll set this equal to the curly brackets this time and instead of just having one single value in a dictionary a dictionary is composed of a value and a key so these pairs are what make up a dictionary we denote these by using the key first a colon and then the value so let's say our key is key one as a string and then our value will be value one now a dictionary is powerful because we can pass them multiple pairs into the dictionary for storage so we can say key 2 : and value 2 will print this out so our dictionary and we'll see what the return looks like the dictionary is returned the same way that we type it dictionaries are useful whenever we have multiple properties about something so let's say we change the key to name and then we'll use my name so we'll pass in Derrick and then we can change the second key to another property of almy so let's say key 2 will be equal to height and then we can say 6 foot now let's go ahead and talk about asset so we'll do this one first we'll drop down and say our set and then sets are created using curly brackets as well but instead of key value pairs it's just single elements so we'll say one the second one two and then the third one three will go down and print our set and when we print we see that we have a set here but what's the difference in using a set and using a list well asset is unordered that means it doesn't keep the order here so we see that 2 is in the first position instead of in the second position like how we put it here and then it also has to be unique if we were to include a repeat so let's put 2 again and say this we should only be returned one string to in our set here we see that's exactly what we get these are useful for doing unions or intersections or something of that nature now for our last one let's say our frozen set and a frozen set is just like a set in the same way that a tuple is just like a list a frozen set is just an immutable version of a set we can use frozen set and then asking the set value that we have will drop down and say print our frozen set and it should look very similar to her set we see that it does except that it has the parentheses around it denoting it as a frozen set well look at what we can do with mutable and immutable objects in the upcoming lectures for now we just need to know that there's a variety of different composite data types that we can use in our Python scripts I'll see you in the next video hey everyone let's talk about numbers in this lecture let's jump right in let's create a new file since I don't want to get rid of this one we'll say this one has example 2 pi close the terminal and now let's go ahead and import our first package called math to do this in Python all we need to type in is import and in the package everyone this one is just called math and the math package just gives us access to a lot of different operations that we can do on our numbers we'll drop down and we already know that we can create integer values and we can also create float values Python also gives us access to use complex numbers we'll say Z is equal to the real part of the number 2 and then plus 3 J the J here just denotes this number as a complex number everywhere to drop-down and say print type of the variable Z open up a terminal and execute a Python script so Python 3 example 2 del pi we see that the class is complex let's go ahead and close this and python it's very easy to do math operations because they're already built-in we can drop down and save result equals and then X plus y and print result to the terminal when we execute this we're given the addition of y plus X you see that Python doesn't care about these two data types being different if we wanted to do multiplication we could use an asteroid if we wanted subtraction we could use a hyphen and if we want a division we could use a slash these are all built into Python already so we don't have to do anything special to use these however let's talk about how we can use the math function that we did need to import whenever we imported this into our Python script it gave us access to a lot of new functions that we can use we can look at the documentation on the Python website but all of these represents some type of new function that we using our Python script well let's go ahead and use one of these let's say we want the ceiling so the smallest integer greater than or equal to X we'll go over and to use that we saw that we need to type math dot seal and then pass in the value so let's drop down and say print math dust seal and then we'll pass in the value so let's pass in Y well save the file and execute and we see that the ceiling function did exactly what we thought it would and returned the lowest integer higher and then our number in this case that was two it's okay if you don't quite understand this part just we'll have a whole lecture on how we can start using packages that other people have built and using their functions inside of them so don't worry just yet if you don't understand this but what we should understand from this video is that we had three different data types and numbers where we have integers floats and complex numbers and we also have the ability to do simple math operations already built into Python such as addition and subtraction and that's pretty much it for this video I'll see you in the next one hey everyone in this video we'll cover Python lists let's get started in our text editor let's go ahead and open up a new file and I'll go ahead and close these out and that way we don't have them and now the first thing we need to do is let's create a list so we'll say our list and then remember we denote these using square brackets and then we pass in objects into these square brackets so let's say a and then we'll say B and then we'll say C's well I'll save our file that way we get the highlights so let's say list pod and remember that list can be a multi type container so we don't have to put in just string values we can put in integers Sloan umbers and any other data type that we want as well will drop down and say print our list saving this and executing it will say list up high and we see that we return to list in the terminal so we know how to create a list and we can do it just like this but if you remember a list is also a mutable object and that just means that we're able to change it once we create the variable here so if we wanted to change the Arliss variable we could drop down and say our list and then if we wanted to add something in we could append it so append parentheses and then we can append whatever we want here so let's say we wanted to pin the letter D if we were to print our list again well see that we have a new list in our terminal which we see that we do we now have the string D and our list value this is possible just because a list is mutable if we tried to do this with the tuple we wouldn't have the same result if you remember as well a list also keeps the order of the elements inside of no matter how many times we were to print our list this will be the order of these elements since this data type keeps the ordering that means that we're able to index values out of it indexing is just using the index position of an item inside of a data type to pull a specific element out of it let's try to only pull out the element a from our list we could drop down and say print our list and now anytime that we index values out of a data type we'll use a square bracket so we have our list which is the variable here and then we're using square brackets to pull out certain values within our variable so if we're only trying to pull out a Python starts in the zero index position what that means is that a is in the zero position so a would be zero B would be one C would be two and so forth to pull out a we use a zero so I'll put a zero here and now when we execute we should just be returned a which we are like I said a list keeps the order so that's what makes this possible it also makes it possible for us to insert elements at a certain position within our list will drop down and say our list and then we'll insert parenthesis and now we specify the index position of the place that we want to put the element so let's put it at the very beginning and now we'll put in the element we'll put in first element as a string now when we print our list we should have a first element string at the very beginning of the list which we do we're also able to remove that element from the list if we want to we can drop down and say our list not remove and then specify the element that we want to remove so let's specify first element and now when we print our list we shouldn't have that element or list anymore which we don't we also could have done this using the pop method if we wanted to remove a certain index position from our list we could have used pop we'll put parentheses and then we'll specify the index position that we want to remove so let's remove the last one D which is in the zero one two three four fifth index position well put it in a 5 here and now print our list again when we execute we see we no longer have the string of D in the last position of our list lastly we can nest lists inside of each other so let's say nested list is equal to another list and then we'll pass in a second square bracket which will be just another list inside of the first list so this will be the first list so let's say 1 2 3 and then the second list will be 34 5 all we're saying is that there's one big list with a smaller list inside of it well print nested lists to see what it looks like we see that it looks exactly like how we typed it in the terminal as well and that's it for this video it's important that we remember and that a list is a mutable object and that we're also able to keep the order of elements and our list I'll see you in the next video hey everyone let's talk about how we can use strings in our Python scripts let's get started over in our Python script I'm in a file called strings dot PI we've already been using string variables quite a bit but there's a little bit more that we need to talk about let's create a new variable called string and then if you remember we use a delimiter to specify a string a delimiter is either a double quote or a single quote we'll use double quotes here and now let's set this equal to a name so we'll just use mine Derrick it's very easy to create strings in Python however there's a lot happening behind the scenes this string in Python is actually an array so if we were to drop down and say print and then we'll use a new function called le in which just calculates the length of the array we'll say length parentheses and then string a now when we execute this we'll see what I'm talking about we get the return of 7 that's because each letter in my name is actually a single element and they all combine together to make this string which has an array length of 7 so the same way that we were able to index list value so we can actually do that here too if we drop down and say print string a and then the same way that we index before using square brackets we can do the same here so let's pull out the first letter of my name so we'll use a 0 now when we execute we should just be returned to D which we are we also have a few different functions that we can apply to strings I'll just show a few of these we can say print string a and then we can use the function upper to make my name entirely uppercase we see this in the terminal because every letter in the array of this string is now capitalized we also have access to the reverse function of lower we're also able to split strings in Python so let's say string B is equal to my name so Derrick Matt and then let's say Jessica and now if we wanted to return each name individually we could drop down and say print string B and then use split as a function and then we'll pass in the character that we want to split on so let's split on the comma value we'll put parentheses and then a comma save and then executors file now we see that we have three values of strings in a list we have Derrick Matt and Jessica so that's pretty much it we know how to create a string we know that a string is actually made up of an array of elements instead of just one element we know that we can calculate the link and how to index the string we now know that we have functions that we can apply to the strings and we're able to split strings as well if you have any questions about this please let me know I'll see you in the next video hey everyone let's get started talking about tuples over in a text editor let's go ahead and create a tuple I'll say tuple a is equal to parentheses cat dog and mouse remember that we specified tuple using a parentheses and that they're very similar to a list object the only difference is a tuple is immutable so this can be useful if you want to create a list but don't want people to be able to change it we can create a tuple instead which is immutable and doesn't allow for that change everywhere to try to do that here say to boy dot append how we did in our list and passing some type of value so value and save this an executed we should get an error in our terminal so two pools top pipe we see that tuple object has no attribute append that's because it's an immutable object and we can't change it so let's go ahead and get rid of this line of code or if you remember a tuple does keep the order therefore we are able to pull out values from where tuple using indexing we can print tuple a square brackets and then let's pull out the word dog which is in the first index position we'll put in a 1 and now when we execute we should be returned just all the tuples are one of the more uncommon data types that we have in Python however we still need to know how to use them we can sometimes find ourselves using tuples whenever we're working in a coordinate plane if we have a bunch of x and y values tuples are probably the way to go we use tuples from time to time they're really not that often I'll see you in the next video where we'll talk about sets hey everyone let's start talking about sets sets are just unique element containers we already saw this briefly but let's create another one so let's say set a is equal to in curly brackets and then we'll create a set let's say one two and then three we also know that sets are mutable we can see this by saying say and then we can add an element to set we'll put it in parentheses the string for terror set a will drop down and say print set a and see what the return is to our terminal Python 3 and then I accidentally typed over a previous script or tuple stop hi but we see that a set is an unordered because we have 1 4 2 3 in our return when we put in 1 2 3 & 4 so it's an unordered that means we cannot use an indexing on a set and it is mutable as we were able to add a new element to a previous set every wanted to add multiple items to your set we could drop down and say set a and use the function update now we'll put parentheses and we'll put in square brackets to create a list of items and then we'll say 5 6 and then 7 well print set a and execute it through a terminal and we see that we can add one element using add and we can add multiple elements using the update function if we want to get rid of elements from our set we can use the functions discard and remove remove just returns an error if the item is not in the set this card will just return none both of these will use the same way so say a remove and then we can put in any element that we want so let's remove the string 6 and now we should no longer have the string 6 in our output which we do not now if we were to try to remove the same element again and it's no longer in the set will see the error that we get returned so say a remove and then we'll remove the same element 6 we'll just try to do this without printing and then we'll say Python 32 pools and uh pod we see that we get the error tier 6 on remove 6 if we were to change the second remove function to the discard function we should no longer get the error when we execute we see that just returns a none and we no longer get in there lastly we're able to remove an element from a set using the pop-punk or drop down and say set a dot pop and now this function is kind of dangerous because it's going to remove the last item in the set however we know that asset is unordered so there's no way of knowing which item will be removed using the pop function and since I'm such a fan of danger we're just gonna do it anyways so print say a and I'll see what happens well execute and from the terminal we'll see that the string one has been removed remember that a set only contains unique values so if we have multiple elements in a set that are the same value this set will treat it like there's only one element of that value if frozen set is very similar to a set and we just used a frozen set parenthesis and then we can pass in set a to it will create a variable so F set a and what our frozen set does is changes our set from a mutable it's an immutable object it means that we can no longer do these functions of remove update add to our frozen set so you would use this whenever you wanted a set but didn't want any other values to change and that's pretty much it for this one what we need to remember is that a set contains unique unordered elements in a frozen set it's the immutable version of a set I'll see you in the next video hey everyone it's keep on going and start talking about dictionaries let's start off by creating a variable we'll say our dictionary and we'll set this equal to curly brackets how we did before and then remember that dictionary is full of key value pairs so the first item and the key value pair is the key we'll say key 1 how we did before and then value 1 we'll put a comma and then we'll actually hit enter here and do our first multi-line variable so then we'll say key 2 : value 2 we'll say print our dictionary and then cipher file we'll type in Python 3 and then the name of this file is dictionaries pod we see that we have a dictionary made up of two key value pairs key 1 is related to value 1 and key 2 is related to value 2 now remember that our keys can be some type of property so let's say how we did before name and then the value of name for me would be Derrick we could say height and then we can pass in 6 foot so 6 foot and now we can also keep on going can create as many key value pairs that we want so let's say another one and we'll say location and will give me a location of lost dictionaries are very powerful to us and we'll use them a lot the reason why is we can use these keys to pull out information from our dictionary put square brackets and then we can put name this should return to us in the terminal of my name which it does this makes it very easy for us to pull out properties values using a dictionary we're also able to change these values because the dictionary is mutable we can do the same thing so we'll actually just take this line of code here we'll copy it paste it here and then let's change the name so instead of Derrick let's say that my name is now Michael now we'll drop down and say print our dictionary we're readily able to change the values in our dictionaries using this method this is a very nice feature that you may use a lot so we know that we can change values but let's say that we wanted to add a new value to our dictionary we could do the same thing that we did before say our dictionary and try to index a value but if we change this value to something that's not already in the dictionary we create it in our dictionary so let's say I color is equal to blue we'll print our dictionary and since the key eye color is not already in our dictionary it should create this key in our dictionary will drop down and execute and now we see that our dictionary has name height location and also eye color if we wanted to get rid of this we could say our dictionary and then use the pop function that we learned in the sets lecture and pass in eye color it is caps sensitive so that's why you saw me backspace the capital e and replace it with a lowercase e you print our dictionary we should remove the eye color key that we just added in we see that we have like I said dictionaries are very powerful and they're a very good tool for us the takeaways from this video are that dictionaries are made up of key value pairs and that we can pull out values using the keys I'll see you in the next video hello everyone let's talk about if loops here in a text editor let's go ahead and create three variables we'll say X is equal to 1 Y is equal to 2 and Z is equal to 3 now to start an if loop we need to use the keyword it will say if and then we need to pass in some type of logical statement well this can be anything like greater than less than not equal to 4 now let's say X is less than Y and then to end an if statement we need to use a colon now we'll drop down and notice how we're indented over that's because this block of code follows this logical statement line so everything in this block will only be executed if this is true we'll say print X is less than Y when we open up a terminal we'll type in Python 3 and then the name of my file is f dot pi and we see that this statement is true because we get this block of code returned to us this is because x is equal to 1 and y is equal to 2 and 1 is less than 2 everywhere to change this to greater than and execute a file again we should get no return in the terminal which is exactly what happens in this situation we need to create an else statement if we go down and say else then this means this line of code will only be executed at this line returns false or drop down and see that's indented as well because it follows this else statement and we'll say print and then we'll pass in victory well execute and now we should see victory in the terminal because X is greater than Y returns false we already saw that we don't have to include the else statement but sometimes we want an action to happen whenever this statement is false we also have access to a few different operators those operators are and in or well look at how we can use the Ain function now let's go ahead and change this back to less then we'll use the keyword and Y is less than Z well print to the terminal and we'll say something simple like yes so the an keyword just means that both of these logical statements have to be true to get this action to happen we'll go ahead and delete this else statement and that when we execute X is less than Y and Y is also less than Z so we should be returning this action to our terminal which we are now everywhere to change this to Y is greater than Z and execute again we should seem now return in the terminal that's because like we said one of these statements is not true therefore the whole line returns false everywhere to change this to or however and execute it again well get the return because in this scenario only one of these has to be true to return the whole line is true if loops are very straightforward and they're very powerful to us all we need to remember is that we start initially with the keyword if we have some type of logic statement we in the line with a colon and then we have an action underneath we also have access to an else statement if we want something to happen if this line returns false we'll get plenty of practice with this loop so don't worry if you don't have it down to I'll see you in the next video [Music] hey everyone it's continuing on and start talking about while loops a while loop just execute a code while some condition is true we need to create that condition in the most common way that people do is they say I is equal to 1 to start our while loop or drop down and use the keyword wall and now we need to say some logical condition we already have I is equal to 1 so what we'll do is we'll say I is less than 10 and then we'll enter a while loop statement with a colon now we need to create some action here it's important that we don't create an infinite while loop so we need to somehow make this variable I greater than 10 to end or while loop so for each iteration through this while loop let's say I is equal to I plus 1 now we can do some type of action so let's say print I so looking at this while this statement returns true we want this action to be repeated over and over well save our file and now we're open up a terminal and say Python 30 for loops pi is the name of my file hit return and we see that we iterate through that action until I is greater than 10 because we print I each time and we see that we just iterate 2 3 4 5 6 and so on while loops are very straightforward if you understand the syntax of Python we start our while loop with a keyword while we have some type of logical operator and then we have the action of this line indented underneath we can also nest while loops the same way that we nested if loops we can drop down and actually use an if so if I is equal to let's say 6 let's let's say that we want to break and that just means that we want to stop any of the looping action will do this will save and execute and now instead of while being less than 10 we can say if I is equal to 6 we break the while loop which is exactly what we've done here while loops are just one more tool that we can use in our looping process if you have any questions please let me know in the next video we'll start talking about how we can use for loops and our prod on scripts I'll see you then let's talk about the last of our innovation techniques and talk about for loopy here in a text editor let's go ahead and create a string we'll set a variable of name equal to my name again so Derrick now to create a for loop we start with a keyword for and now what we need to specify is what we want each individual element inside the larger grouping to be called so let's say for element in name if you remember a string is not just one string by actually an array of string values so here we have one string value here one string value here and so forth here we have seven string values when we say for element in name we're saying each element so each string value each letter in the variable name we can print each element which is just what we're calling each letter in this name so print element we'll save and execute Python 3 and then this one is for loop ID and now we see that for each letter in my name we print to the terminal we can use for loops in a lot of different ways so let's say that we have a list a and this is composed of a grouping of names so Derrick Michael and then Jennifer will drop down and we'll say for name in list a colon will print name now since the elements are actually list items instead of the individual string characters we'll return each name to the iteration so we'll go down and print these since the values making up a list or actually the groupings of the strings rather than the individual letters making up the strings we returned each complete string through the iteration if we were to put in numbers into our list we could expect the same thing so we'll save and now when we iterate we returned all the string groupings and the numbers for loops are a great way to pull out values but they're also a great way to do operations so let's create a new list we'll say let's B and then this will be a list of numbers 1 2 3 4 5 for number and list B we'll say print number plus 5 this is saying that we want to add 5 to every number in les be well execute and we see that our list of 135 now becomes a list of 60 10 lastly a for-loop doesn't necessarily have to use this keyword here instead we could just say print 5 this will return 5 to us for each element in the list so since we have 5 elements in the list will be returned the number 5 to our terminal 5 times we can execute and we see that we have the number 5 5 times in the terminal 4 loops are one of the most common aeration practices that we'll use and we'll get plenty of practice in the upcoming videos if you have any questions please let me know I'll see you in the next video hey everyone welcome to another video so you made it through all the tough parts of Python you understand the basic building blocks so let's start thinking about how we can combine those building blocks together and create functions let's get started here in our text editor we can declare a function by using D EF now we use the name of the function that we want so let's say that we're creating a function that adds 2.6 to any value that we pass it will say add to 6 and then put parentheses after it now we need to pass in the parameters that we want this function to have since we just want to take in a number that means we can just assign any variable that we want here let's say X now in the function with a colon underneath it we need to make the action of the function so we want to add 2.6 to any number passed to error function will say result so we're creating a new variable is equal to X plus 2.6 now we want to print result so this seems like a very basic example but we can do a lot with functions here there's a few important things that we need to talk about a function always starts lowercase and it follows the same naming convention as a variable so it starts lowercase in each of the words are separated by an underscore and ideally a function should only do one thing so here we're only adding to point six which is fine but if you need to do several different operations you should create different functions for each of them will drop down now and we have this function created so now we can just create a variable well same amount is equal to your function add to 6 and then we'll pass in a parameter which will be treated as X so let's pass in an integer value of 7 well save our file I will drop down and type Python 3 and then the name of the file is function sup pod we can see that we returned 9.6 this may not seem like a lot but creating a function can actually be very useful to us here we just use one line of code when before we would need to we would need to add to point six and then print the result instead we created a function to do both of this for us the more complex your operation is the more useful it is to create a function so instead of just adding two point six let's say we needed to add two point six and then divide it by five point five and now we start to see just how powerful a Python function is we see that we can start creating Python functions that automate processes that we do repeatedly in the next video we'll talk about how we can take this even farther and start creating class objects I'll see you then hello everyone let's dive into the true object oriented programming nature of Python well look at how we can create our first class let's get started so this video might get pretty technical pretty quick but I'll try to keep it very simple here and then put all the technical stuff in a different video later on to create a class it's very similar to a function will call in class instead of the EF and for a class it's common to capitalize the first letter so let's say that our class is going to be report we'll put a colon and then we'll drop down now very simply put a class is just an object that we can use to create other objects made up of functions and attributes we'll create this class object here that we'll use to create other objects that we'll have to initialize using an instance I'll talk through all of that right now because I know it sounds complicated the first thing we need to do is to create the initializing function well say DF two underscores which just means that this is a private function initialize two underscores parentheses and now the first argument and your initialize function always has to be self now let's set a few tributes to our report class let's say that the report needs a title so title and it needs an author we'll put a colon and then for us to assign these we need to say self and then the attribute so that tribute here is title and author so self dot title is equal to title and then we'll do the same thing self dot author is equal to author so what we've done so far is that whenever someone uses this class object to create their own object when it initializes we're setting these attributes so now let's drop down and create a method which is just a function inside of a class we'll say DF and then write report parentheses and this will take in self and then it will also take in text we'll put a colon and drop down and now we want this to return to the terminal whenever someone uses it cell type in return parentheses then we'll put curly brackets that we'll use for the title we'll say bye and then more curly brackets that will use the format with the author we'll say a new line which we can do with the backslash and then an in will put curly brackets which will be the text here and then we'll use dot format which is just a function that we can use to format these blanks and then we'll pass in what we want to fill in those blanks in these parentheses so we'll say self dot title which is just this attribute right here self dot author and then we'll put in text which is just the function that we use right here we'll save this and drop down or decrease our indent all the way over that way we're out of the class and now let's use this class so let's say that I'm the one writing the report my report and then I'll initialize the class by typing the class name so report and now we need to pass in the tributes of a report here the title needs to be the first one so let's say the title is I should of studied art and then we'll pass in the second parameter which is the author which is me so we'll type in Derrick so what this line is doing is creating a new object from the class object of report by initializing a new instance now that we have this initialized we're able to use all the methods and all the tributes within the class so I will drop down and we'll say print and then it's initialized here my report so we'll say my report dot write report as a function which is just this method right here inside of a class object and then we need to pass in the parameters of that function which is just a text variable that we define here so let's say this is object-oriented we'll save the file we'll open up a command prompt or terminal and type in Python 3 in the name of my father's class type pod and we see that we've successfully used a method inside of a class object that we initialized here and then use the method of that class object here the more practice that we get with class objects the better we'll get I hope this was a good baseline on how we can start using class objects in our Python scripts I'll see you in the next video [Music]
Info
Channel: Derrick Sherrill
Views: 17,628
Rating: undefined out of 5
Keywords: python, python tutorial, python language, python full course, python course, learn python, learn python programming, python tutorial for beginners, python tutorial 2019, python programming tutorial, python programming language, software development, programming tutorial, Derrick Sherrill, python automation, python udemy course, python coursea course
Id: YJ8o-datYkI
Channel Id: undefined
Length: 50min 0sec (3000 seconds)
Published: Tue Jul 16 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.