# Python Class Assignment Operator Definition

Operators are used to perform operations on values and variables. Operators can manipulate individual items and returns a result. The data items are referred as operands or arguments. Operators are either represented by keywords or special characters. For example, for identity operators we use keyword "is" and "is not".

In this tutorial, we going to learn various operators

## Arithmetic Operators

Arithmetic Operators perform various arithmetic calculations like addition, subtraction, multiplication, division, %modulus, exponent, etc. There are various methods for arithmetic calculation in Python like you can use the eval function, declare variable & calculate, or call functions.

**Example**: For arithmetic operators we will take simple example of addition where we will add two-digit 4+5=9

Similarly, you can use other arithmetic operators like for multiplication(*), division (/), substraction (-), etc.

## Comparison Operators

These operators compare the values on either side of the operand and determine the relation between them. It is also referred as relational operators. Various comparison operators are ( ==, != , <>, >,<=, etc)

**Example**: For comparison operators we will compare the value of x to the value of y and print the result in true or false. Here in example, our value of x = 4 which is smaller than y = 5, so when we print the value as x>y, it actually compares the value of x to y and since it is not correct, it returns false.

Likewise, you can try other comparison operators (x < y, x==y, x!=y, etc.)

## Python Assignment Operators

Python assignment operators are used for assigning the value of the right operand to the left operand. Various assignment operators used in Python are (+=, - = , *=, /= , etc.)

**Example**: Python assignment operators is simply to assign the value, for example

**Example of compound assignment operator**

We can also use a compound assignment operator, where you can add, subtract, multiply right operand to left and assign addition (or any other arithmetic function) to the left operand.

- Step 1: Assign value to num1 and num2
- Step 2: Add value of num1 and num2 (4+5=9)
- Step 3: To this result add num1 to the output of Step 2 ( 9+4)
- Step 4: It will print the final result as 13

## Logical Operators

Logical operators in Python are used for conditional statements are true or false. Logical operators in Python are AND, OR and NOT. For logical operators following condition are applied.

- For AND operator – It returns TRUE if both the operands (right side and left side) are true
- For OR operator- It returns TRUE if either of the operand (right side or left side) is true
- For NOT operator- returns TRUE if operand is false

**Example**: Here in example we get true or false based on the value of a and b

## Membership Operators

These operators test for membership in a sequence such as lists, strings or tuples. There are two membership operators that are used in Python. (in, not in). It gives the result based on the variable present in specified sequence or string

**Example**: For example here we check whether the value of x=4 and value of y=8 is available in list or not, by using **in** and **not in **operators.

- Declare the value for x and y
- Declare the value of list
- Use the "in" operator in code with if statement to check the value of x existing in the list and print the result accordingly
- Use the "not in" operator in code with if statement to check the value of y exist in the list and print the result accordingly
- Run the code- When the code run it gives the desired output

## Identity Operators

To compare the memory location of two objects, Identity Operators are used. The two identify operators used in Python are (is, is not).

- Operator is: It returns true if two variables point the same object and false otherwise
- Operator is not: It returns false if two variables point the same object and true otherwise

Following operands are in decreasing order of precedence.

Operators in the same box evaluate left to right

Operators (Decreasing order of precedence) | Meaning |
---|---|

** | Exponent |

*, /, //, % | Multiplication, Division, Floor division, Modulus |

+, - | Addition, Subtraction |

<= < > >= | Comparison operators |

= %= /= //= -= += *= **= | Assignment Operators |

is is not | Identity operators |

in not in | Membership operators |

not or and | Logical operators |

**Example**:

- Declare the value for variable x and y
- Use the operator "is" in code to check if value of x is same as y
- Next we use the operator "is not" in code if value of x is not same as y
- Run the code- The output of the result is as expected

## Operator precedence

The operator precedence determines which operators need to be evaluated first. To avoid ambiguity in values, precedence operators are necessary. Just like in normal multiplication method, multiplication has a higher precedence than addition. For example in 3+ 4*5, the answer is 23, to change the order of precedence we use a square bracket (3+4)*5, now the answer is 35. Precedence operator used in Python are (unary + - ~, **, * / %, + - , &) etc.

v = 4 w = 5 x = 8 y = 2 z = 0 z = (v+w) * x / y; print("Value of (v+w) * x/ y is ", z)- Declare the value of variable v,w…z
- Now apply the formula and run the code
- The code will execute and calculate the variable with higher precedence and will give the output

### Python 2 Example

Above examples are Python 3 codes, if you want to use Python 2, please consider following codes

#Arithmetic Operators x= 4 y= 5 print x + y #Comparison Operators x = 4 y = 5 print('x > y is',x>y) #Assignment Operators num1 = 4 num2 = 5 print ("Line 1 - Value of num1 : ", num1) print ("Line 2 - Value of num2 : ", num2) #compound assignment operator num1 = 4 num2 = 5 res = num1 + num2 res += num1 print ("Line 1 - Result of + is ", res) #Logical Operators a = True b = False print('a and b is',a and b) print('a or b is',a or b) print('not a is',not a) #Membership Operators x = 4 y = 8 list = [1, 2, 3, 4, 5 ]; if ( x in list ): print "Line 1 - x is available in the given list" else: print "Line 1 - x is not available in the given list" if ( y not in list ): print "Line 2 - y is not available in the given list" else: print "Line 2 - y is available in the given list" #Identity Operators x = 20 y = 20 if ( x is y ): print "x & y SAME identity" y=30 if ( x is not y ): print "x & y have DIFFERENT identity" #Operator precedence v = 4 w = 5 x = 8 y = 2 z = 0 z = (v+w) * x / y; print "Value of (v+w) * x/ y is ", z### Summary:

Operators in a programming language are used to perform various operations on values and variables. In Python, you can use operators like

- There are various methods for arithmetic calculation in Python as you can use the eval function, declare variable & calculate, or call functions
- Comparison operators often referred as relational operators are used to compare the values on either side of them and determine the relation between them
- Python assignment operators are simply to assign the value to variable
- Python also allows you to use a compound assignment operator, in a complicated arithmetic calculation, where you can assign the result of one operand to the other
- For AND operator – It returns TRUE if both the operands (right side and left side) are true
- For OR operator- It returns TRUE if either of the operand (right side or left side) is true
- For NOT operator- returns TRUE if operand is false
- There are two membership operators that are used in Python. (in, not in).
- It gives the result based on the variable present in specified sequence or string
- The two identify operators used in Python are (is, is not)
- It returns true if two variables point the same object and false otherwise
Precedence operator can be useful when you have to set priority for which calculation need to be done first in a complex calculation.

## What are operators in python?

Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand.

For example:

Here, is the operator that performs addition. and are the operands and is the output of the operation.

## Arithmetic operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication etc.

Operator | Meaning | Example |
---|---|---|

+ | Add two operands or unary plus | x + y +2 |

- | Subtract right operand from the left or unary minus | x - y -2 |

* | Multiply two operands | x * y |

/ | Divide left operand by the right one (always results into float) | x / y |

% | Modulus - remainder of the division of left operand by the right | x % y (remainder of x/y) |

// | Floor division - division that results into whole number adjusted to the left in the number line | x // y |

** | Exponent - left operand raised to the power of right | x**y (x to the power y) |

### Example #1: Arithmetic operators in Python

When you run the program, the output will be:

## Comparison operators

Comparison operators are used to compare values. It either returns or according to the condition.

Operator | Meaning | Example |
---|---|---|

> | Greater that - True if left operand is greater than the right | x > y |

< | Less that - True if left operand is less than the right | x < y |

== | Equal to - True if both operands are equal | x == y |

!= | Not equal to - True if operands are not equal | x != y |

>= | Greater than or equal to - True if left operand is greater than or equal to the right | x >= y |

<= | Less than or equal to - True if left operand is less than or equal to the right | x <= y |

### Example #2: Comparison operators in Python

## Logical operators

Logical operators are the , , operators.

Operator | Meaning | Example |
---|---|---|

and | True if both the operands are true | x and y |

or | True if either of the operands is true | x or y |

not | True if operand is false (complements the operand) | not x |

### Example #3: Logical Operators in Python

Here is the truth table for these operators.

## Bitwise operators

Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit, hence the name.

For example, 2 is in binary and 7 is .

**In the table below:** Let = 10 ( in binary) and = 4 ( in binary)

Operator | Meaning | Example |
---|---|---|

& | Bitwise AND | x& y = 0 () |

| | Bitwise OR | x | y = 14 () |

~ | Bitwise NOT | ~x = -11 () |

^ | Bitwise XOR | x ^ y = 14 () |

>> | Bitwise right shift | x>> 2 = 2 () |

<< | Bitwise left shift | x<< 2 = 40 () |

## Assignment operators

Assignment operators are used in Python to assign values to variables.

is a simple assignment operator that assigns the value 5 on the right to the variable on the left.

There are various compound operators in Python like that adds to the variable and later assigns the same. It is equivalent to .

Operator | Example | Equivatent to |
---|---|---|

= | x = 5 | x = 5 |

+= | x += 5 | x = x + 5 |

-= | x -= 5 | x = x - 5 |

*= | x *= 5 | x = x * 5 |

/= | x /= 5 | x = x / 5 |

%= | x %= 5 | x = x % 5 |

//= | x //= 5 | x = x // 5 |

**= | x **= 5 | x = x ** 5 |

&= | x &= 5 | x = x & 5 |

|= | x |= 5 | x = x | 5 |

^= | x ^= 5 | x = x ^ 5 |

>>= | x >>= 5 | x = x >> 5 |

<<= | x <<= 5 | x = x << 5 |

## ]Special operators

Python language offers some special type of operators like the identity operator or the membership operator. They are described below with examples.

### Identity operators

and are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal does not imply that they are identical.

Operator | Meaning | Example |
---|---|---|

is | True if the operands are identical (refer to the same object) | x is True |

is not | True if the operands are not identical (do not refer to the same object) | x is not True |

### Example #4: Identity operators in Python

Here, we see that and are integers of same values, so they are equal as well as identical. Same is the case with and (strings).

But and are list. They are equal but not identical. Since list are mutable (can be changed), interpreter locates them separately in memory although they are equal.

### Membership operators

and are the membership operators in Python. They are used to test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary).

In a dictionary we can only test for presence of key, not the value.

Operator | Meaning | Example |
---|---|---|

in | True if value/variable is found in the sequence | 5 in x |

not in | True if value/variable is not found in the sequence | 5 not in x |

### Example #5: Membership operators in Python

Here, is in but is not present in (remember, Python is case sensitive). Similary, is key and is the value in dictionary . Hence, returns .