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

 In Python, arithmetic operators are used to execute common mathematical operations. The table below lists Python's arithmetic operators, along with their symbols and meanings. 



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

 When comparing two values, comparison operators are employed. Comparison operators are commonly employed in Boolean settings since they yield either true or false. They are particularly useful in conditional and loop statements to guide program flow.

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.

 


 Logical Operators

 To compare two conditional statements, the logical operators "and," "or," and "not" are employed. To build more complicated conditions, logical operators change and link together phrases evaluated in a Boolean context. When the "and" operator is used, the expression will return true if both operands are true. Using the "or" operator, on the other hand, will result in true if at least one of the operands is true. Finally, the "not" operator returns true only if the operand's Boolean expression is false.

 


Identity Operators

 Identity operators "is" and "is not" are used to compare two objects. Not if they are equal but if they are actually the same object located on the same part of the memory. Hence, two equal objects do not necessarily imply that they are identical, with the same identity. 


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.

 When you type x = z, Python just generates a second reference to the same object. As a result, when you test x is z, it returns true. x and z both share the same object identification. 


Membership Operators

 The membership operators "in" and "not in" are used to determine whether or not a value or variable exists in a sequence. A sequence might be a string, a list, a tuple, a set, or a dictionary. Membership operators, like identity operators, yield Boolean values.

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

 Bitwise operators are similar to logical operators in that they treat operands as binary values and perform bit-by-bit operations on them.

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.