# Operators

##### Operator
Operator is a symbol which are used to perform particular operation. In python language these opeatrs are classified into following types.
1.Unary Operators
2.Binary Opraators
3.Ternary Oprators

### Key Points

• What is Unary Operators
• What is Binary Opraators
• What is Ternary Oprators

##### Unary Operator:
If any operation perform on single operand (Variable) is known as uanry operrator.
These opeators are classified into following types.
1.Unary Minus Operator
2.Incremnent Operator
3.Decrement Operator

##### 1.Unary Minus:
Unary minus means sign value or minus value
Example:
x=10;
then -x=-10;

```def myfun():
x=int(input("Enter x value"))
print("The sign value is ",-x)

if __name__=="__main__":
myfun()
```

###### Output:
The Sign value is -10

## Binary Operators

##### Binary Operator:
If any operation perform on two operands(variable) is known as binary operators.
In Python language these opeatrs are classified into following types.
1.Arithmetic Operators
2.Assignment Operators
3.Relational Operators
4.Logical Operators
5.Bitwise Operators
6.Special Operators

### Key Points

• What is Arithmetic Operators
• What is Assignment Operators
• What is Relational Operators
• What is Logical Operators
• What is Bitwise Operators

##### Arithmetic Operators
These are the operands can be used to perform simple mathametical operation. Which are classified into following types.
Operator Operation Example
- Substraction x-y
* Multiplication x*y
/ Division x/y
% Modular Division x%y
// Floor Division x//y
** Exponent x**y
```def myfun():
x=int(input("Enter x value"))
y=int(input("Enter y value"))
z=x+y

if __name__=="__main__":
myfun()
```
##### Assignment Operators
Assignment operators are used to assign the values to the variable or assign the variable to variable
There are classified into following types.
Operator Example Same As
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y x=x%y
//= x//=y x=x//y
**= x**=y x=x**y
##### Swaping Of Two Numbers With Using Third Variable
```def myfun():
x=int(input("Enter x value"))
y=int(input("Enter y value"))
z=x
x=y
y=z
print("x value is ",x)
print("y value is ",y)

if __name__=="__main__":
myfun()
```
x value is: 20
y value is: 10
##### Swaping Of Two Numbers Without Using Third Variable
```def myfun():
x=int(input("Enter x value"))
y=int(input("Enter y value"))
x=x+y
y=x-y
x=x-y
print("x value is ",x)
print("y value is ",y)

if __name__=="__main__":
myfun()
```
x value is: 20
y value is: 10
##### Relational Operators
Relational operators are used to check the relation between two variables. These are classified into following types
Operator Example
== x==y
< x<y
> x>y
<= x<=y
>= x>=y
!= x!=y
##### Relation Of Two Numbers
```def myfun():
x=int(input("Enter x value"))
y=int(input("Enter y value"))
z=x>y
print("z value is ",z)

if __name__=="__main__":
myfun()
```

###### Output:
z value is: false
##### Logical Operators
Logical Operators are used to combine the morethan one condition. These are classified into following types.
Operator Example
and x>y and x>z
or x>y or x>z
not not(x>y and x>z)

not(x>y or x>z)

not(x>y) and not(x>z)

not(x>y) or not(x>z)
##### Logical Operators
```def myfun():
x=int(input("Enter x value"))
y=int(input("Enter y value"))
z=int(input("Enter z value"))
p=x>y and x>z
print("p value is  ",p)

if __name__=="__main__":
myfun()
```
###### Output:
p value is: false
##### Bitwise Operators
Bitwise Operators are used to perform any operaion on binary(0 and 1) values.These are classified into following types
Operator Operation Example
& AND x&y
| OR x|y
^ CAP x ^ y
~ NAGETION ~x
<< LEFT SHIFT x<<y
>> RIGHT SHIFT x>>y
##### Bitwise and(&) Operator

```def myfun():
x=int(input("Enter x value"))
y=int(input("Enter y value"))
z=x&y
print("z value is ",z)

if __name__=="__main__":
myfun()
```

z value is: 10

### Bitwise or ( | ) Operators

```def myfun():
x=int(input("Enter x value"))
y=int(input("Enter y value"))
z=x|y
print("z value is ",z)

if __name__=="__main__":
myfun()
```

z value is: 15

### Bitwise CAP ( ^ ) Operators

```def myfun():
x=int(input("Enter x value"))
y=int(input("Enter y value"))
z=x^y
print("z value is ",z)

if __name__=="__main__":
myfun()
```
z value is: 5

### Bitwise negation ( ~ ) Operator

```def myfun():
x=int(input("Enter x value"))
z=~x
print("z value is ",z)

if __name__=="__main__":
myfun()
```
z value is: -11

### Bitwise left shift( << ) Operator

```def myfun():
x=int(input("Enter x value"))

z=x<<2
print("z value is ",z)

if __name__=="__main__":
myfun()
```
z value is: 40

### Bitwise Right shift( >> ) Operator

```def myfun():
x=int(input("Enter x value"))

z=x>>2
print("z value is ",z)

if __name__=="__main__":
myfun()
```
z value is: 2
##### Conditional or Ternary Operator
Ternary operator is used to perform any operation on three operands
Syntax :

Expression1 if condtion else Expression2;

Here
Expression1 and Expression2 is a operations

In the above syntax
if condition is true ,The statements of Expression1 is execute.
if condition is false ,The statements of Expression2 is execute
##### Odd or Even Number Using Ternary Operator

```def myfun():
x=int(input("Enter x value"))
z= "EvenNumber" if x%2==0 else "OddNumber";

print(z)

if __name__=="__main__":
myfun()
```
EvenNumber
##### Max Number Using Ternary Operator
```def myfun():
x=int(input("Enter x value"))
x=int(input("Enter x value"))

z= "x is max" if x>y else  "y is max";

print(z)

if __name__=="__main__":
myfun()
```
y is Max
##### 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
is and is not 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 Example Same As
is True if the operands are identical (refer to the same object) x is Ture
is not True if the operands are not identical (do not refer to the same object) x is not True
##### Example : Identity operators in Python
```
x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]

# Output: False
print(x1 is not y1)

# Output: True
print(x2 is y2)

# Output: False
print(x3 is y3)

```
Here, we see that x1 and y1 are integers of same values, so they are equal as well as identical. Same is the case with x2 and y2 (strings). But x3 and y3 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
in and not in 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 Example Same As
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 : Membership operators in Python
```
x = 'Hello world'
y = {1:'a',2:'b'}

# Output: True
print('H' in x)

# Output: True
print('hello' not in x)

# Output: True
print(1 in y)

# Output: False
print('a' in y)

```
Here, 'H' is in x but 'hello' is not present in x (remember, Python is case sensitive). Similary, 1 is key and 'a' is the value in dictionary y. Hence, 'a' in y returns False
##### Simple Interest Program
```def myfun():
a=int(input("Enter Amount"))
d=int(input("Enter Duration"))
i=int(input("Enter Interest"))
si=a*d*i/100
print("Simple Interest is ",si)

if __name__=="__main__":
myfun()
```
###### Output:
Simple Interest is: 80