Transcript for:
Mastering Advanced Python Features

python is a fascinating language that contains a ton of hidden yet extremely useful and Powerful features in this video I'll share with you five of them that you need to understand if you want to fully grasp the language and come across as a professional now another way to come across as a professional is to join something like course careers software development program not only do we teach you all of the essential skills but we run you through in-depth career preparation more on that later now let's dive into the features the first feature to look at here is the anonymous variable now this is simply the underscore and this is used as a placeholder when you need to declare a variable but you don't care about the value of that variable so to give you a quick example let's say we want to do something 10 times what we could probably do is just create a for Loop and say 4 I in range and then we can type this correctly and put 10 and we can do you know print and then do this okay so this is valid this will run and we'll see do this 10 times but notice here that we're not actually using the value Val of the variable I it's not a huge problem this code is still going to run what we can do is replace this with an anonymous variable which is simply the underscore now this code will work perfectly fine now why would we do this well in some programs you actually have a linter or in some idees and you'll get kind of squiggly lines under your unused variables telling you to use them some code bases won't actually allow you to even push code if you have any linting errors and a lot of times it's just a little bit easier when you're not using a variable to Simply use the underscore inp place of it so that you don't get a syntax error and you don't have any confusion about what this variable is doing now as well as using this in a for Loop we can use it in a lot of other situations so here you see I have a coordinate now what I might want to actually do is unpack this coordinate and just get maybe the x or y value so traditionally I could do something like X comma Y is equal to my coordinate X will now be equal to 5 and Y will be equal to 10 however if I'm not going to use either of these values what I can do is replace them with an underscore now I'm still able to unpack this because I have something where the Y value would go but I don't need to declare a variable that I'm not going to end up using now same thing works if we're trying to unpack say the larger coordinate so if we copy this and paste this here we could do something like X comma underscore comma Z and now we're able to get the X and Zed values which we might need to use but we don't need to have an unused variable for the value y now similarly if we're doing something like a list comprehension so let's do something like second elements let's let's say I just want to get the second value out of all of these different pairs in this list well I can do this in a few different ways but if I do a list comprehension I could say second element equals B for underscore comma B in and then list of pairs and now if we go ahead and print out the second elements and we run our code you see that we get b d and f and we didn't have to declare a variable a that we don't end up using now believe it or not this next feature I didn't learn about until I was writing python code for over 3 years now this is the lse statement associated with a four or a w Loop now a lot of times when we're iterating over some type of structure what we're trying to do is find if some criteria is true or if it's False A lot of times we might be looking for an item or trying to see if the list satisfies some criteria now if that's the case we often want to do something with the list afterwards if it did or didn't satisfy that criteria and that means we need to actually have some kind of flag that tells us how we exited the loop what I mean by this is we could exit early because the criteria was true or we could exit after we iterated through all of the different items so let me give you a quick example to show you what I mean in this instance we have a wall Loop we iterate over this list and we try to see if the item B exists inside of the list now if we didn't have this flag that I'm setting here found equal to True when we reach the end of this wall Loop here we won't know why we exited we could have exited because we broke out of the loop by finding the item or because I became equal to the length of the items so we need to include this flag here and then we use the flag to do some type of operation however we actually don't need to include the flag because of this while else and for else syntax so let's remove this flag and let me show you how we could write this alternatively so rather than having if not found we can simply just put the else statement here now this is completely valid syntax and what this will do is print out do something here if we don't exit the loop from a break statement so let me just make this really clear this else statement will run if we don't break out of the loop now this works for both four or while loops and we can look at a quick example here so let's just do a print and just say found it and let's run the code and you'll see that we get found it now if I change this is something that's not in the list so say the value Zed and we rerun the code you see that we get do something here so the L statement will be triggered if you don't break out of the loop that it's associated with now we can easily switch this to a for loop as well so we can just say four and this will be item in items we can remove this and we'll see the exact same thing works if I run my code we get do something here so really some useful syntax whenever you're trying to determine whether you broke out because you found some criteria and Ed the break keyword or if you reach the very end of the list then what you can do is use this else syntax now before we get into this third feature I do want to let you know that I recently teamed up with some of the top software development instructors online for example one of them is webdev simplified we've created literally the best course online to help you land a software development job not only do we teach you all of the skills you need and run you through in-depth specializations in your desired topics so front end backend or devops we have a huge focus on your career so setting you up with the best templates for your resume interview prep how you answer specific questions what jobs to apply for how to optimize your LinkedIn we have literally everything in the course where the sole purpose is to help you land a job as quickly as possible if you're interested you can check it out from the link in the description we've got a free introduction course and obviously a no obligation 14-day money back guarantee so if you're not interested you can of course just refund the course no questions asked no risk we've already had a ton of success and I can't wait to see you guys there so the next feature to show you is something known as the wallrus operator now this was released in Python version 3.8 so make sure you're at minimum using that version now the wall operator looks like this it's a colon and an equal sign and it allows you to actually Define a value while using it as part of a condition really cool and I'm going to show you how we can use it to write more readable and clean code so I actually have a fairly complex example in front of me that's using something known as a generator now you don't need to worry too much about how the generator works but the idea here is that you see that this is looking a little bit messy what I'm doing is I'm defining a generator object and I'm saying data is equal to the next value that my generator is going to give me so it's going to yield me values and just keep giving me values until it runs out now what I say is while the data is not equal to1 which is the last value yielded I'm going to process this data and then I'm going to get the next value so this is something you might have seen before where you're continually getting some value processing it and then getting the next value but you're actually using the result of that as part of the condition in the wall Loop if this doesn't make sense don't worry I'm going to show you a few other examples so in this case it works but it's a little bit messy so the way we can clean this up is the following we can actually use the walrus operator so I'm going to put a set of parentheses and I'm going to say data colon equal to the next value from our generator and I'm going to say well this does not equal -1 now what this allows me to do is remove both of these lines and I'll just quickly show you that when I run the code this works as my code worked before so what exactly are we doing here well what we're doing is saying okay I want some variable data I need to use this variable later in my Loop but I also want the result of it to be a part of this condition in the W Loop so I say data colon equal to this is the wall operator sorry and then the next value from my generator and then I take whatever the result of this is and I say that does not equal to -1 now this allows me to use whatever the current value of data is inside of my Loop without having to Define it or reprocess it multiple times as well as have it be a part of this condition all right so here's another quick example of where we could use the wall operator to get some better and actually in this case more efficient code so let's imagine we have some function f ofx and let's say this is a pretty complicated function it might actually take a fair amount of time to run and it performs some operation on some numeric input now maybe what we want to do is we want to Loop through all of the numbers from 1 to 10 we want to pass them to this function and we want to get the result of the function only if it gives us some certain criteria so in this case if the result is greater than three well what I'm doing right now is I'm saying okay I'm going to Loop through through the values from 0 to 9 and if f with the value so if we pass that value to F and it gives us something that's greater than three then I actually want to take whatever that result is and put that inside of my list this is a list comprehension in Python and what we're doing here is actually Computing the same value two times the reason we're doing that is because we have F ofx called twice so what we can actually do is clean this up using the walrus operator let me show you that example so you can see now we have some equivalent code but that's actually much more efficient and only calling the function one time what we've done is said okay we're going to use a variable result so we're going to say 4X in range 10 if and then we use the wallrus operator so we say result colon equal to F ofx we've now defined the result is equal to this computation or to this result we check if it's greater than three and if it is we use whatever that result is to populate our list this way we're only calling the function once this is much more efficient so moving on to feature number four we have argument and parameter unpacking this is super powerful and you're probably going to see this syntax quite a bit especially in more professional python code so let's have a look at this example here let's say we have some function that takes in four values a b c d could take in any number of values but in this case it takes in four and we have some list that has the corresponding values that we want to pass into this function so what we need to do if we want to pass those values access the first value the second value the third value and the fourth value we got to write them all now it's not a huge deal if we're just passing four five values whatever but if we got to pass a bunch of them it can be tedious and there's a better way to do this what we can use is something known as the asterisk or unpack operator I can actually write asteris LST and you'll see that if I run this code it operates as I'd expect I get 1 2 3 4 now what this asterisk operator will do is what I said it will unpack the values in an iterable object so this doesn't just work for lists it works for any type of object that's iterable and what it will do is take all of the values and pass them as the corresponding positional arguments inside of this function so it will pass one for a two for B three for C four for D Etc now this will work if we did a tle as well so if we simply change this to a tupple exact same thing will work and if we change this to like a string as long as it has the exact number of characters let's not do that one let's do uh okay oops I was going to do hello you see that we get oh k a y so anything that's iterable this will work for and just make sure that you have the correct number of values because if I do okay with two y's you'll see that we get an error here where it's trying to pass Five positional arguments but only four were listed now in the same way that that works we can actually do this for keyword arguments as well or for dictionaries so you can see here that we have a dictionary it contains key and Target equal to 5 and 10 now these are the corresponding parameter values that we have inside of our function now again similarly to before if we wanted to actually pass these values we need to say values key values Target like that to pass them in so if we run this you see it works and we print out five and 10 let's just clear this here however we can actually use a kind of double unpack or keyword unpack whatever you want to call this with the double asterisk operator on our dictionary so we can say asterisk a asterisk Sate values and what this will do is it will pass as keyword arguments the key equal to the value so what I can do is run this and you see that we get 510 and what's interesting if I were to change the order around here and go Target and key this is still going to work we still get five and 10 really what this code is equivalent to is taking whatever the key values are so both key and Target writing those as keyword arguments and then making them equal to whatever their values are associated with so when I do ASX ASX value it's as if I WR wrote key equals 5 and Target equals 10 which works really well for keyword arguments you just need to make sure the name of the keys match the name of the arguments that you have or the name of the parameter sorry you have inside your function so just remember the single asteris is when you're unpacking erable objects and the double asteris is when you're unpacking a dictionary and you're going to be passing these as keyword arguments so our final feature to go over here is something called the default dictionary now before we look at that let's look at a quick example of how we typically use a normal dictionary so the key thing to keep in mind here is when you try to access a key that does not exist in a dictionary you get an error now there's a few ways to handle that with normal dictionaries but it makes our code a little bit more messy and we have to handle a few different edge cases so here we have some dictionary character count and we're counting the frequency of all the characters in The String pretty famous problem so we Loop through the string and the first thing we need to do is check if the given character is not in the character count so it's not one of the existing keys if it's not we then assign its value to zero so that way when we go to access the key down here and increment it by one we don't get an error if I remove this line here and we run the code you'll see that I get a key error a because I try to access a key that doesn't exist in the dictionary so how can we get around this without having to have that syntax that I just showed you well python has a really interesting Library called collections what I can do is say from collections import the default dictionary Now the default dictionary is what's known as a subass of the normal dictionary that has some slightly different behavior sorate that allows us to automatically assign a default value when we access a key that doesn't yet exist so we can write default dictionary here rather than the normal dictionary syntax and what we need to pass to this is something known as the default Factory now this is simply a function that Returns what the default value should be so I'm going to say Define default okay and then here I'm simply going to return zero and then I'm going to put defaults like this but notice I don't call the function I just write the function name I know this is a bit strange but what's required is that we pass some function that when called will return a value that will be the default value for the dictionary when a key does not exist so what I can do now is run this code and you see we get the exact same result so we have all of the characters counted and we don't get a key error like we had before four so just to really recap this here this function is going to be called every single time we try to access a key that doesn't exist so as we iterate through this string we see the key a when we see the key a so we say Char count at a plus equal 1 we try to access the existing value to increment it by one but it doesn't exist so we call this default function it Returns the value zero that becomes the default value we then add one to that we repeat the process eventually we get get to B same thing we use zero as the default value and we keep going now one thing to note this default function does not take in any parameters it simply returns whatever the default value should be we could return a string we could return a list we can return really anything we want it could be another dictionary but you just need to put it inside a function then put the name of the function that you want to be that default Factory so with that said that's going to wrap up this video If you enjoyed make sure you leave a like and check out my channel for more python videos like these [Music] ones