Learn Python in 60 Minutes from Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone the purpose of this video is to teach you the syntax of Python based upon your current knowledge of Java in order to do this I have a clip set up here on the left to run Java I'll write all the Java code here we'll run it and then I'll write the equivalent code over here in Python for Python I'm just using the PyCharm IDE same setup I'll just run it then the console will be down here okay let's get into it so first before we start coding I want to talk about the main differences between Java and Python first if we're over here in Java and you create a statement into a you always end your statement with a semicolon in Python there's no need for semicolons next thing is that you have a lot of flexibility in Java in regards to where you put your statements you want to put a couple spaces put it over here put a couple more spaces put it over here this will still run and it will function the same as if you formatted it correctly that is not the same as in Python in Python you have to be very careful about how you indent and where you're placing your statements indentation matters and rather than using brackets with control statements like Java does Python depends upon indentation we'll see that more as we head into control statements but now let's actually start coding and the very first thing we're going to start with is printing so if you want to print to the console in Java all it is is system.out.print put whatever you like to print in quotations so hello we can run there's hello if you want to hellos we can say system.out.print again let's put it low again and run there's hello hello now what if we want the second hello to be on its own line well we can just make this one a print line that will cause a newline character to occur after this which forces this guy onto the next line so there's our two hellos second one on the second line over here in Python now it is literally just going to be print and then inside of quotations again just whatever you want to be printed so let's say hello go ahead and run there's our hello let's put a second one because a prints hello and print there's our hello hello notice that you didn't have to do a print line in Python it just automatically forces it onto the next line now you can control if it's going to force it on to the next line by saying end equals and then specifying what the end equals if you put n is equal to nothing and run it then it resembles just a regular print from Java you could also put a space here then I'll put a space between them and there you have your printing while we're here we should talk about escape sequences I'm just going to cover two quick ones let's do a backslash N and backslash T if you throw a backslash n anywhere within the quotation marks it's going to cause a newline to occur within Java so right here I put it after a chi that causes a new line and then llo goes onto its own line we can also do backslash T which will cause a tab there's a chi tab and then LLL is tabbed over we can do the same thing in Python let's put a backslash n go ahead and run that forces a chi newline let's change it to T go ahead and run again and again there's our tab so good to go those are nice in the scene and while we're here we should briefly talk about printing out multiple strings in the same print so let's say you've one print statement and you want to print out multiple strings inside of it or multiple literals inside of it so let's say hello and then let's put world in Java you can just use a plus sign then it'll print this one first that one second it will only print literally what's inside of the quotations so let's get rid of this space and then it should print hello world back to back yeah hello world no space let's do the same thing in Python we'll say hello plus world and run there we go hello world back to back no spaces now on to comments in Java you can create single line and multi-line comments for a single line comment all you need to do is forward slash forward slash that creates a single line let's get rid of that guy we'll create another line now if you want to create a multi-line comment or Lock comment in Java its forward slash asterisks an asterisk forward slash wherever you'd like to end let's go over to Python now if you want to create a single line in Python it's just pound sign if you want to create a multi-line comment it is three apostrophes so that we could say print something print something hello blow and those are all comment it out okay let's clear everything now and get into variables there are big differences between Java and Python regards the variables Java is very structured every time you create a variable in Java you have to specifically say what type the variable is going to be so main types that I use in Java are int that can hold positive and negative whole numbers and 0 I use double that hold floating-point numbers so 3.4 2.5 things of that nature boolean can hold true and false string and these can hold words or phrases now Python is very different in Python if you want a variable X we can just set X equal to whatever we want it to be if we want it to equal 4 that's fine it will equal 4 if we want it to equal 4.5 that's fine it's now equal to 4.5 even if we want to set it equal to words we can set it equal to words or phrases we can have space we can have symbols in fact in Python you can even use single quotes or double quotes to represent a string if you want to use double quotes that's fine as well recall that in Java when you use single quotes versus double quotes single quotes representing the character so if you say care C equals C that's a character rather than a string in Python characters and strings they're all well strings are all represented by suppose single and double quotes and now that we have multiple variable types let's talk about printing different variable types so let's go over here and create an int X let's set it equal to 4 and we'll say your system out print X is equal to and then whatever that value of x really is so now let's run we'll say X is equal to 4 good because we set it to 4 here we'll do the same thing over here in Python let's say X is equal to 4 we'll go to print X is equal to but now we can't use plus as our concatenation operator over here we have to use comma and then X so now let's go ahead and run it in Python there's our X is equal to 4 but wait if you notice this it looks like there are two spaces here and there actually are if you get rid of this space then it looks correct so x space equals space for the reason for this is because just like python automatically puts a new line after every print it also puts a space every time you put a comma in here to add in different variables or different string literals every time you do that it automatically puts a space in there so you don't need to actually put one in quotes now you can control this just like you can control the end of the print you can a set is equal to and then if you don't want it to be separated by a space you can say well separation is equal to nothing and then our normal space in here will actually be read and then that look correct you could put multiple spaces in here if you want there's three spaces and it moves the forever okay but let's actually get rid of these extra spaces we'll put it back to nothing and now let's work with some mathematical operators over here in Java I'd like to say let's say just four plus four and in Python we'll do the same thing let's run both of these this of course will give us X is equal to eight and Python and Java addition subtraction and multiplication are all going to be the same but now let's talk about division if you go to Java and you do three divided by four this is going to give you zero because it does integer division whatever the decimal portion that is left it just automatically gets rid of Cassatt to an integer there is no rounding it just drops whatever the decimal is this would have been zero point seven five it drops the whole decimal gets a zero over here let's do 3/4 Python and run this and notice that it actually gives us zero point seven five this does division as if your calculator with do division but there is a way to get integer division or in Python floor division and it's by using two division symbols and now we finally have our X is equal to zero let's go back up Java and let's say that we want to do an exponent well we have to use this cumbersome static method from the math class and you can say math that POW if you want to to the third you can do this guy here let's just make this a double go ahead and run and it will give us our 8.0 over in Python now let's do to the third and you can actually just use two asterisks and it will automatically do two to the third so there's two to the third that gives us eight and the next operator we can talk about while we're here is modulus if we did four for mod two this should give us zero yep and it's just going to be the exact same thing over you're in Python for mod two should give us zero as well very good now next thing that we're going to go right into is casting let's say that we have an integer X and we have three point five and we want to put that into our integer X well it's not going to let us do it because this is declared as an integer so you need to first cast this guide to an integer so to cast you just put whatever you'd like to cast a two in parentheses beside it'll cast it to that so now when we run you'll cast 3.5 to an integer just drops the decimal we'll cast it right to three we do the same thing over here let's say X is equal three point five then let's say X is equal to int three point five and now we can run and there's our X is equal to three notice that the casting for the parenthesis is just backwards Java you surround the type in Python you surround the number that you want to cast Python has a couple more handy shortcuts that Java does not let's say we're in Java and we want to find the maximum or the minimum of two numbers so you can say max three out of four well the maximum number is definitely going to be four we can run it there it says X is equal to four in Python now we can just say max three comma four no reference to the math class it's just a built-in function there X is equal to four we can do the same thing with min for both of these men is also built into Python and then let's just run both min is three man is three now creating programs in the console isn't very much fun unless we're getting input from the user so let's do that in Java first I'm going to do it in Java by creating an instance of the scanner class so I'm going to say scanner I'll call it reader equals new scanner then as a parameter takes in a system that in and once we import then we can use this instance to read in from the user so we can say int x equals reader next int and then we can just system out prints say you entered Man X and if we run it will wait Frost and or something let's say for you entered for congratulations good now we can even add a prop to this let's say system.out.print enter a number : space run this again we can say enter number let's enter 4 you entered 4 there's the Java portion now Python as you probably already imagine is much simpler we can save print enter a number colon space then all we're going to do is say x equals input then we can just print you entered comma x let's run this enter a number we can say for you entered four very good we can do a couple bottom modification since it is python can say n is equal to nothing good I didn't put a space here so there will be naturally naturally be a space there one more time enter a number for you entered for very good but we can you make this a bit simpler by saying enter a number let's grab that and we'll just put it as a parameter in for input get rid of this whole guy and now we can run again say enter a number automatically stops after number and her for says you entered for and it's the same thing we just did over here in Java the last thing I'll say about user input in Python is be aware of you may have to cast once your input comes in from the user if you want this specifically to be a float here then make sure you just go ahead and cast it to a float as soon as you gather the input that will prevent problems later on down the road okay now enough for user input let's head into strings and over here in Java now I'm going to create a string s just equal to input one of the first things you should be able to do with strings is just to get the length of the string so let's do that in Java will say system.out.print line s and all it is in Java is just length that's a method of the string class will just print how many characters are in input there are five now let's do the same thing in Python let's say s is equal to input in Python there's a built-in function called le n for length and then we can just put in for it as a parameter whatever we want the length of we want the length of the S which is a string so it should give us how many characters there there are five all right let's clean up our print out a little bit before we go into the next part let's say length is equal to concatenate those let's go into Python do the same thing and run both just to make sure there are no errors okay length is equal to five good now next thing that we're going to cover is sub strings in Java if you want to substring there's just a substring method so as our string we can just hit the method selector and say substring the first one that I'm going to cover is the one where it takes in one parameter so if you put in two it's going to go to whatever character is at index two and then it takes that to the end so I is zero and is one p is two so it should give us puts when we print this say substring one and there's our put now for the equivalent in Python Python created beautiful syntax for accessing substrings and here's how it works we're going to say a substring one just like we did last time we're going to say s square brackets and then a colon on the inside and then here on the left side you can designate the start index and on the right to stop index but now if we just put two on the left with no stop index that means it goes from two to the end so again i zero and is one p is two it should take two to the end and say substring one is put and there we have it and now let's do a similar example with both a start index and a stop index so let's just copy and paste this will do substring example two and now we're going to say 1 comma 3 so we're going to start at index 1 and at index 3 but it does not include index 3 so the only two letters we should actually get are index 1 which is N and index 2 which is P so it should say substring 2 and P very good let's go over here we'll copy and paste substring too and we'll do the same thing let's just go 1 2 3 here run and there we have our NP alright we'll do one last string example here this is where Python will really prevail let's have a situation where you know that you want the last three letters of a string so let's say system.out.print line and we need to come up with a way in Java just to get the last three letters so for substring 3 example all it's going to be is substring then inside of here we're going to use the single index method but we're going to say s dot length so we know that the length is 5 then you have to do minus 3 to get the last three letters so now when we print this it should give us put like our substring one example did because the length is 5 minus 3 will put us at index 2 to the end and now the really nice thing about Python is that rather than doing length minus 3 over here let's say substring three we can simply do s and then negative three : to the end so the negative three says go from the end the last three characters and then print it out and there's our puts negative indices are very nice if you want the last character you can just put negative one if you want the last two you can put negative two you could also put negative three but let's stop at negative one so this should give us PU there it is Python is very flexible with grabbing sub strings from an original string and now we should talk about using the contains method in Java so let's say system.out.print line will do a contains example and this is just used to determine if there is a substring or an individual character within an original string so we can say s method selector contains and now within quotes here we can put whatever we're going to search for so let's search for P if we search for P then it should give us a true or false as to whether or not P resides within input yes it does so there's our true we could put P you if you usually exist in there as well we could run it there's our still our true so let's just put something that's not in there so you can see a false there's our false PUA does not exist now over in Python Python has an N operator that does the same thing so let's say contains and now we can say p in s and it's just going to check if the character p resides within s and it does there we have our contains we can do sub strings with this as well so let's say PU does PU exist within s yes it does so there's our true again let's do one that doesn't exist run there's our false PUA does not exist within s alright and we can keep going on with this for a while but we're going to stop it here the last thing I'll say is that in both Java and place strings are immutable they cannot be changed so every one of these methods have used in Java this provided us with a new string object and every time we did that in Python it would provide us with a new string object as well in a couple minutes we'll study the delete function that's another built-in function you cannot use that on strings and strings are immutable in Python now moving on to our next data type the next data type that we'll talk about in Python is going to be the list and this resembles the ArrayList with using the list interface from Java so let's create that in Java first we can say list list equals new ArrayList and in job it'll have to import both of these which I already have up here now in Python if you want to create a list all you need to do is say two square brackets that creates an empty list and now we can add things to the list we can remove things from the list just like you can with the list interface from Java now before we modify elements I'll say this about lists in Java it's typical to add a type parameter here so you could say list integer list equals new ArrayList integer so now when you start adding elements to this list the only thing that you can add are integers here since we can add any object we want over here to our list in Python I'm just going to take away those type parameters in Java so we can do the same thing over here now let's add a couple things to our list in Java we can say list dot add all this is a method just add things to list between add house let's add some more things add Mouse and blouse and now if we want to print out our list it's easy to do all we need to do is system out print list let's run and there's our nice printout of our list these also have indices so house is 0 Mouse is 1 blouses 2 we can get the size of our list just with the list size and we can run this the size is 3 it contains three elements and we can also get specific elements of our list we say system out print line list get and you can just specify whatever index that you would like to get so gets let's get index 1 and there's mouse because it has an index of 1 now we'll do the equivalent and python over here and to add thing to lists in python all you need to use is the append methods we can say list stop append let's say house and let's add in the other two from our java list and now we can print our list simply by saying print list and there's our list now let's print the size or length of our list we can just say length and we're going to use that built-in length function again that we use from strings so we'll just say Ln of list length of the list and there we have it's length of 3 really same thing as size from Java but there are three elements in it so it has a length of three we can also print out a specific index let's say index 1 and at index 1 we're going to say list use that square bracket notation again the same thing that we did from strings and now we can run there's the element that resides at index 1 which is Mouse and now let's transition back to Java what happens if you want to add an element at a specific index where you can do that with the add method but there's an ADD method that takes into parameters so let's say list dot add I'm going to say at index 1 let's add browse and then we'll just print out our list again so now it's house Krauss mouse plows now also notice that grouse pushed Mouse and blouse over and they each index essentially added one so now this is 0 grouse is one Mouse is two blouses three for the equivalent Python we have in insert methods we can say list insert add index 1 and then we'll insert grouse and let's go ahead and print our list again and now we have the same thing as Java very good and now let's say after we added grass well now we want to remove it there is a remove method and we'll just specify an index we want to remove the element at index 1 and we'll print out our list again in Java a system.out.print line list and now grass is gone we're back to the original list that we started with and both of these have the original indices that they started with over here now we can do we can use the built-in delete function and inside of here we'll just tell it what to delete well of list we want to delete index 1 so go into list delete index 1 and then we'll print out our list again so prints list and there we have it found Grouse at index one and deleted it and now we're back to our original list this delete notation is very handy you can specify a range in here as well let's say you want to delete from index one to index two should give us the same printout just deletes index one because two isn't included if you want to include it you could do one two three that should delete both grouse and mouse they're both of those are deleted and now we're just down to how some blouse and while I didn't specifically say this you can use this notation for accessing elements as well if up here we wanted to access more than just index one if we wanted access indices 1 & 2 we could say well get 1 2 3 not including the 3 so this should change our line here where it says index 1 now we have mouse in blouse instead of just mouse let's put that back to the way it was so it makes sense and now I just want to do some operations with lists only in Python now I'm going to delete all this we'll start with our original list again and then I'm actually going to create a second list just call it list 2 and we'll make it contain the same elements now I want to show you the extend method so now I'm going to take list 1 I'm going to extend it by adding the entirety of list 2 and now I want to go back and print the original list so all this does is it grabs all the elements from list 2 and then append them onto the end of list 1 we could also create a brand new list if we wanted to we could say list 3 is equal to list + + 2 and let's print list three there's list three same print out and now rather than using the elements of list and list two to create one massive list three we could just keep these as lists and add them as individual elements to list three and here's how we're going to do it let's say list three is equal to an empty list and now we'll say list three dot pens first list now we'll append list 2 and now let's print out list three and you'll see that the printout is going to be a bit different so now we can see the individual lists within the master list list three so we have list 0 and then list with index 1 so now if you want a specific element then you can just say print list 3 specify which lists within that list you want well let's get the second list so that has an index of 1 the one i've highlighted here and then within that list you can specify which element that you want so now let's say we want the one at index two so this should grab this list index 2 which should be blouse so let's print this and there's our blasts now the final thing I'll say about lists is that since they are immutable it's very easy to change their individual elements so let's say we want to modify index 0 and let's set it equal to the number 4 well this will just replace whatever is currently I index 0 with the number 4 and now let's print out our list again so print this 3 now you can see that what was that index 0 which was an entire list now that's gone is just replaced with the number 4 so that covers the list data type from Python and just like strings we could keep going on for a while with several different methods or we're going to stop it there and now we're going to transition into immutable tuples and I'm going to do this one solely in Python over here because the closest thing to a tuple in Java would be an immutable list or a list that cannot be modified so in Python plus create X X will be our tuple and let's create 2 3 4 5 so now X is a tuple made up of the elements 2 3 4 5 now it's very similar to a list except as I said before tuples are not mutable they're considered to be immutable we cannot change the elements of this tuple now we cannot say well let's set index 0 to 7 we can't do this if we would run this we're going to get an error there we have it but now you can print them out just like lists after we have our tuple created if you want to put it out there you have it notice that it's parenthesis now instead of square brackets to represent the tuple and just like lists tuples can contain objects if you want them to contain objects let's try some strings in here and we can run this there's our to pole if you want to create one tuple that contains another tuple you can do that as well let's create a tuple Y and the first element will be X after that let's throw it in and you're in 5 and then a string yes now let's print out y there's our new tuple first contains a tuple which is half smooshed goose then it's 5 and then the next element is yes but you can print out individual elements let's print out elements 0 this should print out the entire X tuple there's the entire X tuple let's print out index 1 there's the 5 and we can use the same notation that we used with strings and lists this is actually called slice notation we can say Y of 1 2 3 this should give us indices 1 and 2 and we can print that out there's our five and our yes so this slice notation is very useful if you will ever want to just print out the entire thing you can just do a single colon here or you can just do Y itself but you can always put one index at the beginning and that will always take you to from that index to the end so there's yes it is possible to create a tuple with one elements but you might be wondering how to do this because let's say if you want to create a tuple that contains the number five well that just creates an integer that contains the number five in order to do this you can just add a comma afterwards so now we can print out X and it should print as a tuple rather than as an integer and there we go now we see that it is indeed a tuple and now just as you can use the built-in length function for Strings and lists you can also use it for tuples let's make this tuple a bit bigger now let's use the built-in length function we can say Ln of X go ahead and print and we see that our like this five very good you can also use the in function let's say print let's say three in X and this will give us true very good let's make one that's false if we put seven is seven in X well no it's not seven is not within that tuple now so that's going to do it for data types in Python there are also sets and maps that exist in Python I'm not going to cover those but for now I just want to summarize what we did cover so you can create X as an integer or a float or as a string strings can use double or single quotes you can create a tuple which is just separated by commas you can create a list which uses square brackets you don't have to append every element you could also do it just like this just make sure that you have the square brackets on there when you create strings strings can use many of the built-in functions that lists and tuples can use as well so if we create a house you can use the in function on any of the three above to determine if an element resides within a tuple list or string you can check it a string resides within a tuple list or string you can also use the built-in like function on the three above then finally recall that tuples are immutable they cannot be changed lists are mutable and they can be changed strings are immutable and cannot be changed so this is the only one that will allow to use that built-in delete function one and all three of these also support the slice notation that we talked about with lists and strings so if you wanted to you could say X square bracket colon put your start in your stop index and it would specifically return whatever range you specify in here and that does it for data types now we're going to move into comparisons for equality in Java you have two options you can use equals equals operator which can compare two primitive types or two objects or you can use the equals method which can be called off of one object and then use to compare to another object so now we need to determine what the difference is in order to do this I'm going to create a quick program here we're going to use a scanner class we're going to take in an input from the user and then we'll compare it to some string variable that already exists we'll call this star two now I'm just going to print out each of these comparisons starting with equals equals so I'll say input equals equals star two and now let's run it we'll wait for you to enter an input so we'll say house now it returns false now notice even though I entered house which is the exact same as our stir two object it still returns false and the reason for this is because it's checking these two string objects to see if they are the exact same instance and they are not these are two different instances of the string class so that means it's going to return false now we could check our other check for equality we're just just going to be input dot equals stur - and now let's run again want our house again now notice that this one returns true and the reason for that is because the equals method actually compares the characters rather than if they are the exact same instances since house is equal to house exactly then it's going to return true equals is typically modified in the class template and then you can dictate exactly how two objects are going to be compared so now what is the equivalent in Python well python has two equality operators as well first one is equals equals just like Java and the second one is the is operator so let's create an X is equal to four and a Y is equal to five you can check if these are equal simply by saying prints x equals equals y and then that will check for equality but now it's almost backwards from Java this one checks if two items are simply equal as far as their values go this one is checks to see if they are the exact same object or the exact same instance so now let's quickly create a similar program to the one we did over there all I'm going to say it X is equal to input Y is equal to house now we can print X is equal to Y we can print X is y so we'll run it'll be waiting for input let's put in house there's our true because the values of house are the same Hou SC they're the same on both and then there's our false because they are not the exact same objects and now let's keep going with operators in Java there are four operators that I'm sure you're very familiar with they're less than greater than less than or equal to greater than or equal to and they can be forms like this less than greater than less than or equal to greater than or equal to Python has the exact same operator so if you wanted to you could say 4 is less than 5 we could print that out and it's true and we could do the same thing with less than or equal to greater than and greater than or equal to so and in Java there's also the not equal to operator which is the same in Python you can check if the values of two variables are not equal to each other so we can say for is not equal to five well that's true in Java you can use logical operators now to negate or combine different relational expressions and you can do that in Python first an example in Java you can say let's say 4 is less than 5 but then you want to negate that well you can negate that whole expression by using the exclamation point so that says not 4 is less than 5 then we can print that out 4 is less than 5 is true so negate it to false very good in Python is going to be a bit different and you're going to say not 4 is less than 5 and it's literally the word not so there's our false in Python and in Java there are two more logical operators so as far as logical operators goes they are not in and for and we can use these in expressions like this where we can say 4 is less than 5 and we also want 6 to be less than 7 so both of these have to be true in order for the entire thing to return true so we can run this this of course will return true you can do the same thing in Python here's your operator summary in Python not instead of being the exclamation is obviously not then and and or also just written out so if we wanted to do this similar expression in Python it would just be for less than 5 and 6 is less than 7 and there's our true we can also demonstrate or quickly just by changing this let's make this one false so six is greater than seven well that's not true but it's okay because four is still less than five so this guy should still return true and there we go now we're finally going to enter into control statements we'll start with the if statement in Java you can just say if some relational expression here 4 is less than 5 then we cancel out that print line let's just print one and we can run since 4 is less than 5 it will go into the if in python the notation is going to be much different since python depends upon tabbing and whitespace we're going to say if 4 is less than 5 no parenthesis and now colon to say that we're entering into the if and now everything that's going to be inside of the if is going to be in tabbed over 1 then once we want to go back out of the if then we'll just go back over to the far left so now we can say prints 1 and 4 is less than 5 so we print 1 in Java you can do an else so this covers everything else in case 4 is less than 5 is not true we'll print a 2 here and let's just make this false so we can see it go into the else we'll run there's our 2 python has similar else statement just else : print 2 and we'll change this as well run and there's our 2 now Java has the ability to chain if statements you can do so like this else if let's say forward is greater than three that will system.out.print three so now only one of these will ever be executed it's different than just simply writing three different if statements only one of these will ever actually print so we'll print one two or three whichever one is true first so this one is the first one that's going to evaluate to true so we'll print this guy there's our three in Python it's going to be a bit different it's going to be Elif and then put your condition so 4 is greater than 3 : print 3 and there's our 3 and now let's talk about loops first loop structure in Java and perhaps the easiest is the while loop will create a counter first we'll say int X is equal to 0 we'll say while X is less than 5 and then in here we can just system.out.print x + space and then we'll hit an x + + just to update X every time so this should print 0 through 4 there is our 0 through 4 we can do a similar thing in Python X is equal to 0 while X is less than 5 again no parentheses here then colon then same rules everything that's going to be tabbed over will be inside the while loop then we can just print X and I'm going to change the end to just be a space then update our X rather than doing the plus plus here we'll just have to do plus equal 1 go ahead and run 0 through 4 same as and really if there's one thing that Python lacks is the increment and decrement operators you can't just say X plus plus or X minus minus like you can C++ and Java every time you have to use the plus equal one and now let's go to for loops in Java the typical for loop looks like this and died is equal to 0 is less than 5 I plus plus essentially does the same thing as a while loop we just put everything at the top here and let's system.out.print I plus a space and there's our 0 through 4 in Python now it's going to be a bit different for Python there is no standard notation like this we're going to say for X in and then we're going to use range and in range we're going to put the upper most limit so limit is 5 and now we can just print out X and is equal to a space and there's our 0 through 4 so here is how range works range automatically takes from 0 up to but not including the limit that you specify and then it increments it by 1 so saying for X in range 5 gives us 0 through 4 we could do X in range 10 and then it would give us 0 through 9 you can also tell the range function where to start if you want to start it too and go from 2 to 10 incrementing by 1 then you can just put 2 comma 10 there's 2 through 10 but not including 10 so it stops at 9 you can also tell it how to increase so let's say 2 to 10 increasing by 3 so there's 2 5 8 tens the upper most limit so it won't go up to 11 and the next repetition structure is going to be the for each loop in Java for this I'm just going to create a quick array and the for each loop in Java looks like this you can say for each integer I and airy then just print each elements which is going to represented by I and then with a space right after so this should print one through four there's our one through four just each element in array Airy in Python now I'll create a list instead of an array I'll say list is equal to one two three four and now we can say for X in list print X again will control the end make it a space and there's our one two three four and just to make sure it's printing out our actual list we can update it there's the one at the end one two three four one now Java has break and continue statements and so does Python we can say if X is equal to three then let's break and there's our one two it hits three that it breaks out of the loop and then that's the end we can also do a continue steven so if you hit three then just continue to the next iteration of the loop so it should really print out this whole list except for the number three there we have it one two skips three four one and now python has another feature that java does not and that is the ability to combine an else statement with the repetition statement and here's the example we're going to use to look at this we're going to say for X and range 5 if X is equal to 6 break now I'm going to put an else but it's not going to coincide with the if it's going to be off of the for statement so I'll say else and then we'll print entered else so now how does this work well we say Forex and range five so it's going to go zero one two three four if X is equal to six which it will never be equal to six that we're going to break out of the loop now the else statement is executed if this makes it naturally through each iteration and then it ends the for loop naturally without hitting a break statement then the else statement will be executed if a break statement is hit then the else is skipped and it will just go down through the rest of the code but let's go ahead and run this there it enter the else now we can prove what I just said by let's say if X is equal to three then we'll hit the break statement and it will never print entered else so nothing should be printed and there you have it and that's good now for repetition statements so let's clear our slide again and now we're going to get into functions to demonstrate this and we're going to create a private helper method over here and then we'll just create the same function over here so in job I'm going to say private static just since we're using this with the main then this is going to return an integer and we're just going to make a sum method that takes in three integers and returns the sum so int a int B in C then all this is going to do is return a plus B plus C that's it now in Java you can then call that just by saying system.out.print line call some 1 2 3 some should be 6 let's go ahead and run there's our Sun and now over in Python let's create the same and here we're going to create a function which you can create by saying def and then just the name so now call it sum and now we don't need to specify what type is going to be coming in as a parameter all we need to simply say is how many are coming in well there's going to be an a a B and a C then : now everything is indented over we code for this function so we can say return a plus B plus C notice that we also didn't need a return type on this it simply recognizes the type that you're going to return in the return statement and now down here we can just say print sum 1 2 3 and this should be 6 as well there's our six and now Python being the nifty language that it is at it in a few extras we can create a mystery variable and then just set it equal to the whole sum function so let's delete that and now we should be able to use mystery in the exact same way that we use some so let's print this 3a 1 2 3 and let's run there's our six and it does the exact same thing as still calling sum 1 2 3 so now we should get 6 6 there it is now one of the other very cool things about python is that it supports optional parameters in Java if you wanted to create a sum method that could take in 3 or 2 parameters you could just overload the method by creating a whole new method like this static in some you could say in a and B return a plus B now when you make the call in here depending on pon how many parameters you put in it will either call this method which takes in 3 if there are 3 parameters or this method which takes in 2 if there are 2 parameters but in Python you can actually just create one function that has an optional and optional parameter so let's go ahead and do that and the way to do that is just to say well the third one is going to equal here and you can actually do this with all three of these if you wanted to so we're going to default C to 0 so if the user only enters two parameters for a and B then it's going to default the third one to 0 and then still return a plus B plus C so let's try it and we'll get rid of this guy and we'll print sum 3 4 let's go ahead and run there's our seven and just to show you we could also default this to a different number it doesn't have to just be 0 we could default it to 10 so now we 3 plus 4 plus 10 which should give us 17 and there's our 17 and now let's clear everything and we'll go into our last topic which is going to be class structure and creating objects and now the first thing we're going to do is I'm going to change this and we're just going to make a dog object so that will be the name of my class now in Java we'll create a simple class it will have a name for the dog and an age for the dog so let's say private string name privates in stage let's create a constructor we'll say public dog will take in a name represent it as nm and an age represented as an integer a then we'll just assign them right to our instance variables so say name is equal to nm age is equal to a and now we can create mutaters and accessors for these two instance variables we can say public int get age return age public string get name return name public void set age into a then just set the age to a and public void set name begin a string and n and set the name to n and the last thing in I will do in here is just create a two string method public string to string so that when we print an instance of the dog class it will give us a nice printouts so we can say return dog : let's go to a new line name plus name + H + H I'll put a new line here as well and now we have enough to go ahead and instantiate and create our first dog instance so let's say dog d1 equals new dog you can say Carley and age is 10 now if we wanted to print this out we could just print out d1 and it should automatically grab this two string here and give us a nice printout of what our dog is so dog named Carly aged 10 very good now let's create that same class template over here in Python and now we'll do the same thing it'll just be quicker in Python we're going to create class dog : one of the first things we can notice is that in Java when you create a class it must be the same as the class name or the file name but in python when we create a class it does not have to be the same as a module name this module name is demo but our class name is dog this module can contain many different classes and they do not have to be the same as the module name so now let's create our constructor we can say def and then to create a constructor you can just say in it then put self and then after self will place all the parameters that are coming in so we're going to say name and then age : now we can decide what to do with these unlike Java and Python you don't have to explicitly say there's going to be this instance variable there's going to be this instance variable we can just create them on the fly and I'm about to do that here we can say self then I'm going to create a name is equal to a name then I'll create self age is equal to age I'm placing an underscore in front because that lets other users know this is considered a private variable don't change this variable from the outside without going through one of my methods and that'll do it for a constructor now we can go ahead and come down and create our accessors and our mutaters let's do our accessors first so we can say def get age self no parameters on that and we're just going to return age def get name return name and will these cell phone this one as well now let's create our mutaters we'll say def set name we'll need a parameter for the sky so we'll take in a name and then just set our instance variable to that and now we'll do the same thing for age and now the last thing we need is that to string we created in our Java class over here so we said public string to string and that is grabbed every time you just print out an instance of the dog class it's going to be a bit different in Python and we're going to say def underscore underscore STR to designate the to string in Python then in here we're just going to return this line I have here say dog name plus self underscore name to grab that instance variable and then the same thing with H here but since age is considered an integer we're just going to cast that guy to a string so now let's go ahead and go below this class template and let's just create our own instance of a dog and we'll say D 1 is equal to dog we can put a name in here scruffy H 5 and now let's go ahead and print D 1 and this should print similar to our Java right there is dog name is scruffy ages 5 big thing to notice here is that you didn't have to actually use a new operator or anything like that in Python and Java you have to say new dog in order to instantiate here we can just call the name of the class and then any parameters that we need assign it to a variable then we can go ahead and print D 1 the other thing that you should notice is this use of self when you use self it dedicates it as an instance variable or an instance method if you omit self then it's going to be considered a class method just like in Java it's automatically in an instance method unless you put the word static on it then it's considered a class method but first let's just use a couple of the actual methods in here so we can say prints d1 and we can just use its age and let's run that there are the ages 5 since we created him with an age of 5 we could also say get name as well you can also see the naming differences between Java and Python it's standard in Python to create names of methods and functions with an underscore between lowercase names rather in Java they just use camel case with each word after the first word having a capital first letter and now let's demonstrate something that doesn't use self so let's create a function in here and let's say random no self on this guy and we'll just return 7 now in order to access this we can just say print reference the dog class and then call that method so we should be able to call random now takes in no parameters go ahead and run and it should just print 7 and there we have it doesn't depend upon the instantiation of the dog at all just every time you can reference the dog class and then use that function all right that's as far as I'm going to go I hope this helped you out if you're coming from a C++ or Java background I wish someone would have pushed a video like this when I was learning Python it's much easier to go from one language that you already know then to a new one when they're being compared like this thanks for watching leave any comments below
Info
Channel: Krohn - Education
Views: 84,451
Rating: undefined out of 5
Keywords: Python (Programming Language), Java (Programming Language), Programming Language (Software Genre)
Id: xLovcfIugy8
Channel Id: undefined
Length: 60min 9sec (3609 seconds)
Published: Tue Dec 08 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.