filmov
tv
The Python Power Operator (Exponentiation in Python)

Показать описание
Welcome back to our chapter on expressions and operators in Python. In this video, we will take a look at expressions that make use of the power operator. The power operator makes use of 2 asterisks. It allows us to perform exponentiation operations.
So let's take a look at some examples. Here, we have two variables, x is equal to 2 and y is equal to 3.
The first example makes use of 2 literal integer values. 2 and 5. So we read this as 2 raised to the power of 5. And the result here will be 32. Notice that in this example, as well as the other ones, that we are passing an expression to a function call. We're calling the print function, and then inside the parentheses, we have the expression 2 raised to 5. When passing expressions to function calls, the expressions are evaluated first before the function proceeds. So here, the expression will first evaluate into the value 32, and then the print function gets executed after that.
The second example is almost the same, except the first value is a float instead of an integer. We have 2 point zero raised to the power of 5. In this case, the answer is going to be 32 point zero. When using the power operator, if one value is an integer and the other one is a float, whether it's the base or the exponent, then the result will be a floating point number.
In the third example we have x raised to the power of y. Instead of literal values, this expression contains variables. When variables are used in expressions, the variables will evaluate into their respective values first, before the operation is carried out. So in this case, x will evaluate to 2, and y will evaluate to 3, and then 2 will be raised to the power of 3, resulting in a value of 8.
The fourth example shows that you can mix variables and literals in the same expression. Here we have the variable x, which evaluates to 2. And here we have the literal numeric value 4. And then 2 raised to 4 will give us 16.
In this next example, the expression starts with a negative sign, and is then followed by 3 raised to the power of 2. So what will be the result here? Will it be -9 or positive 9? In this case, the expected output is -9. Python will first perform the exponentiation, and then only after that will the negation be applied. So this one is going to first perform 3 raised to the power of 2, and then it will negate the result. To make the order of execution a bit more clear, it would be just like placing the exponentiation expression inside parentheses and the negative sign outside. Both of these will give us the same result.
But what if we actually wanted the negation to happen first? Well in that case, we can place -3 inside parentheses.This will result in the negation happening before the exponentiation. So now what we're saying here is, -3 raised to the power of 2. Or in other words -3 times -3. So the result here will be 9.
Now in this final example, we have 2 raised to 3 raised to 2. So how does this one get evaluated? With successive exponentiation operations, we go from right to left. So we actually evaluate 3 to the power of 2 first, which results in 9. And then we do 2 to the power of 9, which will give us a final result of five hundred and 12.
And that's it for this video on the power operator. But in the next ones, we will be taking a look at more types of expressions and the different operators that they use.
#python #learntocode
So let's take a look at some examples. Here, we have two variables, x is equal to 2 and y is equal to 3.
The first example makes use of 2 literal integer values. 2 and 5. So we read this as 2 raised to the power of 5. And the result here will be 32. Notice that in this example, as well as the other ones, that we are passing an expression to a function call. We're calling the print function, and then inside the parentheses, we have the expression 2 raised to 5. When passing expressions to function calls, the expressions are evaluated first before the function proceeds. So here, the expression will first evaluate into the value 32, and then the print function gets executed after that.
The second example is almost the same, except the first value is a float instead of an integer. We have 2 point zero raised to the power of 5. In this case, the answer is going to be 32 point zero. When using the power operator, if one value is an integer and the other one is a float, whether it's the base or the exponent, then the result will be a floating point number.
In the third example we have x raised to the power of y. Instead of literal values, this expression contains variables. When variables are used in expressions, the variables will evaluate into their respective values first, before the operation is carried out. So in this case, x will evaluate to 2, and y will evaluate to 3, and then 2 will be raised to the power of 3, resulting in a value of 8.
The fourth example shows that you can mix variables and literals in the same expression. Here we have the variable x, which evaluates to 2. And here we have the literal numeric value 4. And then 2 raised to 4 will give us 16.
In this next example, the expression starts with a negative sign, and is then followed by 3 raised to the power of 2. So what will be the result here? Will it be -9 or positive 9? In this case, the expected output is -9. Python will first perform the exponentiation, and then only after that will the negation be applied. So this one is going to first perform 3 raised to the power of 2, and then it will negate the result. To make the order of execution a bit more clear, it would be just like placing the exponentiation expression inside parentheses and the negative sign outside. Both of these will give us the same result.
But what if we actually wanted the negation to happen first? Well in that case, we can place -3 inside parentheses.This will result in the negation happening before the exponentiation. So now what we're saying here is, -3 raised to the power of 2. Or in other words -3 times -3. So the result here will be 9.
Now in this final example, we have 2 raised to 3 raised to 2. So how does this one get evaluated? With successive exponentiation operations, we go from right to left. So we actually evaluate 3 to the power of 2 first, which results in 9. And then we do 2 to the power of 9, which will give us a final result of five hundred and 12.
And that's it for this video on the power operator. But in the next ones, we will be taking a look at more types of expressions and the different operators that they use.
#python #learntocode
Комментарии