3.3 Operators
Introduction
Operators
are tools that allow you to manipulate variables and values. The most
fundamental operations are addition and subtraction.
>>>
x + 5
The values
that an operator acts on are called operands. In the above example, + is
the operator. X, a variable, is the first operand, while 5, a value, is the
second operand
Arithmetics
Operators
The fundamental
arithmetic operators are addition, subtraction, and multiplication. Then there
are two divisions: standard division and floor division. A float is always
produced by standard division. In the Floor Division, the fractional component
of the result is trimmed, leaving just the integer part.
Assignment
Operators
In Python,
assignment operators are used to assign values to variables or any other
object.
For example,
x = 5 is a straightforward assignment operator that assigns the value 5 to the
variable x on the left. There are also other compound operators, as seen in
this table. x += 2 multiplies the variable x by 2 and assigns the result to x.
This is the same as x = x + 2. And the value of x will now be 7.
You can go
through the below table to learn about all other compound operators in Python.
Comparison
Operators
As
comparison operators, we have greater than, less than, equal to, not equal to,
greater than or equal to, and less than or equal to. The outcome, as seen in
the example, is a Boolean value. 5 larger than 2 returns true, while 4 less
than 4 returns false.
Identity
Operators
Both x and y refer to lists with the same elements. They are equivalent but not identical since they do not relate to the same memory item. As a result, x is y yields false. Using the id() method, you can ensure that x and y do not refer to the same object.
Membership Operators
Here's an
example of a list with "a" and "b" in it. The assertion
"a" in list is true. The line "c" not in list also yields
true since "c" is not a list entry.
Bitwise
Operators
For example,
the binary value of 5 is 101, whereas the binary value of 7 is 111. Because you
performed a logical operation on each bit of the integers one at a time, the
"and" operation on these operands yields the result 5 (101 in binary
form). This is exactly the same as doing the "or" and "xor"
operations bit by bit, but using the proper logical operation. XOR will return
true if just one bit is true, not both.
We also have
operators on the right and left shifts. A binary value is shifted to the left
by pushing zeros.







0 Comments