1.7 #xpression and their evaluation in Python | Python Full course 2024 #learnpythonin2024

Akansha Yadav covers Python expressions, operator types (arithmetic, logical, comparison, Boolean), and how Python evaluates them using PEMDAS/BODMAS order. Live examples show step-by-step how parentheses, exponents, multiplication, division, addition, and subtraction resolve.
Chapters
Ladies and gentlemen, in this video we will discuss about the expressions and its evaluation. So we will see how our Python evaluates any operations in Python, like how actually it is being executed. So let's first discuss what is an expression. Expression is basically, we can say in mathematics, that we call equation. That is same. So expression is equal to equation. Basically, it is a combination of, uh, in Python, it is a combination of variables, values, and operators. Operators, as we discussed already, are There are multiple types of operators in Python, like arithmetic operator, logical operator, comparison operator, Boolean values. All these are the operators. Values— values are also like— values are equal to operands. You will find some words, operands, it means values. Suppose I am writing an equation, 50 60. So here, 50 and 60 are Values, which is— which we can also say as an operand. And plus over here is operator. And when we combine all these things, we call it expression. You can even store it in a variable. So when you are going to store it, let's suppose sum is equal to— I'm writing 50 60. So here I have a combination of a variable, operator, assignment operator, and some values, it is called expression. So once this thing is clear to you, let's move on to the concept, that is how it evaluates it. What is the scenario in which it is going to evaluate your expression, your equation in Python? So there are two things which you have learned in your mathematical thing, like in your schools as well. That there are two things. Like, one is P-E-M-D-A-S, another one is the B-O-D-M-A-S. So these two are the scenarios in which our equation, our expression, will be evaluated in Python. Expression will be evaluated on the basis of any of these things. Any of the scenario can be followed in Python for evaluating expressions. So let's discuss them one by one. What is P, E? P, E, M, D, A, and S. So P is basically a parenthesis The next that we have is E, which is exponent. Then next we have is M, which is multiplication. Then we have D, which stands for division. Then we have A, which stands for addition. And then we have S, which stands for subtraction. Parenthesis, exponent, multiplication, division, addition, and subtraction. This is a scenario in which our Python equation will be evaluated. In case there is a parenthesis in the equation, so that will be the high priority. It means that parenthesis will be solved first rather than going on to the addition, subtraction, division, or multiplication. Then moving on to the exponent, it means if there is any power of something like x to the power of y or 5 to the power of 3, like this, if there is an equation, then that will be evaluated at the second priority. Then the preference will be given to the multiplication. If there is any value which is under multiplication equation, then that will be evaluated. Moving ahead with the division, addition, and then this subtraction. Another scenario is the board mask. The next scenario on which Python evaluates the equation is B-O-D-M-A-S. So either it is a PMDS or BODMAS, the lowest priority is given to the subtraction only. In case of B, it is brackets. So in case of parenthesis, you can call it a bracket as well. So Both the scenarios are the same. In case of exponent, in this BOD, it is order. Then D for division, M for multiplication, then addition. And the last one is the subtraction. So either of the scenario can be selected by Python, and the equation will be evaluated. Let's take an example of each and everything like parentheses, exponent, division, floor division, and other things. So I'm going to write a code. Result 1 is equal to pi plus 3 multiplied by 2. Oh, need to select the kernel. Yeah, it's done. So the equation has been evaluated. So let's write down the results. So our result is 16. It means it just solved the parenthesis first, which is 5 3, then it evaluated the multiplication. Let's try some other examples. Now it's going to be result2 variable. Now x to the power of 3. Result 2. So it's normal because exponent is there, so it jumps on to the second one, which is the exponent. So if there is any big expression using brackets and all, let's suppose 5 3 multiplied by 2 in brackets to the power of 5, again in brackets, minus 50. This is just equation with the combination of all these. Now I'm going to divide it, divided by 2. So this is one of the equation where I'm going to store it as an equation variable. Let's see what is going to be the output of this. So we got our output. It evaluates one by one the parenthesis, then the exponents and the multiplication, then the division in the end, because the parenthesis is the first priority. So this is how our expressions are evaluated in Python. So once this concept is clear to you, Try to explore more the concepts of expression using different, different values and different, different operators in combination.










