5 Python tricks that will improve your life

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
for my first job I was technically hired as a front-end developer but things weren't that clear-cut on my team and for my first task I found myself having to dive into Python code the problem was I had never formally learned Python and I found myself having to use it a lot to do my job no problem I thought learning on the job is the best way to learn and I do firmly believe that that being said I had to struggle to figure out basic things a few of which would have made my life so much easier in those early days of using Python so here's five tips and tricks you can start using right away to improve your Python experience the first trick is the interactive shell let's say I have a Python file with a function that adds two numbers now conventionally I might call this function at the bottom of my file and then run the file with the Python keyword but there's another way I can run this function and that is with the interactive shell so instead of Python main dot py I add the - I flag for interactive now I can call this function in a shell like this this can be really useful for developing things like thoughts if you've seen my tinder bot video we control our web scraper through this interface so this will load your Python file but then instead of closing after running the code it will give you access to all the functions and variables inside so again this is a great way to do development testing and it can make you feel like a god when you have the interactive shell at your disposal issuing commands at will tip number two is PDB the Python debugger while Python - I will run your entire file then give you an interface into that environment PDB can stop your file before it's finished executing so you can debug I'll give you an example if I'm adding one and two and then I'm adding one and some character that shouldn't be there I will get an error when I try to run this file but let's say this file is huge and I don't know where the error is occurring in that case I can import PDB the Python debugger that's built in a Python and then call the set trace method to pause the execution of my code now I'll get this weird kind of interface that says PDB where I can do different things like print the current values of variables and functions or I can go to the next line with n for next so again this can help me identify where my code is breaking and I can even follow the debugger through complex multiple levels of function calls the great part is you can import PDB in lines so you don't even have to do it at the top of the file and then you can just delete this when you're done number 3 the virtual environment a big part of the reason Python is so dominant is because of how many good libraries it has in fact python has so many good math statistics data analysis libraries that it will probably be around for quite a long time that being said the dependency management can be a bit annoying in other words how do you confine the libraries you need to a specific project making it shareable and portable one of the ways we accomplish this is through virtual environments the most used one is called virtual end and you can install it with pip install virtual end once you do that you can create a new environment with virtual end followed by the name of the folder your environments in and then you can install the dependencies you need for the project so you do this by activating the virtual environment by sourcing the end bin activate now you'll see this little prefix in my terminal prompt meaning it is active now I can install any dependencies in isolation so let's say I wanted to develop a a bot I can pip install selenium the pendency that I might need and it will just be in this VN folder anyway I don't want to get too deep into dependencies but just know that generally working in a virtual environment is the best practice when you're using Python number for list and dict comprehension let's say you have some data a list of fruits with prices and names represented as a dict it's a pretty common use case to want to pull out a single property from an object but to do that in conventional syntax we have to do something like this so it does work but it also takes four lines and a lot of typing just to get this done so there is actually a way to do this with one line and that's with list comprehension this is kind of weird syntax that's only occurs in Python but when you get used to it and if you're not scared of it kind of like I was then it can really come in handy so within these brackets I'll type a loop like syntax essentially it's giving me each item in the list in a second variable over here and with this variable I can modify the output so this is what will go into our new list I print this out we'll get the same result as before now that's not all we can do with list comprehension we can also append things like conditional statements to the end for example if our fruit starts with an A then it will filter that list down as well okay let's talk about dict comprehension which is kind of similar to this this time let's create a dict that has a fruit name as the key and the price is the value so again normally we would have to loop through each one of these manually add them to a dict but in this case we can just do that in one line so I would do this with this syntax and printing that out I can see it worked so again this part looks the same it's just a normal loop but then we set our key and our value over here with that second variable that's created from looking at each item tip number five is to use the lambda I think the name sounded mathy but a lambda is just exactly the same thing as a function with a different syntax let me show you what I mean so we have a function for ad and I'm gonna make another function add to that has a lambda so it turns out and if I open this with a interactive shell these two are exactly the same so what it is is just a different syntax now where does this come in handy and why would you not always use lambda well this one is quite a bit easier to read and reason about what you're doing also if you have multi-line complex functions you definitely want to put them in a def now lambdas very good for example if you wanted to define a function in your interactive shell that's one case that I often use them and another probably more common example is in list methods that need a callback function for example sorting mapping and filtering so let's just use the filter function as an example which takes as an argument a function that will be applied to all the items as well as lists so if I print that out in list format I'm gonna have any list of all the numbers greater than one now this is kind of a lot of code again for what we're trying to do so one way to do this would be to use less comprehension in fact for this example that would probably be a better way or well you could again this the one line using a lambda so I'm gonna delete that function write a lambda directly in here as an argument and then use a pretty clean syntax just like that now just to make it one line I'm gonna move it directly in here as well and we'll get the same result so again there are a lot of cases where you'll want to use a function as an argument and lambdas are great in that case too I hope you guys found that useful like the video if you learned something new subscribe for more quick tips and tricks and tutorials and this is code rip I will catch you guys soon
Info
Channel: Aaron Jack
Views: 503,228
Rating: 4.9253421 out of 5
Keywords: programming, web development, react, learn programming, learn to code, coding, software development, become a software developer, python, python tutorial, python tips, learn coding, coding tips, software engineering, code tips and tricks, how to code, coding python
Id: 5tcs2qXP3Pg
Channel Id: undefined
Length: 9min 12sec (552 seconds)
Published: Thu Jan 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.