Загрузка страницы

The Python Unary Arithmetic Operators

In this video, we're going to take a look at the type of operator known as the unary arithmetic operator. But first let's go ahead and define what a unary operation is. A unary operation is an operation that involves only 1 operand or value. An example of what is not a unary operation is 1 + 1, because in this case, the operation involves two values. With unary operations, only one value is involved.

In Python, there are 2 unary arithmetic operators. We have the unary minus operator, which we can also describe as the negative operator. This operator yields the negation of its numeric operand. Take note that negation doesn't always result in a negative value. If we negate a positive number, then it becomes a negative number. However, if we negate a negative number, then it becomes a positive number.

And then there is also the unary plus operator, which we can also describe as the positive operator. This operator yields its numeric operand unchanged. In other words, it isn't going to do anything. The number is going to stay the same. +9, for example, is just the same as 9. So I know that sounds strange. Why is it even an option, if it doesn't affect the value? But there are actually some special use cases for this, however we will not be covering those in this module. And in this video, we will only be talking about the unary minus operator.

Ok so now let's take a look at some examples here. We've got a variable x that is equal to 5 and a variable y that is equal to -3.

And then this first example uses the unary minus operator on the floating point value 1.0. So this one takes the value 1.0, and then negates it. So the output here would simply be negative 1 point zero.

This next one says negative x. So currently, the value of x is 5, so if we negate it, then the result is negative 5. But take note that this line is only printing out the value of the expression negative x. Nowhere in this line are we actually updating the value of x.

So when we get to this next line, the value of x will still be 5, therefore, the output will still be 5. If we wanted to actually update x with the negated value, then we would need to explicitly assign it. So we would say x equals negative x.

What we have here is an assignment statement that has an expression on the right hand side of the equals sign, which is also using the same variable named x. If you have something like this, the expression on the right hand side gets evaluated first, before the resulting value gets assigned back to the variable.

So what happens here is, it takes the current value of x, which is positive 5, and then negates it. So this results in -5. After that, it then gets assigned back to the variable x, thereby replacing the previous value. So if we print x again, this new print statement will now output negative 5.

But take note that this previous print x statement will still output 5, because we're only updating the value of x right after it.

And finally we have negative y. The current value of y is -3. So if we apply the negative operator to it, then it becomes positive. So the output here will be 3.

Let's go ahead and run this, and here we see the expected output.

#learntocode #python

Видео The Python Unary Arithmetic Operators канала choobtorials
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
27 августа 2021 г. 13:43:52
00:04:36
Яндекс.Метрика