7 Python Built-in functions that will improve your life

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of the reasons that python is so popular is because it has many useful built-in functions that allow us to perform tasks in a straightforward way without writing many lines of code in this video we will look at seven different python built-in functions that will help you write more pythonic code so let's get started number one all and any the all function takes in an iterable like a list or a tuple and returns true if all the elements of the iterable are true else it returns false let me show you what i mean so let me create a list of booleans by saying boolean underscore list equals true and true now i'll say print all boolean underscore list let me run this code and we can see that we get true as all the elements are true if i change the second element to false and run the code again this time we get false similarly the any function takes in an iterable and returns true if any element of that iterable is true let me change this all to any and see what we get so here i'll say print any boolean underscore list and this time when i press run i get true because the first element is true and that is enough for any to be true these functions come in handy very often suppose we want to check if a list has all odd numbers we can do this task in just a single line using list comprehension and all let me show you what i mean let me first define a list so i'll remove this old code and i'll say numbers equals 1 comma 3 comma 5 comma 7 comma 9 then let me say all underscore odd equals all obviously i will have to fill in the expression inside inside this all i'll say list comprehension n modulus 2 or the remainder when n is divided by 2 for n in numbers and then let me print out all underscore odd when i run this code i get true now let me replace one of the odd numbers by an even number and this time when i press run i get false as expected we can also use the any function in the similar way number two enumerate before learning about enumerate let's first understand where it is used suppose we have two lists called name underscore list and marks underscore list so let me remove this old code and here i'll say name underscore list equals mary anna and alexandra similarly their marks are stored in a list as marks underscore list equals 70 45 and 96. here students in name underscore list have marks from the marks underscore list of the corresponding index so mary's marks is 70 and as max is 45 and alexandra's marks is 96. suppose we are writing a program that searches for a name and displays the corresponding marks obtained by the student for this i can write a for loop so i will say for student in name underscore list and then because i am searching for say for example anna i can say if student equals equals anna and then i can write something here now let's print the marks for anna but notice that we can't do it directly as we have looped over the iterable and not a range so first i'll have to create a counter variable and increment it in each iteration so i'll start with counter equals 0 and inside this if statement i'll say print marks underscore list and then i want the counter and if i get the marks then i want to break out of this loop and outside i need to increase the counter to counter plus equals 1. now when i run this code i get 45 which is the marks for anna as expected now we could have done this same task more easily using enumerate the enumerate function adds counter to an iterable and returns it due to this we don't have to implement and keep track of a counter variable ourselves first let me remove the counter variable so here i'll remove this and obviously i can remove this as well now i'll wrap the name underscore list list with an enumerate so i will say enumerate innovate returns an iterable with elements in the form of a tuple with counter variable and then the element itself so i can unpack these items instead of student i will now say counter comma student so counter comma student in enumerate name list now let me run this code and our program works exactly like before by the way if you are finding this video useful a sub to the channel would be much appreciated the third useful built-in function is zip the zip function takes in an arbitrary number of iterables with corresponding elements and aggregates them into tuples now that must have sounded really complicated so i will show you with an example suppose we have two lists so i can say number underscore list equals 1 comma 2 comma 3 and then the equivalent strings so i'll say str underscore list equals 1 2 and 3. suppose we want to group the corresponding elements of the number list and string list for this we will use the zip function so here i'll say result equals zip number list comma string list now let me convert the return zip object into a list and print it so i'll say print list result now when i run this code i get a list of tuples as you can see the corresponding elements have been grouped together you can do this with any number of iterables let me now show you how we can use zip to solve a problem from the previous section i have this code from the previous section and let me replace enumerate by zip so i'll say zip here and along with name list i want to add marks underscore list in the arguments finally i will change counter comma student to student comma marks because the first list is the student and the second list is the marks and instead of accessing the list like this i can print the marks directly let me run this code and i get the same output as before the fourth useful python built-in function is dir the dir function returns all the attributes and methods of an object and all the definitions inside a module or a package let's try it out so i'll go to my code editor and say numbers underscore list equals 1 comma 2 and then i can say print dir numbers underscore list when i run this program i can see all the attributes and methods that are available in a list let's now list out all the definitions of the math module so i'll remove this old code and i'll say import math and then i can say print dir math now when around i can see all the attributes and methods that the math module makes available to me another useful built-in method is the eval method the eval method returns the string passed to it as a python expression within the program let me show you what i mean so on my code editor i'll say x equals 1 and then i can say print eval and then instead of saying x plus 1 i will pass x plus 1 as a string let me run this code and you can see that 2 which is the expression x plus 1 evaluated is printed since this function allows us to execute any python expression as a string we can make very interesting programs by taking input from the user and passing them using eval let me demonstrate this to you i'll create a command line calculator in just two lines of python first i'll create an infinite while loop so let me remove this old code i'll say while true and inside this while loop let me take input from the user so i'll say input enter expression now let me pass this value to eval so i'll say eval input expression finally let me just print the value returned by eval so i'll say print eval input enter expression and now let me run this code now let's try a few expressions i'll say 4 plus 5 and you can see that 9 has been printed let me try something more complex so i'll say 6 to the power of 3 and voila you get 216. this calculator will work as long as our input is a valid python expression before moving to the next section of the video the programming team has created an app that allows you to learn python from your phone the app contains bit size lessons that are easier to understand a built-in interpreter so that you can run python on your phone quizzes and many more the app is available on both ios and android the links are in the video description another useful built-in function is the map function before we learn about the map function let me first show you why it might be useful suppose we have a list like this so i'll say numbers equals 1 2 3 4 and 5 and we have to create a new list with all the squares of the numbers from this list for this we would normally do it this way so i'll say squared underscore nums equals an empty list similarly i can say square equals let me define the lambda function i'll say lambda n colon n raised to the power of 2. now in a for loop i will append all the values to the squared numbers list so i'll say for num in numbers squared underscore nums dot append square of num let me go through this code once again first we started out with the list of numbers for which we wanted the squares then we defined an empty list that will contain the squared numbers next we defined a lambda function that would convert any number to its square finally we look through the list and append all the square numbers to the list called square underscore numbers let me show you how we can achieve this same task in a more elegant way using map the map function applies a given function to each item of an iterable let me show you how it works so i can remove all of this and i'll just write squared underscore nums equals map now map takes two arguments the first argument is the function that will convert each element of the list so here it will be obviously the lambda function we wrote before so lambda n that returns the square and the second argument will be the iterable itself in this case it is numbers here the lambda function takes an argument and returns its square and the map function applies this lambda function on each element of the numbers list let me first convert this map object to a list and print it so here i'll say print list squared underscore nums and when i press run i get the list of squared numbers the last useful built-in function that i want to talk about today is the filter function the syntax of the filter function is similar to that of the map function however instead of creating a new iterator by applying the given function the filter function filters out only the elements for which the function returns true i'm sure that sounded like a lot of mumbo jumbo to you so let me show you what i mean suppose we have a list of numbers like this i'll remove this old code and i'll say numbers equals 234 3245 i'm just typing out a list of random numbers here 690 550 and say 654 and i want to filter out only the even numbers for this i would normally use the for loop and check if every item is even or not using the filter function however i can perform this task in a single expression i'll define even numbers as a filter and i'll use a lambda function to check if it is even so here i can say even underscore numbers equals filter lambda n n modulus 2 is 0 and then the second parameter just like with map is the list this lambda function takes an argument n and returns true if that number is even and false if that number is odd now this filter function runs this lambda function on all the numbers one by one and filters out only those values for which this lambda function returns true since filter also returns an iterator i will first convert it to a list so let me say print list even underscore numbers and when i press run i made a mistake where did i oh there is a extra column when i press run you can see that i get the list of only the even numbers in this list that's it for this video if you want to revise these concepts you can find all these programs in our github repository i'll also put this link in the video description below and if you like this video hit that like button and subscribe to the channel and i'll see you in the next one happy programming
Info
Channel: Programiz
Views: 1,576
Rating: 4.963964 out of 5
Keywords: 7 Python Built-in Functions You Should Know | Python Best Practices, 7 Python Built-in Functions You Should Know, Python Best Practices, Programiz, Python Built-in Functions, built-in functions, Python, python programiz, programiz, programiz python tutorial, best practices in python, programiz python, programiz examples, programiz python examples, programiz best practices, learn python, learn programming, python for beginners, python tutorial, python, python built in modules
Id: 2_xfAoJFWBY
Channel Id: undefined
Length: 15min 25sec (925 seconds)
Published: Wed Sep 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.