Advanced Python Skills: become a better Python developer! | David Armendáriz | Skillshare

Advanced Python Skills: become a better Python developer!

David Armendáriz, #NeverStopLearning

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
39 Lessons (4h 34m)
    • 1. Intro

    • 2. Zip

    • 3. Zip longest

    • 4. Exercise: Summing numbers like in school

    • 5. Enumerate

    • 6. Lambda functions

    • 7. Sorting with keys

    • 8. Exercise: Learning the words in a strange order

    • 9. Map

    • 10. Filter

    • 11. Exercise: Anagrams

    • 12. Exercise: Implementing XOR

    • 13. Iterators

    • 14. Generators

    • 15. Exercise: Fibonacci Numbers

    • 16. Exercise: Prime numbers

    • 17. Any and all

    • 18. Python closures

    • 19. *Args and **kwargs

    • 20. Decorators

    • 21. Chaining decorators

    • 22. The @property decorator

    • 23. The @classmethod decorator

    • 24. The @staticmethod decorator

    • 25. Exercise: bank account

    • 26. Reduce

    • 27. Accumulate

    • 28. Exercise: transforming data

    • 29. Getting and setting values properly in a dictionary

    • 30. The Counter class

    • 31. Sets

    • 32. Operations on sets

    • 33. Frozensets

    • 34. Files you must have in every Python project

    • 35. Python dotenv

    • 36. Custom exceptions

    • 37. Unit testing with pytest

    • 38. Fixtures

    • 39. Parametrized tests

22 students are watching this class

About This Class

Welcome to Learn Python by doing!

Tired of the same Python course teaching you data types and printing the same message in a for loop? It's time to learn exciting things!

In this course, you will learn a lot of built in functionalities for you to become a better Python developer. You will also learn how to implement best practices and some unit testing. Some of the things that you will learn in this course are:

  • The zip function
  • The map function
  • The reduce function
  • How to implement your own iterators and generators
  • How to use the sorted method like a pro
  • Learn about Python closures
  • Learn about decorators and some useful ones that come with Python
  • Sets and why are they cool
  • Unit testing with Pytest

And more! Also, you will be asked to do some interesting exercises in the class project, so you reinforce what you learn in this course.

Have fun and enjoy!


1. Intro: hi and welcome to learn I thin By doing so. What do we learn in these scores? How to use built in functionalities that are sometimes ignored, but she'll be used in order to become a better buy from developer. See how some things work internally in Bytom implements on best practices and some unit testing were specifically, we're going to learn how to properly use functions like seep map. Fill terse, orb seeped longest reduce, accumulate, inhumane etcetera. Learn how to implement it. Aerators and generators. Learn how to implement your own decorators with Biden closures and learn about some built in decorators like property classmethod starting method, etcetera. Hearing about sets and why are the faster for searching the least? Best practices demands your environment in your project unit testing with by just. But this course is intended to change dynamically so you can ask for more content. Just joined the discourse server or math, a six second language that I am going to provide here in the in the course description where you can chat with me and ask me for something new that you would like to be added to discourse. So that's everything I hope you enjoy. As much aside enjoyed during this course. See you in the next video 2. Zip: hi and welcome back. So this is a new serious cold learned Brighton by doing on in these videos will talk about key functionalities of fightin so you can learn these programming language faster. Okay, so the first thing or the first function, I will talk about east the seat function. I have three things to say about this thing. That's it. Function. The 1st 1 is that it takes it trebles. That means least stationary Stupples strings on DSI keeps them into doubles. Okay, so I have to make a video talking only about it trebles because you can also define your own intervals. The second thing is that this is useful for parallel situation. So we'll see what this means in a moment on. The third thing is that it returns a seep object which is an e trader off doubles. So that's first great. A least called countries. And okay, so these example will be at least on in these countries. I'm going to include mind, which is Ecuador, uh, Peru, Colombia, on the USA and the capitals of these countries which are Kito Lima border on Washington D. C. Now that's great. A sip object called countries and capitals on the first argument will be countries and the second will be capitals. Now let's sprint countries and capitals. And as you can see, these returned a seep object. As I told you in the third point which is an in trader off doubles. So all it aerators have these a special method gold next. So in these next With this next method, we can access the first an element off the city aerator which is this double at brother and get Oh, so what did what? Sit deal Waas to see if these element off the country's list with these element off the capital's least. So let's execute next again. No, we have through seat with Lima again Colombia with Bogota on again USA with Washington D. C. So what happens? Who ive? I executed school waas again. It gave me a never called a stop in duration. So why did these hair appear so e traders can be iterated just once. If you want to eat rate through these object again, we have to create the object again. So I'm going to Grady object again on now I can call the next method again on e trade through the subject. So that's what any directories And as I told you now, we're going to talk about parallel it, raise situation so we can in a for loop safe or country coma capital in countries and capitals print both of them so country on Capitol. So we have to create the object again. I'm now we iterated through country and capital in Palo, so we will have them something with country on separately, we could have done something with capital, but in the same situation off the four looks. So this is what parallel situation means. Okay, so one more theme Is that supposed we have, um, another country here. So suppose chili. What happens if we are great? The CEB object and we rained it so we can print the elements off this seat object if we create a leased out of it. So we're going to say least countries and capitals. And as you can see, the result is the same. Chile is not included in this in this least of double. So if we delayed it, it's the same result. Nothing happened. So let's add it again. Nothing happened. So why is this it's because sip takes the length off the shortest least in this case capitals. So it takes the length of off these lists, and it takes the elements of countries and capitals and starts eating them until the last element off capitals with which is Washington. But if we include the capital of cheer, which isn't Tiago, then the's last trouble is included in our list of doubles. Okay, on the last thing is that I told you that it takes any number off the trouble so we can put here some cities, like while you kill um, ano Trujillo messaging Um, New York on and about bodies. Okay, so great. Now let's seem we have to include these cities in the in the arguments of the CPE object. I know we have the sink troubles, but with ah third element in each of these double so Ecuador kita on Why kill really matter here, Columbia with damaging, uh, USA Washington C New York and chilies and terrible price. So you can take any number of few troubles that the same function takes any number of vegetables on DSI. Put them into doubles. Okay, so that's with beasts with dictionaries It's almost the same. So let's see what happens with dictionaries. That's great. Um, I don't know. Products. Um, let's see. A novel and the price in apple? Um, I don't know. Let's put here Dick Brooks. So another apple, which is an iPhone. So this gusts a lot of money on Windows on? No, I'm just putting random numbers here. Okay, so now that's great. You CIB object barracks on deck, Burke. And here I'm going to say buttocks on take birth. So what will be the output of this than same rocks and tick products? Okay, So as you can see seep when you pass a dictionary to the city function, it takes the kiss off the dictionaries and seats them together. So apple ease with iPhone and pineapple ease with windows. Suppose you want, um, now the values. So off the keys off these dictionaries, then you can do the following products that values in the Same with these one now, uh, 0.5 is seat with 1000 0.7 with 600 someone so you can remove, For example, the values here a Now see your 0.5. Is it with iPhone which is the key off these tech Berks on 0.7 is seemed with the key off off these tech product, which is Windows so you can play with that. Now let's see what happens with doubles. Well, you can actually guess it's the same thing here, so I'm going to actually copy and paste this. But instead off these being list, I'm going to change them two doubles. So the great couples, you just change the brackets to Prentice's on your records in parentheses. And let's see countries in capitals What's there? So So it's the same. It's the same result of these a lease of doubles with these elements here. Finally, let's see what the sea function does with, uh, strength. So it's put here drinks. So first string will be out of another country, which will be Ecuador on the capital, off this country, which is, kiddo, so country on Capitol will be the object. The CEB object country on capital. So we're passing only a string here, So let's see what the result of these country and Capito um Leeds is not define us are least Okay. So what the sip function did here is it took the first character which is capitally, and seated with the first character off capita, which is que so that's the first double. Here, eat. Thank you. The second temple is see And you, you and I a anti Undiano on it. Stop there because Ecuador has two additional characters which are O. R. So he took only equity unsaved it with. So this is everything I'm going to say about the seduction You can see it's very useful for ballot situation specifically that it can have another application. So I hope you like this video. See you in the next one. 3. Zip longest: I am Welcome back to learn python by doing number two. So last time we talked about the CPE function. Now we're going to talk about the CPE longest function, So these will be very short video because it's almost the same. It similar to the same function. But it takes into account the longest it terrible past. So remember, in our last example, we have these released countries, capitals and cities on dwell each of them. Right now they have five elements on we create our seep object that's created again on we create a leased out of the city object on this is er sold at least of doubles. Uh, well, aren't you all know right now? What are the what are the other in Southeast Ophuls? Okay, So what happens if I delete by Price, for example? I forgot to add Valparaiso to the cities least then we create the CEB object again, and now the last double is gone. Why? Because the sip function only takes into account the shortest list. So in this case, cities on. Then it starts to see it to the other list that have five elements. So, in order to take into account the longest least that in these gays are countries and capitals. We have to use on a special function all the seed longest. We can't import. So from eater tools we can import. Um, See, longest on now, let's replace or sleep with seed longest. Okay, so we have to execute this gold. We create the seat. Longest subject on now, the last couple east back again. So the first element is chilling the second, Santiago. But the third element off these double is none because we we don't have anything here as 1/5 element in the Siri's least. So that's why Python feel these with none. Okay. So supposed you don't want these to be known? You wanted to be a default string. So we, um, specify this parameter gold feel the values on Let's say I don't know. Um um, but bodies but bodies. Okay, so we create. I feel value. Sorry. We create the seat longest object again and create the least Andi that none and got replaced by ball buys. But remember that these will be used for all the nuns. So, for example, if I deliver this, then this is going to say Valparaiso here again. Okay, so that's the seat. Longest function. Um, I can guarantee you that it's very, very useful, and you'll see these used in very, very economy. So I hope you like this video. See you in the next war. 4. Exercise: Summing numbers like in school: Hi and welcome back. So in this video, I'm going to show you the solution to this exercise summing numbers like in school. So we're going to complicate our life a little bit here. But that's just for showing you how to use the seep longest function in a fun way. Okay, so I told you we're going to use the CB longest function, which is part of itertools. And I know it sounds silly, but I'm going to review how we some numbers in school because at the end of the day we used modular arithmetic. But maybe we weren't not conscious. At the time we were kids. We were using something like that. So suppose we have a number one, which is going to be 1564. And we have number two, which is going to be animal 587. Okay, so the algorithm was like this. We put the first number at the top, then we put the second number at the bottom and we draw a line. And well, we start summing from right to left. So this is important because we have to reverse something in our code. So keep that in mind. So seven plus four, well that's 1111 modulo ten, that's one. So you see where you seem modular arithmetic here. Okay? No, we carry one and we say six plus eight, that's 14. Ok, so 14 modulo ten, that's four. And plus the carry, that is five, and we carry it one again. Now, five plus five, well that's thin. Modulo ten, that's 0 plus the carrier which is one. Then we have 0 plus 11. And finally, we have one plus at the end of the day that's 0, loves the carry, which is going to be one again. So one plus 12. So that's the result. I hope I didn't have any mistake here. So now we're going to define a function which is going to be cold, some like in school. Ok, so we're going to have these two parameters, number one, number two, and the base. Great. And we are going to have these rebel digit seeped. So we're going to use the civil longest function. And first, we are going to reverse number one and number two. After we have casts. These two numbers to strings. So number one, number two as a string. And we're going to miss the sari field, the missing values with 0. Ok, great. It can this 0 can be an integer or it can be string, doesn't matter. You're going to see why later. So let's print the contents of digit sipped. Ok. So some, like in school, are going to pass these two numbers, 1564587. Okay, so we're missing one positional argument which is the base. We're going to default database to ten if we don't specify it. And you can see that we have 47685510. So now we can in a for loop, some distinct numbers and take into account the carry. For example, we can say four plus 74 plus seven and then apply modulo ten or modular debate was specified here. And then we continue. And in the second loop, we are going to some six plus eight modulo the base plus the carry. So that is going to be the algorithm. Okay, so let's first declare the variable carrier which is going to be initialized with 0. The result is going to be a string because we are going to concatenate a string. So this one, for example, four plus seven modulo ten uploads the carrier, which is 0, is going to be one. Then we're going to concatenate that one with these five. Then we're going to concatenate these 51 with this one, and so on. And at the end of the day or going to cast the result to an integer. Okay, so for digit one, digit, two digits zipped. We're going to declare this variable sum of the two digits is going to be, well, we have to cast digit one, then some digit two and sum the carry, which right now is 0. It is always going to be 0 at, at the beginning. Okay? Now we're going to say if sum of two digits that's greater than or equal to the base, then we have to say, well, the carry is going to be one. And the result is going to be, well, the stream some of two digits. But modulo the base plus the previous string of result, which right now in the first iteration is an empty string. Else. Well, the carry is going to be 0. And the result is simply going to be the sum of the two digits without applying modular base. And we concatenate this with the previous value of result. Okay, so that's everything. Now we can return result that gassed as an integer. Okay, so now let's see what is the result of this. So we have to do this and this. So it's 2151 like here. We can sum also in, I don't know, modulo 13. And that's going to have this result modulo, let's see 45, that's the result of modulo 45, modulo 13. Or if you're 15. Well, you can play with this a lot. As you can see, the result is the same for this modulus, for modal law or Hbase, sorry, for this base and base 11, that's the result. Okay, so now you get the point. My hope you liked this video and see you in the next one. 5. Enumerate: hi and welcome back to learn fighting by doing number three. So in this video we're going to talk about the enumerated function. So what does animate function do? It adds a counter toe on e trouble and returns on the numerator object. So remember any trouble right now are least dictionaries, doubles, strings etcetera on. Remember that the CPE function returned A CPA object that waas any trader the enumerated function seeming early returns and the numerator object which is also need terrible. So this new right function is very useful. If we combine with four loops on, we're going to see that in a moment. So let's grade a least that is going to be a fruits least on will contain a bowl in apple Um Leymah, watermelon and grapes. Okay, Now we're going to great The enumerated Fritz on these will be enumerated firsts on that springs commemorated fruits to see what happens. So us expected these returned and inhumane subject. As I told you, Andi, you can create a leased out of these and emigrated fruits because it's any trader. So let's great the least. And now this returns a least of doubles. So let's see what the first double has. So the first element off the first couple is zero on the second element is apple, so you can see that zero is the index off apple in the fruits least in the second couple we have in the first element one, which is the index off pineapple in the foods, at least in the third Topple you can see to Which is the index of lemon in the fruit least . Okay. But we can me, um, these number start from an R B trying number. So we specify the permit. Er, start on, let's say five. So right now, five is not the index off apple in the fruits list. So we cannot interpret this as the index off Apple in the fruits list. But it created these counter, which increments by one. So we have 56789 on. That's what this is start by amateur dude. So if we will meet the start parameter now, we can interpret interpret this as, ah, the index of apple in the first least, and this is very useful. Asai told you. If we combined with four lives, so let's say bys for some reason we have to map these fruits to some file. So we're going to say for index element een in Yuma rate, um, fruits. Then we're going to great final name on these is going to be, I don't know, file. That's great. The format that string file, um, index that J p t. I know on. Then you do something with a file on the element on. Lastly, you bring the filing. So it created file zero The GP day, baby Deezer file When? The day p gene and so one. So we can make these to start from one. No, this is file one file two or three or four. File five. So, um, these may not Seamus useful right now, but believe me is extremely useful on these. Functionality is already included in some our programming languages. So you don't have to make these and numerator objects in programming languages like JavaScript. So I'm just going to demonstrate this if you don't want to watch this than go to the next video. But we're going to grade the same fruits least, but in javascript it's going to be an array. So let's just copy and paste because These also works in JavaScript and let's see fruits. And in order to do a four loop, we can say for each on here we can say element on index. So this is the other way around on. We're going to say council luck. Um, let's say we're going to do and interpolation here. It's going to be file. Um, we say this index that j begin. So as you can see, it's the same result file 01 until file for so as you can see, I didn't have the necessity to great inanimate object. This is already a functionality of JavaScript, and it's very, very useful because sometimes you use these index in order to pass it as a key for some components, so it can be unique. So don't worry if you don't understand this, but believe me, this is very, very useful. So I hope you like this video and see you in the next one 6. Lambda functions: hi and welcome back to learn by from by doing number four. So in this video, we're going to talk about Lambda Functions. Lambda functions are extremely important for functional programming. On you will ask what is functional programming? Well, it's a programming cardigan that the states that computations should be treated as the evaluation off mathematical functions. So that sounds kind. The fancy on I won't like functional programming is difficult, at least for me. So we're not going to talk about function of breath coming here. We're going to talk about Lambda Functions. What are them? How to use them on these kind of things. Okay, so then the functions are also called anonymous functions because they are only so contrary to the regular functions that do define with the deaf key work. London functions used. The Lambda keyword on its syntax is like this a Lambda arguments, Gotham expression. A simple as that. You don't have to a specify a return statement. You just write the expression to be returned, and that's all. Andi, I was also going to say that you have to be very careful with using them to functions because, um, if you have a nearer in a lambda function for some reason, then it will be difficult to track that error because Lambda functions don't have a name. Okay, so they can also take a any number off parameters, just a regular functions. They are useful to understand the following video, so pay close attention. Okay, so let's great they very, very simple Lambda function. We're going to say ab easy go to Lambda the parameter onto the spare perimeter. We're going to add five. So we're going to say Brint have, um I know seven. So seven last five obviously is 12. So that's the result. And we can also use virals here inside the expression of a lambda function. So, for example, let's say X plus I Andi, I let it be got five, and so the result is the same. But you have to be very careful about this. Why? Well, let's see an example off. What will go around with this? Let's create a least off Lambda functions, so I'm going to call this list Lambda functions on. I'm going to create a least comprehension, which is going to be like this Lambda x X plus game is going to be. They returned expression. I'm going to say for gay in range from 0 to 3. Okay, so in theory, the first element of these lists she'll be London Eggs X plus zero. Right. So first time the function, I'm going to extract it from the Lambda Functions list. I'm going to say Brain First Lamba function. I'm going to pass deeper the parameter or the number five. So in theory, this show returned five plus zero. Right? So let's see what they were sold. This it's seven. So it's not five blasts zero, which is five. It's five plus two. So what happened here? The London functions, all of them to J to the last value off Jay. Which is too, because remember, J goes from 0 to 2. So these took the last value off day. Why? Because London functions are created at runtime. So, uh, the moment we executed this, the London function looked for the last element. Oh, sorry for the last value of gay on, say, Hey, it's it's too. Then I'm going to use to for the expression and that's why it rented seven. Okay, so that's something to take into consideration another thing is that as I told you, you can take any number of parameters. So let's say all too application on Let's great Alarm the function that receives two parameters Lambda a comma be and it's going to multiply these two numbers. So let's say multiplication off five before five. It's trying. So nothing special happened here. We just took two parameters. We cool. Take three. So let's add another one on here, too. Um, sorry. Four times. Five times to its 40. So that's all. OK, so we're going to use lumber functions extensively in the following videos. So I hope you like this one on review your Lambda functions by practicing them. See you in the next one. 7. Sorting with keys: hi and welcome back to learn by thing. By doing number five today, we're going to talk about two similar concepts. They are sword and sorted. Andi. They both do the same thing. They sort elements in ascending or descending order, but they're quite different and people tend to get confused that don't worry. After watching this video, you're going to know the difference between them. So sort is a method that only works for least unsorted is a function that works for any interval to remember. At this point of the course, it trebles are least so. Our dictionaries, they are the bulls, their strings, etcetera. Notice that I said in the 1st 1 that sort is a method that means that it is part off a class. It is a function that is part of a glass, in this case, the class least. So we have two great an instance of the class least, and then we have to call this method and it is going to modify the list. Sorted, on the other hand, is a function that means that we have to pass the irritable as an argument off the function , and it will return the sort, at least dictionary, topple or whatever you pass it, and it's not going to modify what you pass. You can also specify in both the parameter reverse if you want to soar your elements in ascending order, and you can also use a key to specify how the sword process should be done. So we're going to see these involvement first. Let's great are fruits list. It's going to be able water, melon, pineapple and grapes now need school. The sword method. So notice that I said fruits don't sort. And if I don't specify any parameters, then this will be sorted in ascending order on, as this is a least consisting of the strings, then it's going to sort them in alphabetical order. So let's say Let's see the contents off roots. It's able grapes pineapple on wire mountain. So the least was sorted in alphabetical order. Okay, now let's see the difference between the sort and sort, So that's great again, these least and let's call the sorted function the best fruits. The result is the same, but no these that fruits, the original fruits least is not modified. It's still the same. Why, because the sword method modified the least while the sorted function seems to returned a new least consisting off these elements sorted in alphabetical order. So this is a general common, but functions she'll never modify their arguments. That's a good program. In practice, that function shall not modify arguments on if you modify arguments, that's about practice, and you should stop doing that. Okay, so, uh, you can see we can also specify the reverse parameter. If we said it's true, then the seas, um, sort that also in alphabetical order about descending. Okay, No, let's see, What is the function of these? Keep parameter. So in order to demonstrate that I'm not going to great the list, I'm going to great a dictionary on Dwell. The keys are going to be the same, and the values will be I don't know that wanted peace off these fruits. So we have 40 40 apples. We have 50 watermelons. We have 10 pineapples. Andi 15 grapes. Great. Okay, So suppose we want to call the sword method on Fritz. It's going to raise an exception and attributes error exception saying the object has no attributes sort. Why? Because sort Onley works for lists It's a method only available on leased, so we have to go the sorted function, which takes us an argument. Fruits. And as you can see, it's sort the keys off the fruits dictionary in alphabetical order. Remember, we can soared them. We can sort the values if we called the values method on friends. So it's 10 15 40 50. But if we don't specify that, then we simply get the keys sorted in alphabetical order. Okay, so now let's define a function of both these one that is going to be called Get quantity. It's going to receive a fruit as a parameter. Um, it's going to return the quantity off Dartford in the Fruits Dictionary. Now let's see. Key is equal. Um, that's great. Another sorted here and that's a G is equal to get quantity. So let's see what the result of this is. It's different. So here we have pineapple grapes, apple on the watermelon. So what happened here? Now? The sorted function is sorting according to the quantities off the fruits, so bine apple is a lowest 1 10 then grapes with 15 apple with 40 on the watermelon with 50 . So now We're a sorting according to their quantities, not by their their peace. So that's great. But we have created these function that is unnecessary. So we're going to delete it on. Remember I told you in the last video that we were going to use Lambda functions extensively. So instead of doing these this love, the function is going to have this argument here fruit and it's going to return fruits. Ah, the quantity off that foot. A simple as that. So these gives the same result. Why? Because these London function on does the same thing as the function we declared early that now it's in line. So that means it's inside. Ah, the sort that arguments. Okay, so that's why Lambda Functions are extremely powerful. Andi, I hope you like this video. That's all I have to teach you in these lessons. See you in the next door. 8. Exercise: Learning the words in a strange order: Hi and welcome back. So today we're going to solve this problem, learning the words in a string shorter. One of your friends wants to learn new English words, but wants to do it in a certain order. First he chooses short warts. And if the words are the same length teaches them in lexicographical order when reading from right to left. So if you're a custom reading for right to left, these may make sense to you. Write a function that receives the least of words and returns the list in the order specified. Okay, fine. So Let's define our function learning words, which is going to receive a list of words. Okay, so let me clarify something. These three words here. As you can see, see US and ducks have the same length. But if we read them from right to left, then these worked begins with a, this one with e, and these one with x. So you can see that a is less than e and e is less than x. So that's lexicographical order from right to left. And then we continue with words that are a little longer. So these ones have length three and these ones have linked for and at the end, these 1.5 length life. Ok, so let's define a dictionary which is going to have the following structure. So words is going to have a skis the length of the words. So for example, West, and it has length four. And that's the value we're going to include in an array. West, the word West. Now the acts, it has length three. So we included in this array size, it has length four. So we included in this array with West size C, It has length three. So we included here, use link three, we include here, think it has length five. So we included here, yard, it has length four. So we included here, or the, it has linked war and town. It has length four. Great. And we're going to declare also an empty array. And now let's construct these words dictionary. So we're going to iterate through the list of words. We're going to calculate the length of each of these words. And we're going to say words of length are linked to use the getMethod words that get length. So here, for example, if we have length four, okay, so right now we didn't have anything in length poor. So return me and empty. I'll least otherwise return me what you have there. And concatenate with these array that is going to contain only one word, which is the current work. Okay, fine. Now we have these words dictionary. If you don't believe me, let's see what's inside these words dictionary. So let's pass this thing here. And as you can see, we have four and we have this array just like this. Three disarray and five disarray. Okay, fine. Let's continue. We are not done again yet. Now, let's iterate through each of the keys of the dictionary in an ascending order. Ok, so how do we do that? We say for length in sorted words. So remember, sorted sorts the keys of a dictionary and default behavior is to sort them in ascending order. So 34 and then five. Okay, so what do we want to do now? So suppose that length now E Street. Okay, so words of length is going to be decks C and use. Okay, so these words are the same length, so we want them to sort them in lexicographical order, wouldn't reading from right to left. So the first step is to convert this list to the same but with each of the words reversed. So X86, AES and E, SU, okay? And then we want to sort this list because now we can sort them. And if we do that, then we are going to have a S, ESU and SAT. Fine. Finally, we are going to each of these words, reverse it again. So we're going to see, are going to abuse and we're going to have tax. And that's fine. Now we have the first three words of the output. Great, so how do we do this? I'm into lines. Well, we can do it by saying for word in sorted. And we're going to do some list comprehension here. X for X in words of length. And then we're going to append, again, worked but reversed. Finally, we're going to return the result. Okay, so meditate on what is happening here. Here in this list comprehension. We are doing this step here. Now if we sort these, then we are executing these step here. And finally, we can iterate through each of these words to get c, Use and tax. And we append each of these words to the result. And finally, outside these four, we return the result because in the next iteration we're going to deal with four and then with five. Okay, so let's test our function. And let's see what happens. Now. We get c, we get use, we get taxed yard, word sized town west. And think just as the output we have here. And you can test this with any least you want, and you will get the correct order. So I hope you liked this video. I hope you like my explanation. See you in the next one. 9. Map: hi and welcome back to learn by thing. By doing number six today, we're going to learn about one of the most useful functions. Not only am biting, but in all programming languages that support the map function. So basically, the map function is an enhanced for Luke because it applies a given function. Toe each item oven in trouble. So remember it trebles right now are least dictionaries. Double strings, etcetera. It returns in my bob checked, which is an it trader Unusual. Use it instead of loops whenever you can. Let's see how the map function works with an example. So suppose we have our first list. Okay, so we have defined the fruits list on now. Suppose we want to capitalize each strain inside the least, so we're going to create an empty leased gold result. Aunt, how would you do this? Well, we can use a for loop and say for fruit in fritz result that depend front the capitalize. So these method here, as the name suggests, capitalizes the works. The first character off a word and let's bring there's old. Okay, So the result ease, as expected on these took four lines of code we can do the same with only two lines of goat . So let's say result is equal to map on. The first argument of the map function is the function that you want to apply to each item off the miserable. In this case, we can pass a regular function, those that are defined with the deaf keyword or we can simply us a lambda function. So that's why Lambda functions are important. We can say Lambda fruit fruit that capitalized on the second argument will be the end. Terrible in his case, fruits. So let's see. What is there a salt? It's a my bob kicked, remember? And these map object is any trader, So we can call these magic method called next to eat. Read through each element off the A trader. So we go next. This is the first element, which is the same as thes one water melon, which is the same as this one. Buy an apple, which is the same stays one on rapes, which is the same as this one. And if we call the next method again, then a stoppage oration exception is raised because remember, we can eat, read through any trader Onley once. Okay, so we can also create the least out of these you traitor on the result will be exactly the same. Okay, so we have used here a lambda function. But suppose we want to make some complex processing to each of these drink so we can define our own function that say it's gold process and it's going to take as an argument fruit on . Let's say the resulting string is going to be fruit that capitalized. And then we're going to say if the fruit ends with an s, then append or concoct innate these sentence Here are great elf. Concoct innate these string here. He's great and finally returned the resulting stream. Okay, so what am I doing here First, um, capitalizing the fruit. So the same as what we did with the Lambda fashion. And then we are a sane If the fruit ends with an S, that means Eve. We're talking about bureau, then concoct in eight these a string here. So for example, grapes and within this, so it's going to show grapes are great because grapes is great. Sounds weird, But for apple, well, it doesn't sound where apple is great. And let's see, we have to replace our London function with our brothers function, so we simply I replace it. Andi, there's over. Our result ablest grade wire. Melanie's grade pineapple is great on. Grapes are great because grapes ends with this. So that's the power of the map function. It's very, very useful. I use it all the time on I recommend you to each time you see a four Loeb. Think if you can replace it with a map function. Okay, So one last thing I want to say is that we can also see these in action using JavaScript. So if you're interested being learning a little bit of JavaScript to stay in the video on, if not then just passed in the next video. So for those of you who stayed, let's copy this list on in javascript. It will be called N ary. So it's now our fruits are a Okay, so the result will be fruits doesn't matter, and it will take the fruit on Dhere. We're going to say fruit char at zero to upper gays plus fruit. That's life from one until the end. Sorry, I misspelled first fruits. Okay, Unless you can see it's exactly the same result Apple, watermelon, pineapple and grapes capitalized. So I hope you like this video. See you in the next warm. 10. Filter: hi and welcome back to learn by thing by doing Number seven in these video will talk about one of the most useful functions along with map, which is Fielder. Okay, so the filter function it grades Anita Reiter from elements of unutterable. So remember it trebles right now they are least dictionaries. They are strings etcetera, for which function returns. True. So in other words, it feel there's a need terrible based in the condition given by a function. Okay, so now let's see how these works with an example. So we're going to filter a list of a strength based on the condition that it starts with the letter A. So let's great are fruits list. Apple have a Keita and the drapes. Okay, so now we're going to create our field turned fruits. And we're going to say, Fielder, and the first argument of the filter function needs to be well, it can be your own function, but we're going to use Lambda function because this is an easy task and we're going to say that the Lambda Function receives receives an argument, which is the fruit and fruit. The starts with a So this is a method for strength. If this is equal to true, then it's going to be returned. Otherwise it's not going to be returned on. The second argument of filter is going to be the irritable with cheese fruits. Okay, so dysfunction already returns true or false, based on the condition that the fruit starts her. Not with a. So we cannot meet this part. But for sake of clarity, we're going to, um, let it be like that. So you have clear that these needs to return True. If this is another function that does not a return true, then you have to somehow make it return true or false. Okay, Andi, we're going to bring the field, hurt fruits and see that this is a filter object. That means that we can call these a special method Gold next, and see the elements off the field turd of the field, her object. So the 1st 1 is awful. The 2nd 1 is avocado. And if we tried to do next again, there we have this. Stop it duration exception. Because remember it. Traders can only be iterated once. So if we want to eat rain again, we have to create the a trader again. So let's see the elements creating a list. So let's create again the field hurt. Fruits filter object. Ondas You can see we have a least of the strength. The first element is apple. On the second element is avocado. So extremely, you see extremely useful on. Remember, if you have more complexity in the logic of the field turf off off the conditions are, then you can to find your own function on bass it directly to the first argument off the filter function. Okay, so now let's see another example where we feel terra dictionary based on the condition that the value must be greater. Dan some number. So now that's great. If Fruits Dictionary Andi, it's going to the apple. And let's say this is the quantity that we have in our store of cattle that C 14 watermelon . Let's say we have uhm 50 and grapes. Okay, so now we have the first along with the quantity. And now we want to know, uh, which routes have more than 70 brought apps are 70 elements or whatever. Okay, so we want, um or let's say, 50. So we have toe elements, so we're going to have apple and grapes on. Let's see how we can do this. Okay, So filtered for its is going to be fielder on. Well, we have to pass the Lambda function. Okay, so here, we're going to have, um, fruit. But okay. Before specifying the lambda function, I have to say something. Remember that we eat rate through a dictionary, then we're returning through the keys off this dictionary. But if we want to either it through that keys and the values, then we have to call this method gold items. So before doing this fielder function, let me show you what happens if I do this. Okay, so let's great this That's Uncle Mentis. Okay. Okay. So we have a dictionary consisting off. Um, sorry. We have at least consisting off doubles on. The first element is the key. And second element off. The trouble is the value. So this is an e trader so we can pass to the fielder function. So let's uncle meant this, and we're going to pass fruits that items. So the lambda function is going to receive a couple. Well, we should call it t on the second element off the stubble is the value. So that's t one on. We want this to be greater than 50. Okay, so now we have our field turf foods. So now let's bring the result off filter fruits. Okay, so we have a list of troubles apple with 16 and grapes with 200. That's because these value here is greater than 50 on these while you here is also a greater than 50 that, um, notice that here we have a least of doubles and not a dictionary itself. So this is very easy to fix. Just create the Okay. So we're going to say this is a fruit fielder to dictionary. I'm just bus the field turf words to these dict. So But first, let's great the fielder fruits object again. Now, let's see. Um, the content off these dictionary. So, as you can see, this is the same dictionary. Ah, but feel TERT so great. We have achieved what we wanted to do on. And, well, I haven't. It's a specified or explain these dicked, but you can pass here at least off doubles on. It will create a dictionary out of that least of doubles. So that's how we create our field hurt dictionary. Okay, So I hope you like this video. These he's very, very useful on. Before ending this video, I want you to notice that we have these functions also in javascript. So if you want to learn a little javascript here and see the similarity with bison, then stay with me. You're in the council. We're going to define again our well, in this case, it's going to be a fruits airy. So we define their for Torri. And now, let's see. Result is going to be fruit, that fielder. And here, um, we're going to have our filter function. Um, okay, so these will be fruit, and we're going to see fruit starts with a And as you can see, we have the same result. So you can see the similarity we received. Um, the fruit on. We simply specify the condition here. We don't have to pass another like another argument here because we're calling, um, this method directing this is a method from thes e trouble, which is fruits. So now we ended this video. I hope you like it on See you in the next one. 11. Exercise: Anagrams: Hi and welcome back. So in this video, are going to solve the following problem. And the grams, you will write a function that receives the word and the least awards and stay with all of those words in the list that are anagrams of the word passed. So for example, we can have here in the input, the first argument is going to be code, and the second argument, which is the list of words. And the code can be D, CO, can be got firsts, 35x, priest. I don't know how to pronounce these invented words, but you get the point. So the output shall be code, decode and cut. Because if you arrange these words somehow, they all our code at the end of the beam. Okay, so what if I told you that we can do these? We can solve this problem with only three lines of code. Ok, so the first line of code is going to be the function definition. Find anagrams. We're going to pass the word on the list of words. Okay, so let me actually toggle the line numbers. So you can see there are three. We're going to define this variable, which is going to be called sorted words. And we're going to sort. So in this case we're going to sort code. It doesn't matter if it's ascending or descending order. So after doing this, so this is a common, so this doesn't count. But goat. If God is reward, then, and if we are starting these in an ascending order, then we have that this is going to be something, no, sorry. A, B, C, D, E. Okay, so that's coat sorted in ascending order. And then I'm going to return the following thing. So we start with a list. So we're going to convert something to a list. And what is this something? Well, it's going to be a filter. Okay? And in the field after the function is going to be lambda function, which is going to verify that sorted x is equal to the sorted word. Ok? And the second argument is going to be the list of words. Okay, so if we don't count these comment here, 12123 lines of code, OK? So what is this field are doing? It's taking every element of the list of words. It's sorting that element in ascending order and verifying that he, that sorted word is the same as this one. And well, if it's the same, then it's obviously an anagram. So we should included in the list. Okay, so fine. Let's that star function with a goat. And the second argument is going to be this list here. Great, so we have code decode and he got, so that's all, that's the solution of this problem. I hope you liked this video and see you in the next warm bye-bye. 12. Exercise: Implementing XOR: Hi and welcome back. So today we're going to solve these very, very easy problem, which is the X4 or exclusive or a prayer. So it is a boolean operator with the following truth table. So you can see if we pass 00 or false and false, that should return false. Okay, so remember 0 can represent false, one can represent true XOR of false and true, that's true, x or of true and false. That's true and x or of true and true that's false. So the x operator returns true when one and only one of these two arguments is true. So that's the difference with the OR operator that it returns true even when both arguments are true. Okay, so this is very, very useful in electronic circuits. That's no, that's not my specialty, but I know it's very, very useful. So you must write a function that receives two least us. The arguments. These lists must consist of zeros and a one. And once, let's call them a and b, the function should return a list where the ith element is x or of a and b of i. Ok, so let me say something before I continue. The XOR operator is already built-in in Python like in many other languages. But here, these exercises we are trying to practice with the map and SIP functions. So we have here an example where the input data, the first argument is 0011, the second argument is 0101, and the output is 0110 y, because x or a false and false, that's false. False and true, that's true. X or of true and false. That's true. Annex or of true and true that's false. So that's why this is the output. Ok, so we actually can do this in two lines. So the first line is going to be the function definition. And the second line is going to construct the output. So we can say return aliased, that is constructed with the map. So in the map function, we have to say that the function is lambda x of 0. And here we use the carrot symbol, or also called circumflex, I think. So that's the, or, sorry, eggs or operator. Okay, so we're saying that x is an array which contains two elements, 0 and X1. So those should be on these two elements here. I'm sorry, x is not an array, it's a tuple. And well, the second argument of the map function should be these two lists, but CPT and that. So let's test our function with zeros 0110101. Okay, so that's 0-1, 1-0. That's tested with 10111001. So that's 0010. Let's check XOR of true and true that flows XOR of 00, that's false. Xor of 10, that's true. And x or of 11 or trend through, that's false. So that's everything just, this is very, very easy. It's just for you to practice. And half knowledge, have more knowledge of the map and zip function. That's all. See you in the next video. Bye bye. 13. Iterators: hi and welcome back to learn by thing by doing number eight. So now let it's time to create our own e traders. We have seen examples off the traders like the filter object, the CEB object, map, object and so on. Now it's time to create our own e traders. So far, we have used those two words a lot on May be, I have used them interchangeably, and sorry if I did that. But there is a difference between them. So let's see first these dear friends in theory, and then we're going to see the difference in cold. So unutterable is a representation of a series of elements that can be traded over so, for example, least stationary strings. But they don't have an illustration stayed, such as current element. Ah, but every irritable can be converted to any trader by using eater. We're going to see this in a moment, and typically, an e trouble should be able to produce any number off E traders. So, as I said, at least, is an eat trible. But it is not an in traitor, so let's again use our fruits. Example East. So apple, we have apple. We have watermelon on. We have grapes. Okay, so remember that for any traitor. We called these a special method gold. Next. Let's see what happens if we try to do that. So as you can see, we have an attribute error saying that least object has no attribute. Next. Also, we can use next, directly bypassing these ah, fruit to the next function on. Well, now the error is clear. It says, least object is not any trader. So this is the same as doing these that now if we try to do this, the error is clear. Is it saying the least? Object is not an interior, so it can be clearer than that least. So are not a traitor. Okay, so now let's see what Anita Reiter is. A niche aerator is the object with an inspiration. If state, it lets you check if it has more elements using the next or these next that we have used in the previous videos on move to the next element. If it exists. Okay, so now the question is how to come, Bert, at least to an e trader. Well, it's extremely simple. We simply say, Let's say fruits is equal to eat their fruits. Now fruits is actually an e trader. So that means that we can call these next method. And now it is giving us apple the first element. This is equivalent by coming this next function, I'm it is giving us the next element, which is fruits, then grapes on. Then stop integration exception because now fruits east an e trader and I have said these a lot of times it traders can be traded just once. Okay, so, no, you may be asking How does four loops work? Because if I do this for fruit in fruits Brantford how it is possible that we can execute these four loop many times, So see them the the last line off the second point, and Trible should be able to produce any number off valid it traitors. So under the hood the four loop is creating. It's creating an any trader out of fruits every time it gets executed in fat. Let's let's create the implementation off the four loop Mannelly. So you'll be surprised that four loops are actually infinite wiles. So this is how four loops work. Okay, so let's remove this and let's see it terrible fruits is going to be Fruits are either fruits and we're going to say wild. True. We're going to try. We're going to try to get the next element, which is next off e trouble. Fruits on, for example, That lets sprint this element. Andi, if we get this exception, which is stop it duration, then we're going to break the four loop. So this is the actual implementation off a four loop. We have implemented four loops manually. So as you can see, we can execute this. However, um, many times as we want on, it's getting the same result as a four loop. So, um, four loops under the hood. They're creating it traders out of the trouble on their, um calling his next method on the a trader until the stop it aeration error a cures. Okay, so now that you know the difference between an e trader and then it terrible, it's time to create our own e trader. So for these, we're going to need to create a class. So we're going to say class and let's say we want to return the powers of two. So glasses they are written in capital there. So powers off to is going to be the name over class. And, well, all classes have this magic method gold in it, which is that constructor and the first fermenter itself. And the second parameter is going to be the maximum, um, number that we want to get the power off, though. So, for example, we can default is 20 How we're going to say self that Max is equal, but the maximum is equal to maximum. Now we have to implement to a special magic methods called eater. And also, um, next. So the 1st 1 is going to be eater on this is going to do two things. The 1st 1 is to initialize these, um, viable end to see euro on the second thing these eater function is going to do. He's these, uh, Terry Breath is going to destroy return self, so to return the instance off the glass. Now we have to specify another magic method. Gold next. So next against going to have self super amateur, we're going to say if self the end is less than or equal to self, that a maximum, then we're going to say, um return search result is equal to two to the power off self that we're going to increment . And by one. And finally, we're going to return the result else, we're going to race the stop eat rations. So if n is greater than the maximum, we simply stop and raised the stuff situation. So we have created our own e trader. Now let's create an instance off this glass. So we're going to create these powers of two. And let's say we have We want the part of two until five. Okay, so we say next the object. Andi. Okay, so here we have an error because we haven't great created an eater trader out of it to create any trader at Lafitte. We have to pass these eater. So sorry. I forgot to do this eater. Now we can use this next allowed giving us one to four eight on the last element. She'll be 16. Sorry, the next and last elements should be 32 on now. We have to half this is stop it. Duration exception. Okay, so this is how you implement it. Traders by yourself? Andi Hope No, it is clear. Um, see you in the next video 14. Generators: how you welcome back to learn by thing by doing number nine. So today we're going to talk about generators. But first, let's review what we did last time. So we were creating our own e traders on Remember that we had to create this glass power of two. And in order to create any trader we have toe specify this magic method called eater on. We have also to implement this next method. So by implementing these eater, then we can pass the instance of this class to the eater function on with these next magic method. Then we can use these next to that a trader. So that's how we created our own it traders on. One thing I want to mention before learning generators is that it traders can be infinite. That means that we can create powers off to an infinite number of times. So we didn't have, um, these stop inspiration in that case. So let's modify this a little bit. So we're going to get rid of this condition, okay, on also in the constructor, we won't have a maximum because we're going to have an infinite number off powers of so in order for the constructor to do nothing. We just say yes. Okay, So no, we didn't have here a maximum. But we can still pass this to the inter function on. We can execute these next. An infinite number Off times on, we're going to get 30 to 64 all of the powers off to available. OK, but you have to be very careful when doing these, because suppose you want to have any trader that we turns the number off of all the primes . Um, yeah, the brand numbers, for example. So as you know, right numbers are infinite, but taking eve brain if a number is prime, is very expensive. They're very advanced methods to check ive a number is probably a prime or not, but that's another story in order to exactly check if a number is primer nut, that's a very expensive task to do so you can run out of memory on DSO You have to be very careful when doing these infinite E traders. Okay, so now that we have talked about, um, e traders, let's talk about an easier way to implement them. So these are generators us, I say here in the first point. There are simple way of great creating a trader's, because all the work I mean, implementing the eater on the next magic and methods are handled automatically by generators on they are easy to implement. We just have to create functions that returns elements with dealed with this cure, Jill, instead off return. Okay, so, um, these generators, they can contain one or moral or more deal the statements on when cold it returns an object which is an it trader. That thus not start the execution immediately. Okay, so we're going to see these in a moment. And the other thing we have to note is that once the function deals an element, the function it stops, it is post on the control east transfer to the color again. We're going to see these with an example. Um, one of the most powerful things with generators is that local variables. So variables that are inside those functions and their states are remembered between successive goals. Okay, so let's implement these powers off to with, um, a function. Okay, so we define our normal function, So we're going to call it powers off to Okay, So let's first create version where we have a maximum. So we are going to pass a maximum two dysfunction on. We are going to implement here a four loop. So for I in a range maximum, we're going to say maximum plus one. So we include the maximum. Then instead of returning, were going to say deal dealed, um, to to the power of I Okay, so now let's see this in action. So Powers and went to name these variable powers powers off to on until five. Okay, so now let's see. We can apply the next function directly to powers. And now it's one to four 8 16 32 on the next time is going to raise thes Stop it. Duration. So was you can see these does the same as this, but this is easier to implement. Okay on. Well, this is the version where we have a maximum. But we can have also the version where we don't have a maximum. So, like the one that is implemented right now. So here we just, um, replace the four loop with a while on initialize these viable I Andi, After this, we simply, um, increment the values off. I buy one. Okay, so now these doesn't need the maximum. And next next. Four, eight, 16 32 64 so on. Okay, so note one important thing here. Note that what will have happened? Ive I, um if I had a normal function here, Okay, so maybe these while and true Ah, wouldn't have stopped. So I waas caught in an infinite loop. But this is not the case. Once the function and reaches this Jill, the statement, it stops. Like I said in these Boeing here was the function, guilt, the function he's supposed on. The control is transferred to the color in this case, on the color is ours is variable powers. And so that's what this point means that the function is bossed or stopped. Okay. Um, so this is how you implement generators? Believe me, they are extremely, extremely important. Um, on day are not only used in by thing there, they are also used in javascript, for example, for ah library called Reddick saga. If you have heard of it, um, generators are extremely important. And for a synchronous things that we're not going to discuss in this video, So, um we're going to discussing another thing which our generator expressions. So it's even an easier way into to construct generators. So let's rigo how we create the list. So no, we're not going to create our fruits. Least this time we're going to simply declare a list of numbers. So 368 Okay, so a generator can be these hours I'm going to. So, instead of, um, instead of using brackets who are going to use parenthesis, we're going to say X work for eggs in numbers. Okay, so this is not a couple. The seas, we can say at generator expression, which are assembles, Ah, lot to least or dictionary comprehension. And this is going to be a generator. Powers is going to be a generator. So let's see. Now we can call next hours on. As you can see, it returned nine with cheese, three squirt. Now it's six, which is six squared. And now 64 which is, um, eight squirt. If we call it one more time, then we get our well known Stop it aeration exception. Okay, So why we should use generators because they are easy, easier to implement than a trader. So Okay. Now it's fixed easier than implement. And it traders they remember efficient because, um, generators returns elements just when you need them. So just when you call them, they don't create die entire least, for example, in this case, and the generator isn't creating 9 36 and 64 at a time once it's creating those elements just when they are called. Okay, so that's why their memory efficient on. They are also excellent to generate infinite streams like we saw in this example here. So again, you have to be careful because it doesn't mean that, for example, finding the next spring numbers not going to take a lot of time because you have to check if it's divisible by, ah lot off number. So you have to be careful with this. But besides that, they're excellent to generate infinite streams on, they can be guidelines. So what do I mean by that? So let's take this cold here and put it here. Okay, I know. Let's create these, um, generator here on, Let's say X plus two four x in powers. Okay, so, no, let's see what the result is going to be. Okay, so next additions. As you can see, it's 11. So what happened here? We first did the calculation X squared, uh, which is three squared nine. And then we did. The other calculation, which is adding by two till nine plus two, is obviously 11. Let's see the other element 38. So 36 plus 2 38 on 66 which is 64 by two. So this is what pipeline is that? We can first do this and then do this on duh result is going to be coherent. Okay, so I repeat generators are extremely important. Um, they are also used for other, more advanced things, like graph theory. I have seen lots off algorithms that used generators. For example, the breadth first search, if I'm correct, uses generators to find the shortest bad BP's between two notes. Another example is what I told you earlier. Reddick. Saugus to manage some actions, Um, synchronously. So this is pure JavaScript bar. But it's okay on Well, they are extremely useful. And I hope you learn how to do now. It traders easier and well quicker. See you in the next video 15. Exercise: Fibonacci Numbers: Hi and welcome back. So today we're going to solve these problem, the Fibonacci numbers. So they are very famous and they have been solved and million times bad. Maybe you haven't seen the solution with generators. People use to solve our generated these numbers using the recursion. But that's not necessary when we have such a powerful tool. Lagged generators. Also remember people on that team members are built summing the last two numbers of the sequence, sequence. So we start with 01. So the next to number will be the sum of these two numbers, which is 0 plus one is two, sorry, one. Then the next number will be the sum of the last two numbers. One plus one, that's two. The next number will be one plus 23 and so on. So let's apply what we learned about generators that creating the famous Fibonacci sequence. You must create a generator that yields few and the two numbers. It should be an infinite stream generator. And the function one receive any argument. You shall not use recursion. Instead, you should take advantage of the fact that dinner eaters, remember the state of the function. Also remember that the first element, which is seemed 0 is 0 and the second element index one, iss one. Okay, so let's create our generator Fibonacci numbers. Great. Let us start with the first element which is 0, and the second element which is one. Okay, so the first and the second time we call the next function on this generator are going to simply deal 01. The third time we call the generator than we're going to enter an infinite loop. So while true, C is going to be equal to a plus b, and a is going to be equal to b and b is going to BC. And we can actually do this in the single line by saying a comma b is equal to b comma c. And we don't need this line here. Simply, let's yield B, which is now c. Okay? So that's everything. We have created, the generator for the Fibonacci numbers. So let's call it. Okay, so let's called the next function. It shoe deal 0 now to deal one, now 12358 and so on. So it's infinite. And we can get all of the people Neitzsche numbers by using this generator and we didn't have to use recursion. Well, that's everything. I hope you liked this video. See you in the next one. Bye-bye. 16. Exercise: Prime numbers: Hi and welcome back. So today we're going to solve these very interesting problem, which is to write an infinite generator that yields prime numbers. So this is one of those generators that you have to be very careful about because checking if a number is prime or not is a really, really expensive task. In fact, in this video, we're going to use the most expensive. You can also save the most naive approach to solve this problem, which is take all the numbers from two to n minus one and then divide by all of those numbers and check if it is divisible by one of those numbers. If that's the case, then this is not a prime number. Else this is a prime number. There are a lot more solvable approaches to this problem. For example, I remember that when I was in university, I was in a class that was gold Number Theory. And the professor taught us how to calculate the probability that a number is prime or not. Because you can see that checking if a very, very big number is prime or not may take lots and lots of time. Ok, so prime numbers are also very important because they are used, we a lot of cryptography methods and they are used for securing the internet. So all of the security over the internet relies on the fact that knowing a number is prime or not is very, very difficult. Okay, so we're going to also modify this generator so that it deals not only the next prime number, but also the time it took to find that prime number. And we're going to make a little bloody using matplotlib. So let's first create our prime numbers generator. So the first brain number is two. And we're going to deal too because this is a very special prime number, because it is the only prime number that is even. Ok. So then while true, we're going to say n plus one. So we're going to increase n by one, are going to assume that this is prime. And then we're going to check if it is prime or not. So we are going to iterate through two until n minus one. So remember this n is not included in DC in this for loop. And we're going to say, if n modulo one i is equal to 0, then it's Brian is going to be false. And we're going to break this for loop because we don't need to check anymore with the other numbers. Okay, so if we iterate through all of these numbers, but he's Brian, it's still true. And we're going to say, yield this number because it is a prime number. Okay, so as I told you, this is the most naive approach. You can. For example, only iterate through a square root of n, the floor or the ceil function of the square root of n. That's sufficient. But we're not going to do this right now, are going to test our prime numbers generator. So let's say prime numbers is going to be equal to prime numbers. Okay? I'm going to say next, brand numbers is going to be two. Then the next one is going to be 357, and so on. Now, let's modify this function a little bit. We are going to import the time library. Okay, so now, instead of only yielding the prime number, we are going to yield a prime number and the time it took to calculate the prime numbers. So like a tuple, the first element's going to be the prime number and the second one is going to be the time. So for the first case, we're going to assume that it doesn't take any time to yield these variable. Well, it takes maybe a couple of, well, very, very few seconds, maybe. Nothing, it's very low, so we're going to say it's 0. And then here, when we enter the for loop, sorry the while, look, we're going to say is start time. It's going to be time, that time. So this gives us the current time. Now, when we yield the next prime number, we're going to say n time is going to be time that time, so the current time minus the start time. So that's the time it takes to calculate this sperm number. So we are going to yield this tuple. Ok, so fine. Let's first execute this cell, this one, this one, and this one. So 203 and that time 57. So it will be nice if we could visualize this relationship. And for that, we're going to use matplotlib to make a little plot where the x axis is going to be the prime numbers and the y-axis is going to be the time that it took to calculate that prime number. Ok, so let's import matplotlib pyplot as beauty in another cell. Well, let's execute the cell. Here. We're going to say x, the values of the x axis and live the values of the y axis. So that's execute everything again. We have our prime numbers and now we are going to iterate 1000 times. So we're going to calculate the first 100000 prime numbers. So for i in range 1000 are going to say, give me the result. And remember this is now a tuple. So we're going to append the first element, which is the prime number, the first element of the tuple, which is the prime number, and the second element, which is the time. Okay, so now x and y is populated with data. Now let's create our plots. So we're going to say plt.plot dotplot x comma y. So that is going to gives us the plots. And then we're going to set a title time taken to calculate prime numbers. And then we're going to set an x label, which is going to be the prime numbers and the y label, which is going to be time in seconds. Okay, so let's see. This is the blood. So in the y-axis, we have the prime numbers, and here we have the time in seconds. Okay, so as you can see, the time increases when we increase the prime numbers. So it looks like the last prime number, or 1000 prime number is somewhere between 8 thousand. So it's clearly an increasing relationship. And that's obvious. Well, it's obvious because we are checking against more and more numbers. Okay, so as I told you, there are lots of ways to calculate. If a number is, well, we can't, we don't have any formula, any magic formula to save a prime if a number is prime or not. But we can have more sub-goal methods to know if, if a number has high probability to be a prime number are not. Okay, but that's why advanced. I hope you liked this video. See you in the next one. Bye-bye. 17. Any and all: hi and welcome back to learn by thing by doing number 10. So by now you should be comfortable with these concepts off it trebles and the traders a gay. So well, we're going to learn to functions that are applied to eat troubles. Okay, so any is the 1st 1 which returns true of any element of an it trebled is true. And fools otherwise. Um so if at least the one values true, these functions going to return true. If all values air force, then these functions going to return falls. And if you pass on them t trouble, then you have to ask this question. Is there any value in these miserable dancers? No. So these returns false. So that's just a way for you to remember. What happens if you pass on empty it terrible to this function. Okay, so let's great are fruits at least which have apple by rabble. What arm? Ellen Avocado. Okay, so now let's create a fruits mapped, which is going to be a map object on the Lambda function is going to be okay. So if this starts with certain is fruit starts with a then return True. Els returned false. So let's see the content off these fruits mapped. And let's see, what is there sold off the any function that you can. Um, well, this is not going to work, because once we create these fruits map, then these and the fruits mapped is going to be in. So first, let's see whether the result here is Okay, so, um, we have to true here through intro and two false. So the any function shield return true? Yeah. So it returns true. Okay, so, no, let's grating another map off. Get, uh, which is going to say return Even the laying off the fruit or the length of the off the string east Greater than 50. So none of these drinks Havelange greater than 50. So every element here she'll be false. So let's verify that. Okay, so every element here is false. So now let's ask the map, object directly to the any function, and it returns false. Okay, So far, so good. Now let's spazz an empty E trouble, for example, and then released and these also returns falls. Okay, so that's everything with the any function. Now let's see what all function does. So it returns to if all elements of any trouble are truth and fulls otherwise so, if you have at least one false in your trouble, then this is going to return Fools ive all values are true. This is going to return true and you bask in MT trouble Then this is going to return true. Okay, so let's great again there for for it snapped on Let's ah see in action the first base that says at least one value is false. Okay, so we can and repeat our example if fruit starts with a We have already seen that we have to truth on two falls here. So the all shield return falls and that's correct. It returned false. Now that's the old values are true. So that say that Len Off road is less than 50. So every string here of length that are less than 50 So these shoe consists all off truth. So the whole function she'll return true. And lastly, let's see what happens if we pass an empty list, for example, is going to return proof also. So that's how the any and the all function works. I hope you like this video on See you in the next one 18. Python closures: hi and welcome back to learn Pathan by doing number 11. So in this video, we're going to talk about by thumb Closures on well, closures are not restricted only to bite them. They are also available in programming languages like JavaScript on the concept is the same . So this is a very useful, important concept that will be very important for the next video. So pay attention. Okay, so closures are one of those concepts that are better explained with examples than with theory, but we're still going to see some theory. So let's start with three things that we need in order to have a closure. First, we must have a nested function that means a function inside another function, the nested function must refer toa, a value defined in the enclosing function on the enclosing function must return the nested function. Okay, so let's first start with a simple example. We're going to define a function Gold Brent message on. It's going to receive a son argument message on inside this function, we're going to defining other function called printer. This is not going to receive any argument on it's going to print message and finally, um we call the printer function. So from now on, we're going to call these the inner function and these the outside function So leads, um goal. These function here, the functional print message on gets past these string. Hello. On the answer if Hello. So these function oriented. Hello. So what happened here? So there are some things that happen here that are interesting. The inter function has access to these argument off the outside function. Okay, Andi, one thing that you have to take into into consideration is that these, um bearable here is read only That means that you cannot modified. Let's see what happens if we try to modify it. So we're going to a benda hello world here and see what happens. It raised an error on bound local error. Local variable message reference before assignment. So in order to modify these message Ah, very well. We have to specify that it's a non local viable, and we use the non local keyword to achieve. So we say none. Local message on now. You cooled. Modify these message barbell. Okay, but this is just something you have to take into consideration. But we're not going to modify the message, rival. So we're going to believe that part of the coat. Okay, so now let's do some things likely different. So let's return now. The printer. The printer in your function. Okay, so leads. Um, so I don't know how to call this another function. I don't know. Um, on, Let's seem what happens if we call these another function? So it takes no arguments? Because remember these in this another function is the Brinker function, which takes no arguments, so we can call it with no arguments. Okay, so these viable east a function on it returned. Hello? Okay, so this is interesting because, um, these function here finished its execution, but still remembers these variable message. So what happened here? Well, we had a closure, and that's why um after returning the dysfunction printer, it's still remembers these argument message. In fact, let's see what happens if we delete the print message. Ah, function. So now we don't have that function anymore. In fact, let's see what happens if we try to do this again. So name error bring the message is not defined. Why? Because we use this del operator, which means delete and Now, these mess these function, this outside function doesn't exist anymore. Okay, So what do you think will happen if we do this? Another function? So it's still there? It's still there. Um, besides, we deleted the outside function, so that's what a closure is. So here we did, um, comply with these three points. We had a nested function. The printer function. The nested function must refer to a value to finding in close and function. So these message a variable and then closing function returned our our inner function, which wa sprinter. So this is a closure again. So I went to use closures. Well, closures can avoid the use of global values and provide some form off that hiding it can also provide enough did are entered, solution to the problem. So, for example, let's define a class which is going to be very, very simple. So, for example, it's going to be multiplier. It's going to have this innit? Magic method. Um, we're going to pass these, uh and we're going to say itself and is going to be the end. And then we're going to, um, multiply the dysfunction multiply, and it's going to receive these x on its going to return x times self that. So let's see what happens with this glass. So we're going to have on object on the multiplier. So we passed this and so every element. So every star, every argument that we pass to these multiply is going to be multiplied by five. So, for example, seven is going to return seven times. Five that that is 35. Okay, but, um, we had to and implement a constructor to implement its function in to implement a class. It's better and easier to do this with a, um, function with a closure. So we simply say OK, so pass me that, um viable. You want to multiply those bodies, which is going to be our end on here. We're going to define again our multiply it's going to you can pass it the x, and it's going to return x times and and this is going to return the multiply. Okay, so this is exactly the same thing, but with less lines of codes and without the need to create a class, so I don't know, a multiplier. Maybe this by 500 by five. So this is going to be more the Bleier five. And now we can multiplier by five. Um, call it, I don't know. With the argument seven again on it's the same 35. So this is equivalent to these. But if you have more methods in your class than this is not a good solution, this is just when you have, like, one method in your class and you can use a closure and tow, avoid trade in the class on all the things like creating a constructor and and all the things associated to create in the class. Okay, um, they are also extremely useful for implementing decorators. So that's what we're going to use in the next videos on. That's why I presented you this video here. Okay, so that's all we're going to learn in this video. I hope you liked it and see you in the next one. 19. *Args and **kwargs: I am Welcome back to hurt by for my doing number 12. So before continuing learning about the graders, we need to learn one more thing which are arcs on works. So in Beit, then we can pass a viable number of arguments to a function using some special symbols. The 1st 1 ease asterisk arcs, which is stunts for non cured arguments on the 2nd 1 is double asterisk quarks, which stands for the word arguments. Okay, so the 1st 1 asterisk arcs. It allows us to pass a viable number of parameters which will be encapsulated in a double. So let's see these with an example. That's great. Our function that with odd numbers on suppose, first we want to add to number. So we simply say, a plus B on return A plus B. Okay, so let's call this function with with one or two. The result is three. Now, suppose we want toe some three numbers. So we created another variable. See on we say return a plus B blasi. Now there are 76. Okay, so now suppose we want to some, um I don't know, 1000 numbers, so it will be a problem to create went 1000 rebels on return a plus B plus C plus D until 1000 variables. So that's where arcs come in. Okay, so we say here arcs and let's first see the type off arcs. OK, so the type is a trouble. So all of these numbers are in a double Onda. We can even call these without any arguments on it still will be an empty tubal. Okay, so now we seem to save return the sun off arcs, and that's all. So if we call it with Syria argument than it zero if we call it with these numbers than that, Sir Sultan too, and so on. So that's how you use these arcs in a function you can obviously access them. For example, if I want to access the first element just and say Brent arcs zero on Well, one is the first down. Okay, so that's very, very is. In fact, this is a very easy topic. I know. Let's see what quirks does. Okay, so, uh, that's great. Now a print words function. Andi, it will receive works. Okay, So first leads being the type off works. Andi, let's pass. Let's call this function with apple. And suppose we have a quantity of 16 water melon. We a quantity of 100 on grapes. We've 2000. I started 200. Okay, So notice how I passed the arguments here. I bust them like this. I didn't create a dictionary. I just specified the key undervalue. Okay, so the type off courts is a dictionary. So all of these were encapsulated in a dictionary so we can do the things we can do with a dictionary, for example, for key and value in works that items. So we trade through the key on value. Let's spend key on value. Andi. There it is. Apple 60 watermelon, 100 grapes. 200. So let's frantic works so you can see that it's a dictionary. So it's a dictionary. And here we didn't print arc, so you can see that it's a double. And as you can see, it's a double. Okay, so that's everything with this topic. You see, it's very, very easy, and it's very, very useful. Okay, so now let's talk about in the next video about decorators. See you there 20. Decorators: hi and welcome back to learn by phone by doing number 13. So finally we are going to talk about decorators, so they are extremely, extremely useful. So pay close attention to these in Brighton and other programming languages like, for example, JavaScript. We can pass functions, us arguments, toe order functions. This is because everything in Piketon is an object, including functions. Functions are also optics. So that's why we can also pass functions. US. Arguments, um, to other functions. We have already seen this with functions like Filter and not so remember the first argument of filter. He's a function. It can be a lambda function, or it can be an ordinary function, but you get the point. The first argument is a function on the second argument is that it's terrible. Such functions that means functions that receive arguments, functions that's arguments are called Heidger order functions, and you may have found abbreviations like hlf. So every time you read these in an article or in a book, hlf means Haider order functions and they are very, very common, um, so H o F can return functions as well as we saw already enclosures and the definition of decorators. The creators are hunger order functions. Um, the takes of functions as an argument on that some extra functionalities and red turns it a simple as that. So let's see an example off our first decorator. First, we have to define the outside function like we did enclosures, and it's going to receive a function f as an argument. OK, now we're going to, um, do you find that inner function which right now is not going to, um, take any argument and we're going to print? I am a decorated function. They created function. Finally, we execute or coal f on return the inner function. Okay, so far, so good. Okay, now we have to do the following thing. We have to define our hello world function or whatever you want to call it on. This is not going to receive any parameters on. It's just going to bring hello world a symbol of that. Okay, now, that's great. Another function eso I'm going to call it decorated. Hello, world on. We're going to cool rapper with hello world as a function. Now we just gold. Um these function here and this is the result. I am a decorated function. Hello, Work. OK, so first let's see what's happening here. Um, here we called, um, these decorated hello world with no arguments. Why is that? Because here were simply executing these inner function or calling this inter function so the inter function doesn't have any parameters on. That's why these function doesn't have any arguments are parameters OK, so, um, this is how decorators work on there is a syntactic sugar that you may have seen that instead of doing these wrapping the function and calling, I'm going it We simply do this rapper, and that's all. So now we simply call Hello world. Um, that's all I am dickered and I am a decorated function held work. So by doing these, we are, um, calling the inner function of the rapper directly. So this is a syntactic sugar. They like it to call it like that syntactic shiver to remove unnecessary coat. Okay, so that's what the creators are. Now let's see another example. Um, but with for amateur. So now the inner function is going to receive parameters. OK, so let's again define a wrapper. Function is going to receive on a function as an argument. Now the inner is going to receive two parameters. So the function that we're going to pass is going to have two parameters or two arguments. Okay, so, um, first, before encoding the creator, let's define the function that we want to decorate, which is going to be a simple divide function. Okay, so we're going to return a divided by B A simple s that, Andi, obviously, if you divide by Syria, then you will have a never So you may be asking why Just you don't do this vehicle to serve zero print, we can divide. Um, we can divide else, return a divided by B. Okay, we can do this, but suppose that we have another function like, for example, complicated operation that, uh, again receives A and B. And it's doing some complicated thing here on at the end return something that resembles a divided by B. So we have to do the same thing here on we are duplicating coat. So there's these principle called the r Y. Don't repeat yourself. And that's when decorators come in handy because they allow us not to repeat coat. So Okay, we can do this, but we're not going to do this, so return a divided by B on. Okay, here. We're going to say, if these equal to zero, then we're going to bring. We cannot divide by zero or we cannot use zero. We cannot use a V zero. Um, we're going to return anything else. They were going to return f A within two arguments and be okay. Finally, we're going to return. The inner function of simple is that now we decorate dysfunction here with rapper. Um, we call it like this. OK, so first, let's call it with one of five. It's your going to now with one and zero. It says we cannot use the zero. Um, it didn't return anything on a side. Told you we can use these rapper with complicated operation, which can, which is not necessarily dividing by, but we cannot be zero Andi. Then we're using these same coat on. If we use be equal to zero, the same message is going to purr, so it allow us toe not to repeat coat. Okay. Okay. So the last thing that we're going to see in this video east to putting practice what we learn about arcs on quarks. So, um, let's define again our wrapper function. Andi, this time we're going to measure the time that it takes for the function to execute in seconds. OK, so we're going to define the inner function here on. We'll know nothing about dysfunction. We don't know which kind of function is how many parameters it takes. So we can simply say arcs works. And by that we are granting that it can be any function. So here, um, actually, we have to import some library here. So it's the first time I think that we import something. Um, we're going to say, start time the time. Then we are going to execute these function. We bust our eggs on arcs on. Finally, we measure the in time. And, friend, um, these were going to use f strength here. These function, Duke. Um, and so time execution time, we're going to define this variable, which is going to be end minus and start execution. Tired. Okay, finally, we have to return the inter function. We wrap any kind of function here, actually, so I suppose a square root? I don't know it's covered. No. Let's call it complicated. Operation Okay, so this is going to receive a B and C on. The first thing that it's going to do is a plus B plus C than our is going to be multiplied by a than our is going to be divided by C. And finally it's going to retire or print are Okay, so let's pass. Complicated Operation five eight on 10 on 19 sir. So as you can see the result East 8.42 the function to 0.2 I think this is seconds. I'm not completely sure, but I think it's second. So 7.67 67 ah times 10 to the minus five seconds. OK, so that way you can use this wrapper function with any function you like independently from their function signature. That means independently for their arguments on dso one. So this is everything I need to tell you about decorators. See you in the next video 21. Chaining decorators: hi and welcome back to learn by thing by doing number 14. So we still are going to learn something else about the creators is going to be very easy chaining decorators. But before we move on, I wanted to tell you that decorators are everywhere in by them, especially when you use thes framework gold flask, which he's very used for Web development. Aziz their logos is one drop at the time, so it's a maker framework and it's very, very flexible. I like it a lot more than Django. Maybe you have her of that and well, in the first program that you write in flask, then you encounter these from flask import flask. App equals off last name on, then in the third line of code or well in the fourth. In this case, if you use thes whitespace, then you're going to encounter a decorator that up the route and this is how you define a route in flasks and then you decorate your function, which is going to return Hello world, and then you can go to your local house on CDs. Hello world in your screen. So that's an example that I wanted to show you on. Let's see now how to change and decorators. OK, so let's first find our rapper one function which is going to receive these function f on. Then the inner function is going to Well, you have. We have already thing that we can pass arcs on parks directly. Um, I don't know. Let's seem, um I am the first, the grating, the greater then we clef with arcs and art. Um, here, I'm going to print I am the end off the first, the greater the greater and return the inter function. Now let's copy and paste thes growth. Andi here is going to be a rapper too. I am the second the creator. I am the end off the second decorator. Okay, so now we're going to, um, Finer hello world function, which is simply going to bring tell worked. Hello, words. Now we're going to decorate these one with rapper one on. Well, you already know the result of these, Sir. We call it directly. I am the first decorate the creator. I am the end of the first, the greater so nothing special. But we can decorate all of these with the second decorator and we simply have to, um specified and that rapper I, both the first rapper. So now it's wrapped inside the second decorator. So the order is important because if we change the order now, these is going to be the result. I am the first a greater and second a greater and well, it depends on your application off what you want to do. So that's all I wanted to say in this video that you can also change decorators. And this is actually extremely important and useful because in flask, for example, there are some other the creators, for example, my thing, it's called lugging required. So this is for protecting your your out. So if the user is not loving, then you can use this decorator in order to specify. Hey, you need to be first logging in order to access the space. There is another one. I think in school circuit that I o or something like that. I don't remember exactly that. That's for saying, Hey, dysfunction is going to be a socket. So this is going to receive the events off the second so they are extremely useful on. Well, I hope you like this video and see you in the next warm 22. The @property decorator: hi and welcome back to learn by from by doing number 15. So we have learned a lot about decorators. Now it's time to explore some built in decorators that by them can offer to you. So we're going to start with the property decorator. So what does this property decorate or do? Well, it helps us to find centers and getters for classes in a convenient way. So let's greet the glass cold person. That's great. A constructor which is going to take the self age. And it's going to say self that age is equal to each on that's Bill discotheque. Very method, which is going to be very easy. It's going to say if age is less than 13 then it's going to return. Kit um, you've age. I will sort itself the H self. That age is less than 13. A leaf self that age is greater than or equal to 13 and self. That h is less than 18. Then you are 18 ive. You are greater than or equal to 18 and has done 65. Then you're probably another and else return elderly. Okay, so that's a very, very simple glass. And now let's create an object out of this class of person is equal to person 25. So if we call the category method, it's going to return all those. Okay, we can modify these variables directly. That h variable, we can say 13. We can access these viable also directly. It's going to return 13 and we can also call again the category method, and it's going to return team. Okay, so suppose you are the person who is in charge of implementing this. Plus, So this glass, it's part off a huge code base because you and your team are building a huge application, and this is just part of that code. So someone and in other parts of the code is using your class on Do something like this person that H is equal to minus five. You can access the verbal directly. It's minus five, and you can, um, cold Secretary method, and it's going to return kit. So this is a very unfortunate case because, um, well, negative values Well, h can be a negative values on these ease. This doesn't make sense at all, so you have to validate that the age must be greater down are equal to zero. And then you can set the value, Um, to whatever number you pass it, you can also, for example, validate that it is a an integer instead of afloat. So something like that. Okay, um, so this one off the things that bothered me a lot when learning beytin? Because look at that. You can modify these variable directly. You can, um, access to its value directly. And as a guy who I learned programming with C plus plus, then this is very, um something that is very me. A lot. Why? Because in C Plus Plus, we had these keywords where we call the specified for the age to be private or public. So something like this, but in beytin we don't have We don't have something like this inviting, but we can still replicate that behavior using that property decorator that we're going to see in a moment. Okay, so let's remove all of these things. And now let's modify our class a little bit. So first we're going to modify the constructor and we're going to define these get age function, which is going to bring this message getting the age so it's clear whenever we call these method Andi, this is going to return self Dutch on their score each. Okay, so why did I bother to put these underscore before the name of the viable? Well, this is a convention. It's simply a convention to tell that these needs to be a private verbal. So it's just convention. It doesn't do anything else. It doesn't do anything magic. It's just telling other people that see your code, that these East a private, viable and shouldn't be touch unless you use centers on getters and remember centers and getters well, as they need. The names suggest they get the value or said the value. Okay, so this is just convention. You have to be very clear in that it doesn't do anything magic. Now we have to set the age, and it's going to receive self on agents parameters. And first, we're going to say, if H is less than zero didn't bring the message. We cannot have negative ages. Else. We're going to bring the message. Setting the age on same self that underscore H is equal to whatever value you pass it. And now every time you call the constructor, you have to call the set H method so you have to do something like this. So now let's create a new instance or an object off this glass. So let's past 35. So as you can see, um, whenever you creating new object of this glass you are calling the set age method. So that's why um, we are here now we're here. Each is a greater done or equal to zero. So we're Ridge, this else condition here. It prints this message and, um, sets these underscore h variable to 25 so we can get the age like this. And now it's 25. Um, let's change the age to 13. Um, good, age 13. So it's working correctly. But if we try to, um, for example, colder continue method, we're going to get an error. So let's see the error. So it's a tribute there. A person object has no attribute age, because now it's not called H. It's gold. I'm underscored each. So we have to do this in every self. The age we had. So now it's going to work. So let's see. Now it's printing. Continue 18 for 13. Okay, so there are two problems with these implementations. The first problem is that we can steal axis the underscore each volume or viable directly. We can still modify it so we can do something like minus five. And we didn't get any error if we do something like that. So that's why I told you that when I started learning by phone and I encountered these, Um uh I said Well, so what's the reason for the get agents at age, then they have no reason to exist because we can still do something like this. The underscores, I told you is not doing anything special is just saying to other people this is a private variable. Don't touch it. But if you are a rattle on, do you want to touch it? Then you can still do it. So it didn't make sense to me coming from C plus plus. But then I learned about the property, their creator and then it made more sense to me. This is the first problem. The second problem with these implementation east that then you're 10 other people. Well, you have to set the age with these set H method. And if you want to get yet the H you have to call this get age method. Before these, they were doing something like this, remembers person that h equal to 13 and they were retrieving the value by simply calling person of H. And now you're telling those people know that's not like that, and more. You have to call the set age on the get age. So maybe that those people are going to get upset because maybe the I assumed that the U already created those age variables with property. But in fact, Yudina. So you have to modify your code somehow t o be coherent and make it coal or said the age like you need before. Otherwise, those people may get mad on they because they need to refractor their code to set the age with this method and to get the age with this method that they don't want to do that. Okay, so that's where properties come in handy. So let's lead all off these things. And now let's return to the original version over class. So remember we had self that age equal to age. The this is going to be untouched and here we're going to, um, go back to the original version of thes method. Okay, so now we're going to say, H, he's, um, property. So remember, decorators are rapper functions, and in fact, they can get more than one function. So the first functions got going to be the gator on the second function is going to be the set age, So the center. And now let's see what happens if we create an instance or an object off this glass. So as you can see, it's sprinting the message. Setting the age. OK, But why did this happen? If in the constructor we are not calling the set age method, it gets called automatically every time you want. Toe set the value off age. So, for example, if we try to do something like that now, specify 13. Now it's calling first the set age method. So it's passing through this method first in order to set the h of value if we try to do something crazy like minus five or minus four. Sorry, it says, we cannot have negative ages. So it's passing through the set age method first before sending the value off age so we no longer need to do something that, like, said, age because we can re directly like we did in the first example on Let's see what happens. You. We tried to access the Valium. Then it's rent in the mist message, getting me getting the age So you are passing through these get age method first. So that's what the proper team decorator does. Onda. Let's see also, if the contemporary method is working so it's working. But it's sprinting, getting the age three times. So why is this happen? Yeah, it's because first we are calling get age when we the glare sums like these self that h is going get age. So it's Brendan getting he age, Um, but our H is not less than 13. So we go to the second condition and we call Self that each again on and himself that each well, we call it here two times. Plus this time, then we're calling it three times because here were returning. So we're not reaching these conditions here. So we're executing self that age or getting the value of age three times. So that's why this message is printed three times so you can delete the message. If you don't want to see that, A message coming up three times. But it's clear how it's getting the message. Are the age, sir. Okay, so, um, that's very, very useful. Um, but on personally, I like how the sees the fine age, equal property and then we parts of functions. But there is a syntactic sugar for doing this same thing. So, um, people that despite them don't like to a right getters and centers so didn't like to define method, like get age or said age. They just want to write hh. So here what? They just want to write a check on age. So, um a both the getter we have to specify here the property decorator. And here we can see Asia, that center. So here we're telling these, take these as the center and this is going to do the same thing. So let's see, the gold is unchanged. So people prefer to do it like that. I prefer to do it like the manual way and defining the getters and centers because, you know, it remembers, it makes me remember to c plus plus, but that's okay. We're learning beytin. So do it like this. It's better on. Well, one last thing I want to tell you is that we can. We can also specify two more things apart from getters and centers. We can specify the leaders if you want to call it like that. And dark, which is can documentation for the property. Okay, so that's everything I wanted to tell you in these video help you learn a lot. So this is getting really advanced. But it's for you to know more about biting on. Get to a senior developer position if possible. Okay, so see you in the next video. 23. The @classmethod decorator: hi and welcome back to learn by thing by doing number 16. So we're still going to learn about decorators built in the creators that bison has to offer to you And do they were going to talk about the class method decorator. So first we have to define what is a classmethod. So it last method is a method that is bound to a class rather than its object. Okay, so we can call these classmethod without even creating an object. So that's cool. And it doesn't Willis. I told you it doesn't require the creation of a glance off a classy instance and less method works with the glass, since its parameter is always the glass itself. Okay, so the first argument has to be the class where it's attached. So if we defining like deaf classmethod, the first argument has to be the glass, just us. We define methods, ordinary methods, um, in a glass. The first argument yourself referring to the instance off the blast self refers to the instance of the plus. Now these CLS refers to the class itself. So let's see these with an example. But first, um, I have to tell you that they are used generally for creating factory methods on what they're fucked through. Methods will. They are methods which returns a glass object, so they are very, very common. So that's why this is a very important concept here. Okay, so let's great a glass like with in last time we created this person. We created the constructor. Um, so this is getting self age and over the same self that age is equal to H. So we're not going to use properties and complicate our laps here. We're just going to make, like, the first version off our person on Well, let's define also hey, display each, um, method here, and it's going to simply brains. Neff string. Um, here, we're going to say, the age of the person east on self that h okay, so nothing fancy with this class, we can past the age and called the display H method. Okay, so nothing fancy, as I said, but suppose we want to create an instance off this glass. But instead of using the age, we're going to use the birth year. So that's where class methods come in handy. So we're going to define these person from birth year birth, dear. Unless I told you the first argument needs to be the class itself. So the CLS is referring to these person class on the second argument is going to be the eight. Sorry, the birth year. Okay, so, um, we have to wrap these. I am with this glass method decorator. And also, we have to import I'm the date from the daytime library. So sorry from the time in Port Date. And OK, so we're going to return the class. Um, but remember, here we have toe pass the age that the birth year. So first, let's calculate the age. So we're going to create these variable called calculated age, which is going to be We can get the gear from today and we can substructure the birth year . Obviously, we have to take into grids, consideration the month on the day. Maybe, but we're going to make it simple under it like this. Okay, on. Then we passed the calculated age, and that's all. So no, we can called these classmethod without even creating any instance of the class. So how do we do that? Well, we say person dot person from birth year and we pass the birth year, which is, in this case, 1998 on. Obviously, these needs to be these will return an instance off the glass. So we have to assign these two person one, which will be an object off the person. Plus So let's see, person one. We can well see the eight, which is in this case, 22. And we can, um, call this method a sorry display age this place age on it, saying that the age of the person is 22. So we have called this glass method without creating a I an object as a we. We can call it with that Create an object on assign to this person one variable, which will be any instance off the person class because this is returning at class itself. Well, on instance, off the glass, actually. Okay, so this is very important. Very important application factory methods are extremely used. At least I have seen any machine learning used a lot. And something more technically is that you can, for example, um, I don't know, inherit from person. So let's great these on the employees simply class, and it's going to inherit from person. Okay on. Okay, So when we inherit from these glass, we are also inheriting the methods in this case, person from birth Year on this class is not going to do anything, So just pass on. Okay, so let's see what happens. Um, let's create these verbal employees and we're going to call these person from birth year and they this thing here. And of course, we can display the age things like that, sir, this play each so the age of the person is 30. Okay, but more importantly, ease that we can see of the instance off employees. So let's see if employee is an instance off the class simply So this is true. Um, yeah. So if you didn't understand this last part So don't worry, because thes easy instance, I have to dedicate the video just talking about this instance. But it's our this classmethod. It's guaranteeing that these employees it's going to be off the type or will be an instance off the glass employees. So that's all. This is a more technical thing here. Also, we can see the type or simply, and it's going to be on object of family. OK, so don't worry if you didn't understand sport, If you understand that we can create instances off the class where these classmethod is attached, then that's nice. And well, I hope you like this video and see you in the next one where we will still talk about decorators. 24. The @staticmethod decorator: hi and welcome back to learn by from my doing Number 17. So we're still going to learn about decorator some useful decorators, and this time we're going to learn about the static method decorator. So what is a static method is stepping methods. Much like last methods are methods that are bound to a glass rather than its object. So this means that we can use them without creating an object off the glass, and they're not dependent on the state of the object. So the difference or the key difference between the strutted method and the classmethod is that aesthetic method knows nothing about the class and just deals with the parameters. Last methods work with the class since. Remember the first parameter off a classmethod. He's the class itself. Remember in the last example that we did? Hey, factory method using at last method. Because last methods, the first argument is always a class, so that's very useful. But static methods don't know anything about the class, So if they don't know anything about the class nor the object, when are they used? Generally, they're they're useful to group utility functions toe a class, so studying methods can be outside of the class. They can even be, um, in other models. But if conceptual is speaking, that method makes sense to be attached to a class. Then you should declare it as a static method. So let's see what I am talking about by declaring this class our which is going to have this constructor in it, which is going to receive self and seconds. So we're going to say self, that second sequel, Two seconds. And now we're going to use aesthetic Method decorator And here we're going to say, Convert seconds to, um hours. Well, let's keep it sure to our and it's going to receive only one argument, which is seconds. It's not going to receive self nor class just seconds as any other normal function. And well, there are many ways to convert seconds to hours, minutes and seconds. But the most concise way to do this is to do the following time. That string from time and was busy five deformity hours, minutes and seconds on the second perimeter is GM time. We passed the seconds, So this is the most going sideways. Maybe you can find another way, but this is a wine 19 or so. That's why I like this. And then we're going to make a normal method from the class, which is going to be display our okay, So these research receives self as the barometer because it's a normal method on we're going to see say's Our is going to be self that convert seconds to our and we're going to bat self that second. So notice what happened here Convert seconds to our is a static method, but it's still attached to a class. So we can, um, call it from another method that used himself that come Bert seconds to our So that doesn't being a starting method doesn't mean that we can't reference it from another method using self dot com, Bert seconds to our on the argument. Will these yourself that seconds? Because this is just a number, and then we're going to return these ever stream or well, just print bring the next string sane. Um, the hour is and then does the variable our Okay, so that's everything that's now create, um, a an instance where an object of this last hour. So let's pass. I don't know. 65 seconds. Now let's display. That's called the method. Display our. And as you can see, the hour is one minute. So zero hours, one minute, five seconds. So that's correct. But now suppose that remember, you are building the huge application, and this is only one part of the coat. So another part of the code wants to convert the number of seconds toe an hour. But they do not want toe create an object off the class because that will be, um, well, that will bring more problems like like memory and so on. So we can simply call this aesthetic method with that, creating an instance of the class by saying our that convert seconds to our and pass directly So I don't know, 65 again, and now it's returning the same hour as on a streak. So this is very useful because we don't need to greet an instance off that glass, and we still get the same function out. Um, so that's everything with static method. It's very, very easy on. I hope you like this video and see you in the next one 25. Exercise: bank account: Hi and welcome back. So today we're going to solve this problem bank accounts. In this exercise, we will be creating a bank account class. This class must have a constructor that receiving initial amount and set it to a property called balance. And if you don't provide an initial amount, then you shall set balance with 0. You shall write the setter and getter for this property, the center she will validate if the amount you Path is greater than or equal to 0. And if it is negative, the valence shall be set it to 0. Okay, so let's apply our knowledge here. That's great. The class bank account. Let's define the constructor which receives self and the initial amount, which is going to be set to 0. By default. I'm going to say self, that balance equal to initial amount rates. And now we're going to set this property with this decorator, are going to say balance itself. And this is going to return self balance. Okay? Now, let's write the setter. So remember, this is the getter. Now we have to write the setter. Let's say balance, self and amount. And let's say if the amount is greater than or equal to 0, then self balance is going to be equal to this amount. Else. The balance is going to be set to 0. Okay, so pretty easy. Now let's define an instance of this class. So bank account, bank account. Ok, so let's see. Bank account balance. That's 0, okay? And now if we say BankAccount that balance equal to 60. Let's bring it again. Bankaccounts, sorry that balance. Now that if we set this to minus 60, then this is set to 0. So this is pretty easy. I hope you liked this video that you apply the knowledge. You acquired, the setters and getters and see you in the next video. Bye bye. 26. Reduce: hi and welcome back to learn by phone by doing number 18. So no, it's time to learn about the reduce function. Remember when we talk about the map function? And I said that it was the most important concept off functional programming. Well, the reduced function is as important as the map function. It's on Lee. It's also sorry, extremely used in big data. So there's the thing called Hadoop mob produce, which is a suffer framework for big data. And if you want to do big data than probably the first to function that you're going to encounter our map un reduce. But what Dusty's reduced function do? It applies a function of two arguments. Communicative lee to the items of the trouble from left right so as to reduce the trouble to a single value. So I say that it's the contrary to the map function, because with the map function you are creating another. For example, in view basil east. Then you are transforming that least I'm but with the reduced function, then you are transforming that leased to a single value. Okay, so this is one of those functions that I use a lot, especially in JavaScript. It's also available on very useful. But here in Brighton you have to import it from the phone tools library. So that's the first thing you have to do. And now we're going to see a simple example, which is something the elements of a list. Okay, So as I told you, the first element ease a function which takes two arguments and in this case, well, remember, Lambda Functions can take any number of arguments. So we have X. Why on we are saying that the operation should be X plus y, then we because the e trouble on there is 1/3 argument that we're going to see in a moment , which is, um, well, you can say the initial Isar. That's let's see what the result off this as well. This is 15 and I'm going to explain you what's actually happening here. So in the Lambda function, the first argument X is the accumulated value. While why is the current value So in the first iteration were simply doing one plus two. Then, in the second iteration, we're doing one plus two plus three in the fourth. Then X is going to be one blister, blustery. And then we had why, which is the fourth element, which in this case, is for and now X is going to be one plus two plus three plus four. And why is going to be five? So that's how these reduced function works. And, well, maybe thesis an overkill, because you can apply sun directly to these, um, list. But suppose you want a nuff set. So for you have tow, um, initialize the some intent. Then you pass these 1/3 argument, which is the offset if you want to see it like that. And now the the result is 35. Why? Because, Well, the sum of all of these numbers is 15 as we saw earlier. But now we're something 10 which is the office of so 10 plus 15. It's 25. Okay, so this is extremely useful. Um, again, if you have more complexity into your into your function, you can to find your own function. But remember that that function should have two arguments on. In fact, we can replace these. So there's another that very cold operator and, for example, from operator, we can import some, um, very common operations like act. So we we do adhere with best Eric yet. I'm sorry. Then it's 25. If I say move, which is multiplication than, um Well, I have to change here more. Then This is the the result because, well, we have one times two times, three times, four times five. That's 100 training time stand. That's 1200. So, in fact, this is a better way to implement the factorial so we can implement this function. Factorial. Andi, I remember when I first started learning programming, I had to use Riker Shin, but Okay, so this is easier. Just called the reduce. Say that the operation is the multiplication and then pass range from one to n plus one. Okay. And that's your factorial function. Then you can call it with five. And it's 100 training just in the one line. Why? Because we are multiplying all the elements from one to end. That's the definition of factory. Well, of course, maybe there is an error. If we tried to do factor l zero, but well, that's in a special case. Um Well, um, there is a lot of applications for the reduced function But you have to keep in mind that it helps you to take unutterable and reduce it to a single body. And that's why it's called like that. And this is extremely, extremely important. So I hope you like this video on See you in the next one. 27. Accumulate: I am welcome back to learn fighting by doing number 19. So today we're going to learn a function that it seems hard to the reduced function, but it is likely different. So it is the accumulate function, and it seems they are to the reduced function. But it returns the intermediate results, so reduce just and returns the final result. But the accumulate returns the intermediate steps that it took for four for it to get to the final result. Okay, so also it's different because you have to import it from the eye turtles library and not from the funk tools on. The first argument is not. The function is the irritable, so let's seem that's important is from the ICTR toes on. That's also ah from the operator Ivory Import Multiplication. So we're going to replicate the example that we live in the last video on. Okay, so result is going to be accumulate on the first argument East going to be the trouble as I told you so. First, let's great some numbers here, so I don't know. Around the members here we buzz their irritable on down. We asked the move. Okay, so remember these can be your own function if you want, but it has to receive two arguments. Okay, Now, let's spring result and see that it is a non object. On the subject is Anita Reiter. So these means that we can eat a rate for step in result, are going to bring step and see that it's 14 28 because of Jenny. Eighties one times, four times seven, then 84. Because it's one times four times seven times three, and finally, 106 68 which is the multiplication off all of these numbers. Okay, so basically, uh, this is what they're reduced. Function will return us, but the accumulate function is returning us all the intermediate steps. So this will be useful. For example, if you want, um, to draw some graph, for example, accumulated grafts that will be useful for ah. And also, if you have well, you want to know danger. Immediate steps. Okay, so that's all this wasa short video. I hope you like it and see you in the next one. 28. Exercise: transforming data: Hi and welcome back. So today we're going to solve this problem, transforming data formats. So we're going to apply what we learn with our reduced function. And Well, just to say the reduce function is extremely useful to transform data structures. And when I say data struck first, I'm not talking about queues, link at least and all of those things I'm talking about how a dataset is presented. A structured. For example, suppose we have this list of dictionaries or list of objects. If you want to say that we have these invitees, list and each element of the list is this dictionary or object. We have this key email. We have this key name, and we have this key stats. So well they email is a string, the name also string and the status is also string. And we want to transform this list of dictionaries to single dictionary, which kind of looks like this. The email and the status. So Alex at is attending Brian at declined, and so on. So maybe we want to do this because we want to present this in the front. And so when the client side data needs to be structured like this, so you can present it more easily. Okay, so here the reduce function can help a lot. You just have to write an adequate function. And maybe using a lambda function would work here because it's a little more complicated. It's not that complicated, but maybe constructing a nother function, pass to the reduce function is a better choice. And pass it to Derrida's reduce function along with the invitees list and an empty dictionary as the initializer. So without further ado, let's import from fundamentals, the reduce function. Okay, great. Let's write a function which is going to be called transform invitees data. And we're going to pass the invitees list of dictionaries. And this function, which I'm going to write right now, we can define it outside or inside this function. It doesn't matter. We're going to write it here, transform data. And this function is the function that we're going to pass to the reduce function. So the reduce function is going to take this function and do its job. So remember, we have to pass an accumulator and invite T, which is a single element of the list. What are we going to do with it? Well, we're going to say the new key, which is invite T of email, is going to have the value of invite T status. And we return the accumulator or ACC for short and outside this function. So in the outer function we are going to say results is equal to the reduce function. We're going to transform the data. We're going to pass the invitees lists. Our bass initializer, which is an empty object. So this is also the current value of the accumulator if you want to say it like that and return the results provided are returned by the reduce function. Okay, I think that's all. Lead. Declared. This variable invitees here. Okay? And let's call this function with these invitees and see the result. Okay, so as you can see, we have just as this example here, I like sad attending Brian at, that client and so on. So this function is what, what we needed in order to achieve this. And the reduce function, obviously. So as you can see, this is a very, very powerful functions. You can chain it with. Maybe with a, with a map function or filter if you're returning a, a list. So that's also very useful and very common to-do. And well, I hope you liked this video, the solution of these problem. And see you in the next one. Bye-bye. 29. Getting and setting values properly in a dictionary: I am Welcome back to learn, Pathan, but doing number 20. So in this video, I'm going to teach you how to get and set the values properly in a dictionary like a by than pro. OK, so first, let's define Harford's dictionary has always on. We're going to have apple here. We're going to have 60 water melon. Um, you're 70 on Don't know rapes 200. Okay, so our usual dictionary. So what happens if you want to access the value of apple, then well, you simply do for its apple on you get the value of 60. But suppose you had a typo here and use belt apples instead of apple. Then you're going to have a key error. And to avoid this key error, probably you need to wrap these coat in a try, except you have to know that it would raise a key error and, well, that's cumbersome. So there's a better way to get values from the dictionary, which is the get method. So if we call this get method with apple, it's going to return 60. But ive you call it with Apple, then it's going to return None. Why? Because well, Let's print actually this soul to force the non to appear. And there it is. It's returning, known. So you think this get method. He's better because you won't have these error and you don't have to wrap your code and catch that exception. So if that key doesn't belong to the dictionary, it's just going to return them. Okay, so let's see again are Fruits Dictionary. It's unmodified, but there is another method from dictionaries that you can use in order toe set the value of, for example, apples. So let's say fruits and then we call the set default. And, for example, if I call it with Apple, which is a deed that belongs to this dictionary, then it simply going to return 60. But if I, um, want to set a default value for a key that doesn't exist, then I have to pass another argument and say zero, for example. Now let's see the fruits dictionary again. Now it's up old 60 were among on 70 grapes 200 now we have apples with zero. I'll say. OK, so this is very useful for the following thing. Suppose I'll have a friends list. OK, so we have apple. We have watermelon, We have apple again. So we want to count on put in a dictionary. How many times this apple appeared. Okay, so we want a dictionary that says apple two times on watermelon one time. Okay, so for these, maybe. Well, there are better ways to do this, in fact, but work for the sake of presentation, we're going to do it like this. So for fruit in fruits list. So what would you do you e before watching this video. Maybe you did this effort not being, um so that's great. An empty dictionary. Um, I know it's going to be called quantities, so it's going to be an empty dictionary effort, not in quantities. Um, then we're going to say quantities off. It is going to be one if wanted tees off road. Um, you didn't want to that. OK, so now let's see quantities, as expected. Okay, but we can do the same thing, but with one line of goat. So saying quantities off fridge is going to be quantities off that set default. Sorry. Oh, friends. And the 1st 1 is going to be cereal on plus one on the result is the same. So here the first time that the quantities dictionary seed these fruit keep and it's going to this default it to zero and then it's going to add one and then the second time it sees at the fruit, then this is going to return one on. Then it's going toe. What? One again? So the result is going to be too and wellness. You can see it's easier doing DIY separation this way. Okay, so you have to be comfortable using now get and set default because they are the proper way to get and said values in a dictionary. So I hope you like this video. See you in the next one. 30. The Counter class: hi and welcome back to turn patent by doing number 21. So now we're going to learn about the counter class. So the counter class is just a subclass off the dictionary class, but the country Class E's enhanced for counting fashionable objects. It is a collection where elements are stored. This dictionary values Asari. Dictionary keys under counts are stored as dictionary values. OK, so in other words, counters are simply dictionary dead counts elements from manageable. These counts are allowed to be any integer, including zero or negative counts. On the counter glass, it seems, are two backs or multi sets. Another programming languages. If you have heard off these concepts, elements are counted from any trouble, as I already told you, or initialize from another mapping or counter. So first, of course, we have to import the counter class, and it is available from these collections library, which is wilting and by thumb. So from collections were going to import counter. And now let's see an example with our fruits, least you're going to say apple using this least three times water melon is going to be one time here on Graves is going to be two times here. Okay, So now we can, um, define another object off this last counter. So we passed the constructor fruits. And now let's see the, um, elements of or what does these object half. So the subject or dictionary has thes apple. We with a count of three, this watermelon with a count of one and these grapes with a count off to so very good on, we can. As you remember, this is a dictionary so we can get it with the get method on, say, Apple well is going to give us three. If we pass apples, which is a key that is not present in this dictionary, it's going to return them. But let's access elements like we did before. So, like this, it's giving a sample. And remember, this should give us a key error, but with counter, it's giving us zero. So whenever a key and the sun does not exist in the counter, then it's going to give us a count off Cyril. So it's safe to call it like this or to call it with get in this case, there is no difference because, um, we get you can also maybe I didn't specify this, but if you you don't want none to be returned and you can specify this second argument and then it's going to be the same. So you can replace these with zero with none are with whatever value you want to get if the key doesn't exist. So this is equivalent to these, in these case for the, um, counter class. Okay, so, um, there are very useful methods. Um, bounded to these counter class on the one that you that I use the most East called most common. So let's see counts that most common on I can't pass here. He number. So if I say one, then it is going to give me the most common element in these fruits list as a couple. So the first element of the double will be the element. The key and the second element will be the value. If I say to here, then it's going to give me the two most common elements in the suitable on. If I posit with no argument, then it's going to give me all elements as the least off doubles. Okay, so this is very useful. For example, when you want to calculate the mode, for example, of a set. Um, this is very useful. You just have to call guns must come. Aan den. You will have your your mode game. Um, there are other methods that you can ah, research in the documentation in the biting documentation. But remember that this counter, um, can't count also from any trouble. So, for example, if I say counter and I pass here, I hope I am spelling these word correctly not, uh then correct me and look at this. So remember, strengths are also e troubles. So this gives me at account off the character. So am appears just once I appears four times the ass appears four times on p appears one time. So this is also valid view past any trouble. So we have learned the difference between intervals and interrogators. So at this point, you can pass to the counter any beatable if you want. So that's everything I have to tell you about the country class, as I told you, is very useful for finding the mode from a set on. Well, see you in the next video. 31. Sets: hi and welcome back to learn by thing by doing number 22. So they were going to talk about an interesting and that destructor, Gold said. So he set, basically is a collection which is an ordered and un indexed. So, contrary to lists, remember, at least have in excess on. So they are ordered said's are not like that. That's still sets are in trouble, mutable. That means that you can add more elements to it after you created unlike doubles, for example, and has no duplicate elements. So remember, in our fruits least we can have, um, apple three times grapes, two times water Mendham n times, I don't know, but with sets they're going to appear just once on by, Thens said. Less represents the mathematical notion off a set so you can do basic operations like the union intersection. Different symmetrical difference, etcetera because they represent these mathematical structure on the major advantage of using a set are supposed to list is that it has hardly optimized method for checking whether a specific element is contained or not in the set. So, for example, if you want to, um, check, even element is in the list. Then you have to traverse all that list until you find that element on that can be haggling efficient if you are dealing with very large lists. But with sets, it is not like that because elements off a said our hash hable, just like in dictionaries. So it's extremely fast to find a new element in asset. So if you want to find an element in a list, then you should consider first converting into a set and then trying to find the element in a set because this is a faster process. Okay, so now let's see this in action. That's great. Our first set from a least from are so famous Fruits least. So we're going to have here Apple, um, like three times a couple apple and apple, watermelon and grapes stewed times. Okay, now we're going to grade fruits said, which you can do it like this just because the irritable to set on. There you go now, Fruit said, he's going to contain apple grapes and watermelon on. As you can see, they have beer just once, and they are in Carly braces, so you can, um, create sets like that with Curly braces. So let's great another for it set, which is going to contain lemon. It's going to contain pineapple mango on a beach. Okay. Ungracious again. Okay, so, um, now we have created, at least So let's say that the type is a said Sorry. We have created sets, so yes, this is a set, and we can also create empty sets. So, for example, we can create it like this on the type. Obviously is going to be empty. Said it's going to be a set. But if you created like this, then you have to be very careful. The gusty is will be a dictionary. So as you can see, the type of the anti set is a dictionary. So if you want to create an empty set, then do it like this. Okay? We can obviously have elements to a set. Because, as I told you, said star mutable. So, for example, to the fruit said, um, we can add a single element, but using the method. Okay, so So both we want, um to add Hey, banana. No, Let's see. Now we have a banana, and if we want to do these operation again, So let's execute the cell again. Then nothing will happen because I remember, sets cannot have duplicate elements. If you want to have more than one element, then just use the update method and you can pass an e trouble like a list so you can pass Banana. Um, you can pass melon on. You can pass graves again. So let's see here. Now, we have, um, the same fruits here, but we added these one here. So, um, this is how you at more than one Element two. That's it. OK, now, let's remove um, some elements of the set. We can use the remove elements. So, for example, if we want to remove banana, um, sorry. Fruits said that removed? No, it should be removed now. We don't have banana here. Um, if we tried to do this again, then we're going to have a key error. Because banana is not present in the dictionary. Hips are in the set. Um, if you want to avoid these exception to be raised, then you should use another method called the discard method. So we seem to save, discard, And if we bans banana and nothing will happen because, um, this card warranties that if the element is not in the said, then it's not going to raise an exception. It's just going to maintain the set unchanged okay on. Well, we can also do some other operations that are also present in lists. For example, in her FRUITS list we can bump is the bulb method, and we could pass the index here. But if we don't pass on index, then we're going to remove the last element, which is grapes. So that's all grapes on now. We didn't have grapes anymore, but with sets remember, they are on order and they are only indexed. So that means that pop is going to remove a random element. So, in fact, let's do it, bub. So as you guys seem, it removed a random element which, in this case, his apple on. We can also, for example, clear these Fred Set. Now it's an empty set on. Well, the separation is also Bressant in. Sorry, I hear bring the deference list for set. So now it's an empty. Now it's an empty set. Okay, so these are some things you can do with sets Now. In the next video, let's see how we can operate with the sets. See you in the next video 32. Operations on sets: I am looking back to learn by thing by doing Number 23. Now we're going to talk about SAT operations. So remember we can do the same basic operations that we can do with mathematical sets because Typhon sets are representations of these mathematical structures. Okay, so remember last time we created our fruits least, which contains apples, watermelon and grapes on, we created a set out of these least on. We graded also another fruit set. We well, I have modified a little bit the elements, So these are the elements off the another fruit set on. Well, if we print for set, we have apple grapes, watermelon and another, Fritz said, has apple grapes, lemon, mango, beach, pineapple and watermelon. Okay, so first we're going to see E. For example, the fruit said he's a subset of the another. Fruits. And so we say. Fruit said that he subset off another fruit set, and these will return True because fruit said half apple grapes, watermelon and another, Erfurt said also has apple grapes and watermelon. So this is a substance. Now let's check if it is a super set. So we call this med method is super set on. It is false because this is not a super set off this, but if we change the rolls so saying another, Fruit said. He's super said, Oh, the fruit said then the result is clearly true because this is obviously a super set off these fruits set. Okay, so now let's go to the union. So this is the Venn diagram for the Union. And let's see, we can use the separator, which is a vertical bar on calling other fruit. Set on these will be the result apple grapes, lemon, mango beach, pineapple and watermelon. But actually we can do the same operation, but using these, um, method, which is simply the method union. Okay, so you know there, Fritz said. Fruit said on We have the same result. Okay, so this is clearer because maybe you don't know or the people who is seeing your code. I don't know what this particle bar means, but they can No, that you're trying to do the union of sets. If you explicitly say, Hey, I'm doing union here, OK, but if we print fruit said then it is unchanged on if we call or bring the another for set another roots set, then it is also unchanged. So these two operations here do not change the original sets. If you want toe change the original set, then you must do the following. You must use these upper this vertical bar with the equals saying another fruit set. I know you can print again the fruit set fruits said on. As you can see now it is updated. So remember we had only apple grapes. Watermelon. Now it has apple graves, lemon, mango, beach, pineapple on water, melon. We can also do these with the open date method. So remember, we have already used these update method and while the result will be the same so it's actually recreate these things here so we can go with the intersection. Okay, so this is a Venn diagram for the intersection on a freight We're going to use fruit said I were going to use this symbol here on. We are going to call the another fruit set and the result is going to be the same because a east a subset or fruit said, is a subset off another friend set. So the intersection will be afraid set again. We can do these also explicitly with the intersection method. So Fruit said intersect. Actually, let's do, um, yeah, for inset the intersection, another fritz it and, well, it's the same result. But again, if we call Fruit set on, let's bring them to say the space on that spring also, another fruit said, then they are unchanged. So if you want to, um, modify the original set, then you have to use again. These with the equal sign another first. But well, it's going to be the same said. So let's, um, maybe let's use another. Let's put here. I don't know, Um, what who I put here banana. So now these result will be the friends. So let's see now we have banana on the intersection. She'll be difference of fruit said It's going, um, to be freed, said watermelon. Apple grapes and watermelons of banana is odd is out because banana is not present in another's any another fruit set. Okay, so you could have done this also with intersection of date, another fruit set on debt. See, Fruit said again, it's apple grapes and watermelon. So banana is out because, um, banana is not present here, so it's not in that dangerous section, OK, now we can do the difference. So, um, I hope you are seeing now the pattern here. So with the difference, we say fruit set minus another fruits set, and it's going to be an empty set. Okay. Why? Because, um well, we updated here, so we have to actually regrade these things here. Okay, So now it will be by Nana here, I think. Yeah. Now, this is going to have only banana because banana is the only element that is not present in another fruits set again. We can do this by simply saying difference with another fruit said the result is going to be banana again. If you want to modify this because this is creating new sets, then you have to use the following thing. You have to say a minus equal. Another fruit set. So now let's check fruit set banana. Or you can simply, um, use difference of date with another for set. Okay. And now let's see. Fruit set. It's still the same result. Banana on the finally we have here the symmetric difference. So let's actually recreate these things here on now. Um, let's see here we can use these upgrader here, Fruit said. Another fruit said, And it's going to return these elements. This is the Venn diagram describe in that operation. But we can also do we symmetric difference. Another fruit said on the gang the same result. And if you want to um, remember, update the first Ted, then you can do something like this. Another front set. So that's enough. Red said contains this. Or you can simply se symmetric the friends of date another fruit set. Okay, so her now these change a little bit, okay? Because we modified fruits said before, but But it's okay. You you can use symmetric difference of date or these one. Okay, so those are the operations you can make with That's on. Well, I hope you like this video. See you in the next one. 33. Frozensets: I am welcome back to learn by thing by during number 24. So we are Ah, going to talk about frozen sets. So basically, person said returns in the mutable frozen's that object initialize with elements from the given it trouble. So remember that I said that that sets are immutable. Well, frozen sets are immutable, just like doubles. Okay, The first and sad is just an immutable version of the pythons set objectives I already set on. While element was that can be modified at any time, elements of a frozen said remained the same after creation. So I wanted to talk about what elements can you put in a set. So that's great simply and empty set being. And he said, remember, this is how we created Um, Let's try to have a dictionary. OK, so let's try to use with these diction air here on it says type error of hush herbal type deep. Why, it's because in said, we cannot would things that are immutable. We cannot add elements that are immutable. Why? Because they are on hash apple. Andi. We cannot find a hash map for mutable things like dictionaries. The same thing applies with dictionaries. So, for example, let's create this empty dictionary empty dictionary. Okay, so this is how you greet an empty dictionary on. Now, let let's try to add a least, um, to the dictionary. So let's say I add 123 and is going to have, um, something like this. So again we get the type er on hash hable type list because we cannot do something like this. 123 ah, five. So I shall have done this. It's easier to understand. Okay, so now it's like that type of air unpassable type least we cannot put a least as a key in a dictionary. Now let's try to add a set. So remember we created sets just like the dictionary. So we're going to put some random numbers here on again. We get the same mirror unhatched herbal type, said we cannot do a set as a key, uh, off a dictionary, because sets are immutable on Gus. They are unhatched hable. Now let's try to, um, again, that's great or empty set. Um, that's tried toe have to this empty set a a set again. So again, we're getting, um, a lot of times this type Aron hash able typeset. So by this time, then you, um, must convince yourself that least dictionaries and sets are unhatched hable. Why? Because they are immutable again so we can create like a hash hable or immutable version of a set. And those are frozen sets. So let's great are frozen set. So we just simply do this on, for example, that spans his list. Now, the Frozen said is like this. I know. Let's try to add that frozen set, and now we don't get any error on our set contains this, Rosen said. Why? Because now, Frozen said, is immutable on Gus. It's hash a ball, and you can put that as also as a key. For example, in the dictionary that's recreated again in the dictionary can take these frozen set us a key. So let's see the content of this. So now, Frozen said, is one of the keys off the dictionary. So this is interesting because now you know that in order to have and are too great an hash , a ble that I'm sorry, a heritable, that destructor, then you need to pass immutable things to the keys off that data structure. OK, so that's everything I wanted to tell you about frozen sets. Hope you like this video on See you in the next one. 34. Files you must have in every Python project: hi and welcome back to learn by thing by doing number 25. So now we're going to, um They're in some more general things on Bees are related to the structure off your project. OK, so these are the four files that you must have in every project will. Actually, the 1st 1 is that a file? It's a folder that contains many files, and it's the virtual environment. So a fertile environment is a way to isolate your project. So, for example, if you have a project that used some dependency, for example, time this, um, pandas recently, um, now release the 1.0 point zero version. So I suppose you want to use Banda's the latest version, But you have installed in your machine some old version, then you have You may have some conflict, So in order to avoid those conflicts, then you should use a virtual environment. Um, the second file is a requirement that txt were us specify all off your dependencies. So every time you do keep installing your machine, maybe you forgot that you install some package. In fact, um, it's a challenge for you to know what packages you installed because maybe it was long a long time ago on doing Remember now. So in order to remember what James told in your virtual environment in this case, you have to put their requirements that txt also the read me that indeed, if you're using marked down, it's very important for other people to know. What are you doing in your project? If there is any configuration, any additional configuration, you must do in order for your project work. So that's very important to specify anger, read me and also the that you'd ignore. So if you are going to use a version control Andi, I tell you, you should use it. Then you should create this while in order to ignore some files like the virtual environment, because the virtual environment contains many, many files and you don't want to commit that. Okay, so now that's great. Um, let's go to the termino. I'm using Lennox here, so let's go to the desktop. And that's create ate a full Dirk will learn, um, urine bison by doing I don't know, um, let's change the factory there on that's open visual studio. Okay, so the first thing here that I'm a I'm going to do is go and create the requirements that 16 the real me dot Andy on the get ignore file he'd ignore. Okay, so here I can use the mark down. I'm going to say learn, I think by doing, and I'm going to a specify some instructions here. So these bad fix is for, um, putting some code and then going to stay here in order to create a veritable environment. We should run the following comment. So we're going to say, by from three minus m then on these is going This is the name of the script that executes and create the virtual environment. On the second argument is the name of the virtual Berman. So usually you call it the same. Um, then to activate the virtual environment, run the following command on us, specify here that the command in Lynn looks at least its source of them being activated. So that's how you activate a virtual environment on to install the tendencies, uh, from requirements that the x team then run the following command. Baby install miners are requirements that txt. OK, so now let's open in your term. You know, I'm Let's do that. Let's say python three miners and then Okay. So as you can see now, these created a van folder on Dhere. We have lots of other folders and files. Andi, that's okay. Okay. Now leads to youth. Uh, get or get version control. Whatever you went to called the subtitle here, then to initialize the get rebel. Then run the following comment on you say here simply did it. So other people that are seeing your goat is going to do this thing here initialized, empty git repository and all these things turned green. Then you have to hear, specify what things you don't want to track. So, for example, you don't want to track these virtual environment now that turned great in their requirements. You can food. For example, As I told you, Found this on. Remember in the read me, they said, uh, first toe, activate the virtual environment like this source been activate on then run these command here even stall Magnus, our requirements that txt. And now this is installing the latest version of bond us. So expand this 1.0 point three. But just in these virtual violent game, um, so Now, this is the basic things that you should always do whenever you want to initialize a project. A fightin project. Um, these are the basic steps, so these are recommended practices that you should follow. Okay, so that's all for these video. I hope you like it on. See you in the next one. 35. Python dotenv: Hi and welcome back to during python by doing number 26. So now we're going to learn about some useful libraries in this game. We're going to talk about the python dot So what does it do? It helps to manage environment variables very easily. So let's see these with an example. I remember that we created these project here on. We had our requirements that texting. So here we're going to include these Bytom that of library under, Remember, in order to install these dependency, then we have to first check if the virtual environment ease active. So here we have these them. So that means it's active on. Then do execute or Rhonda's coming here. So I already installed it before. So that's why it says here that requirement is already satisfied. Okay. Um so here Bendis was ignored because it was raining. Stolt Andi is also Wes Ignore because I installed it before doing this video. But maybe you should see a message that by fund and waas um, successfully installed. Okay, so now here in the looks. If we want to, um, use an environment viable, then we should do something like this. So suppose We're working with an A P I and the A. P. I have has a key. So every time we want to use that as an environment viable, I suppose this is the A B a p i key. We have to do this. So if I execute this command print and to bring the environment variables Andi, catch that a p I key, then it is set. Okay, But it will be conversed cumbersome to the these every time you want to use that environment variable. So that's why people create these files called that envy file. So in this envy, then you can do this A p I key. Andi said the body to whatever it is. Okay, so, um, if you're using, if you are pushing these to a public represent Terry, then maybe usual at this file to the get ignore because it may contain, um, sensible information that you don't want other users to know. Um, but if you are in ah, private repository, maybe it's a good idea to maintain the CNB file. I'm not added to the git ignore. So I say maybe because people steal or the project the architect, the architect of the project. You're working. Maybe saying Well, don't Ari Atoll that file. Okay, so that depends. So I'm going to add it on again. Um, in order to use that environment variable First, we're going to, um, creating you, Father. That is going to pickle main dot p y on here. I'm going to import the o s built in ivory. I'm going to say if name is equal to Maine. Okay, So if name is equal to Maine Uhm, I'm going to print os duds environment dot Get Andi. I'm going to specify that I want to get the a p. I came. So this get is going to return? None if the environment viable does not exist. So now let's run this thing here. So I'm going to say python three Main that anyway, So now it's the value of the environment key. So remember I did this thing here, bring em grip game. So it's the same value from these one. But here the file. I think I change it a bit, so I want to use these one. Okay. So first, let's see how these get works by specifying a UNEP A on environment burble that does not exist. So sorry. So it's returning? None. Because these a p I keys does not exist. Okay, so now let's use the, um Bytom that a library to manage this problem. So I'm going to say, from that f import load, that and here I'm just going to execute or called is a function here, and no, let's see. Um Okay, So let's close this terminal here, and that's great. A new one. Okay, so now it's the new. It's the new A P I key, which is specified here. So you don't have to do this export a p I key and put the value whatever it is now, it's managed automatically when you import the dot low dot f function from these does every library. So I know it's not called the same as what we specified here, but it's because well, we can't put dashes in in the name off these libraries. So that's why when you want to use this biting the death, then you have to import it just like Dr on. Then these, um, loads all of the environment variables that you specify here. So, for example, let's, um I don't know, specify some secret key here on Dhere. Let's sprint that us and grown get ah, secret game and let's again do so now this is this the the FBI key. Now, this is the second secret. So this is a good way to manage your environment. Variables. I recommend you to use it to do it on. I hope you like this video on See you in the next one. 36. Custom exceptions: hi and welcome back to learn python by doing number 27. So today we're going to learn about the very simple but extremely useful Toby, which is custom exceptions on. Well, you can define your own custom exceptions in biting. You just need to create the class that inherits from the exception. Plus on this glass needs to do nothing at all on. Well, the name of the glass is going to be the name off your custom exception. Okay, So, um, to demonstrate this, we're going to create a simple game where we're going to guess around them. Number that we generate first. So first let's dinner that random number, we're going to port. Run them and we're going to go this random number, the correct number. So we're going to call this method ran into, um, so these is going to be a number between one and 10. Okay, Um, no. Um, supposed that winter a number at that number is less than the correct number. Then we're going to raise, um, our own custom exception That is going to say that the number is too small on you. We enter a number that is greater than the correct number that we're going to raise an exception number to be. Okay, so let's do this. That's great. The class number to a small on this is going to inherit from the exception less and these class instead do nothing at all. So just passed on Number two beak. It's going to inherit from the exception class, and again, it's not going to do anything. So just bass. Now we're going to create an infinite while loop, and we are going to prompt the user to enter some number. So, for example, enter your number on this is going to be transformed or cast into an integer. OK, so we're going to check if and he's less done. Three correct number. Then we're going to race these number to a small with a custom error. So first, let's check how these custom exceptions work so we can simply do something like this. Number two's long, Unless you see now it's throwing this exception, and you can pass a message to this. So you're the number. The number you entered east a small on. Now, that custom message, um, appears here, so we're going to copy these on do you need to sell UNB basted here else if and is let is greater than the correct number. Then we're going to raise a number two big exception saying that the number he's too big on else. Um, this is the case where we guess Remember correctly. Then we're going to print you guessed the number. We're going to break from these while loop. Okay, so we have to catch three exceptions here. The 1st 1 is the number two small. We can get it as e. So this is something people do on E is simply the message. So this thing here Onda, we can do the same with number too big. We're going to catch it us e and bring that message to be more specific. We're going to say e means error. Okay, Andi, last exception we have to catch is when we tried to enter a string That is not a number. So, for example, if I do something like this input, then I enter like hello, and then I tried to do in s. Then it's going to raise these value error because I'm trying to convert these two a number . So this is going to also be, um, and judged. So the value error is going to be catched us error, and I'm going to bring ever Well, I'm not going to bring these error here because it's not that clear. So simply, I'm just going to say you did not enter a number. Okay, so that's all. And now let's play our game. So accept number two small. Okay, So I I forgot to put the try here. And this is this needs to be indented. Okay, Enter your number. Well, I'm going to put four. It says that the number you enter is too small. So I'm going to put five. It's still too small. 62 small. 789 So the random integer was nine on then it's as you guess, the number. So, um, this is how you define your own custom exceptions. I hope you understood the importance of it and see you in the next video. 37. Unit testing with pytest: Hi and welcome back to learn python by doing number 28. So today we're going to talk about a library Cold bite ist, which is extremely useful for unit testing. So what is testing? So there ist this concept called test driven development or TDD for short, That is extremely used useful nowadays on extremely used also. So, um, there are lots and lots of articles out there that talk about TDD. Maybe, um, some of them are better than these, but I'm going to summarize what TDD is all about. So before writing a coat before writing any kind of gold, you have to first, um, right tests for that coat. So the first step is to write a failing test. Then the second step is right. The minimum code in order for thes test to pass. Then you can improve your code or re factory, people say, But if in this process off re factoring, your test fails, then you have to make the test pass again on the cycle goes on and on. Okay, so for me, um, writing. I'm sorry. Teaching TDD is a little bit difficult because their benefits off led are not seeing in small products. They are tedious for small projects. But believe me, for huge projects, if juden half TDD, then that project will fail. Why? Because, um, when you're a factor code in a huge grub in a huge project, it's extremely dangerous because you can break other parts of the coat. But if you're factor and then a test is saying, Hey, something went wrong, then, um, you are guaranteed that you have to do something for, um, for making other parts of the code not to break. Okay, so that's why um it's difficult of this for me to teach timidity, but let's do it. Okay, so remember our project where we had this files. Now, in the requirement that txt we have to include the spite ist on off course, you have to have your virtual environment activated on. You have to run this command having that environment activated. Okay, so I already installed it before. That's why it says here requirement already satisfied. But in your case, if it's the first time that it will, it will be installed successfully. Okay? No, um, we can execute these command here. Biased on. It's going to say, um that there are no tests. Also, despite this cash God created Andi, that's one off those things we don't want in our project. So that's why I put that in these beating. Nor also, when we run our first test, these folder is going to be created. So that's why I also put this folder in the git. Ignore. Okay, so now let's write, actually our first guest. But before doing that, we have to know I am a key word from bye bye thing, which is assert. So assert. East, Um, does nothing Eve the right the left hand side is equal to the right hand side. For example, if I say assert falls is equal to false, then it's not going to do nothing. If I say us, our true is equal to true. Then again, it's not going to do nothing. If I say three is equal to want less, too. Then again, nothing. But if I say that two plus two is equal to five, then it's going to throw an assertion error. Okay, so let's first create a file. So all tests need to be, um the name needs to start with test, so we're going to say test underscore. Capitalize Because we're going to write a function that capitalizes strings. Okay, so first we have to import by just because we're going to use it. Then we have to create our test. Our unit test. So all unit tests begin. Well, our functions that begin with the word test. So it's going to be called test. Andi, I'm going to call this capital case. Okay, Now, let's assert the following thing. Let's assert the capital case of, um, David is equal to David. So now let's execute by this again. Okay, So our test obviously failed notice that this by cash got created. So that's why I put it in the getting nor on. Well, we have thes part off duty. Now we have to make the best pass we have toe right, the minimum code in order for these test toe bus. So we're going toe. Obviously implement the capital case function which is going to us up. Receive a string, and it's going toe capitalize these a string. So it's going to return the string, but capitalized. So now let's run by this again. And as you can see, one at best, past. Okay, so we have done these here. Okay? No, let's, um, re factor our code. But first, let's make another test here. So this test is going to be well, it has to start with test and well, you can put whatever you want, so raises exception ive um not a string argument. Okay, so now we're going. Going to use the bike. That by distance. Terry. So we're going to say with by dist the Racists. Taib Error. We're going to, um, passed some number here. So for example one. Okay, so no, um, let's run by this again and see the result. Ondas You can see one test failed and one past. So what is the They're here that we have to check first. If, um what is being bussed East Eastern. So here we can say if not is instance a string of str Then we have to raise this type error saying, um, the argument must be a strength. Okay, Okay. So now we have re factor are coat. And now let's check if brightest um Okay. So to, uh, guests asked why? Because here where we are expecting a type error if we passing number. So that's what is happening here. Is checking if this is an instance of a string? If it's not, then these function is going to rise race or this type error on these with biters that raises diaper checks. If in fact, we are raising that exception. So that's any introduction to TDD. I hope you like this video on. Keep in mind that this is very useful. Was your project starts growing? I'm growing. Okay, See you in the next view. 38. Fixtures: hi and welcome back to learn by thing by doing number 29. So today we're going to learn more about unit testing on. We're going to learn about test pictures. So death pictures initialized best functions. They provide a fixed base line. So the test execute reliably unproduced, consistent, repeatable results in solicitation may set up services, date or other operating environments. So let's see what all of these is about. Remember, we had our project here on. Now we are going to great in your file that is going to be cold. Remember, it has to start with test and then whatever you want, we're going to great a bank account class, and we're going to test it before implementing it. So, off course, we need to import by this and also from bank account, we're going to import the bank account, um glass and also a custom exception that is going to be gold insufficient amount. So we're going to raise this exception when we try to spend money. We don't tough, but first, let's great the pictures. So we're going to use these, the creator here, and then we're going to define these function that is going to be called empty bank account . And here, Well, we cannot Some documentation. It's, um, for for unit testing. It's very important what documentation. And we're going to say that these returns a bank account with a balance off zero. So with no money return bank account on like that. Now we're going to define another fixture, which is going to be called none empty bank account. Andi again, some documentation returns a bank account were the Ballas of 100 and return bank account. And in the constructor we're going, we're going to pass to the constructor 100 which is going to be the initial amount off money. So now we can use, um these ah, objects off the bank account glass in all off our tests. So the first test is going to be cold. Test the fold initial amount and it's going to receive these, um, empty bank account and we're simply going to assert, if the balance off these bank account is zero now, we're going to test setting initial amount on again. We are going to, um, received the empty bank account and, um, sorry, not the empty your bank account, but the non empty back a bank account. And we're going to assert if the no empty bank account but bank account balance is equal to 100. This is our second best. Now are our third test is going to be cold. Best, um, have money. Okay, on here. We're going to, for example, receive the non empty bank account. Okay, so if we adhere, um, let's have I don't know, the method is going to be called at cash or deposits on. We're going to the bus. It another $100 and we're going toe assert now is if the non empty bank account balance is equal to 200 because, remember, it has, um, $100 initially. Now we're adding $100 more. Now it needs to have $200. Okay, Now, the fourth test is going to be, um, test. It's bent. Gosh, and again is going to receive a new instance of these non empty bank account on Dhere. We're going to withdraw. So that's how the method is going to be called are going to withdraw $50 on. We're going to assert if the balance is now 50. Andi Lastly, we are going to test, uh, insufficient is sufficient amount. Oh, account. And we're going here to pass the empty bank account. And remember these with I just that racist Insufficient, Not in suficiente amount. So we're going to check eve of these insufficient amount. Exception is raised when we try to withdraw money from this, um, empty account. So if I tried to withdraw $100 then these needs to raise thes insufficient amount exception . Okay, so now if we're trying to do by this, obviously, um, we are not We're going to fail because we don't even have these bank account. Um, Glass implemented. So let's do that. No. So bank account that b y. Now we have to write the minimum code in order for these tests to pass. So the first thing that we're going to do is remember, um, creator custom exception in sufficient amount, which is going to inherit from the exception class on. It's not going to do anything. So we have already learned this How the great custom exceptions. So that's easy. And now let's great the bank account class. Okay, so we don't need this, you constructor. So the initial amount that is going to be defaulted by zero. So if you don't pass anything when creating an object of this class, then it's going to be the empty A bank account like we had in our fixture on. Then we're going to say self balance is going to be initial amount. Okay, so now we have grated these balance property, and now we have to, um, defined this method Withdraw. Okay. So, self on the amount withdraw, We're going to say itself. That balance is less than the amount we want to withdraw. They will have to raise these insufficient amount exception. Um, else if we don't raise anything, then we're going to simply say abstract the amount from the balance on. Finally, we're going to define the deposit method, which is going to also receive an amount on Well, here, we don't have any restrictions to the money. So simply, we're going to at these amount to the balance. So we have, um we have created the minimum gold in order for these tests to pass. So I hope I did that. Let's see, with brightest. Okay, so seven, um ah. Desk this past Where? Well, well, here they are included. That one's from the last video. But if we ignore them, then we made these five best best. So now you can reflect her. This gold, if you want, for example, to make the balance a property on make setters and getters. Well, in this case, centers and getters that the center, it will be the boss. It, um, the getter will. It can be a method that displays the amount of money, the current amount of money you have. So in short, these fixtures help us to create objects. Um, new objects for each off these tests that needs it. We can also hear saved by this mines minus fixtures on here. Um, you have the description of each of the pictures you created. So that's why I didn't These documentation was important. Because when you execute dis command, then you are having these message here. So that's all for fixtures. Hope you like this video and see you in the next one 39. Parametrized tests: hi and welcome back to learn by thing by doing number 30. So today we're going to talk about Burma. Tryst tests. So there is a special decorator, which is by this that marked the Burma tries, which allows us to pass several cases to just one test. So remember, in networks our example here, where we had a bank account, we're going to digest combined tracks at transactions. So the first operation will be a deposit, and the second operation will be with gravel. So let's start by, um, creating these decorator Mark that Bram It Rice. Andi, you can pass also arguments to the decorator. So here, we're going to bus a strain, which is going to be amount, um, to deposit. The second thing is going to be amount spent on the third part off the string will be expected. So this is a string, which is you can see separated by commas aan den. This is the first argument. The second argument is going to be a least off doubles. So, for example, in the first case, we're going to the pass it $50. Then we are going to, um, spent $10 so way expect that after these two transactions are done, we are left with $40. Um, in the second trouble, we can, for example, deposit $100 on, then spend $60 on again. Let's change the 60 with 70 on after the doing of the positive 100 on withdrawing 17. Then we're expecting to be left with $30. Okay, so this is our decorator. No, Uh, these degrades these desk transactions on the first argument will be the fixture. So remember, we had these two fixtures here. I'm going to use the empty bank account with an initial amount or balance off. Zero on, uh, the next arguments will be the same as these ones. So amount of the boss it the amount of spent on the expected value after these two operations are done. So empty bank account, we have to call the deposit method. We amount to the bus it The second operation will be with throw with the amount spent. And finally, we want to assert Eve on the balance off. This thing is equal to the expected balance after we do both of these operations. So let's seem okay. So nine tests past we gun good. Any amount off cases here? So now let's suppose we have 54 on. We spend $4 on. We are expected to be left with $50. So now I have 10 tests because these one, um, is a new test is considered a new desk on. Well, it passed because it's correct. So this is how you can test several cases of the same test on It's very, very, very useful. How would you like this video and see you in the next one?