× Python Introduction What is Python Python Features Python History Python Applications Python Install Python Path Python Example Execute Python Keywords Constant Variable Statements & Comments Python I/O and Import Operators UnaryBinaryTernary Unary Operators Unary Minus Binary Operators Arithmetic Operators Assignment Operators Relational Operators Logicaloperators Bitwise Operator Ternary Operators Control Statements in Python conditonal Statements IF if else Else If Nested if Switch For loop Nested For Loop While Loop Nested while Loop Unconditonal Statemets Continue Break Pass FUNCTIONS Python Function Function Argument Python Recursion Anonymous Function Python Modules NATIVE DATATYPES Python List Python Numbers Python Tuple Python String Python Set Python Dictionary OOPS PRINCIPALS Encapsulation Class Variable Method Object Or Instance CreationMethod Calling OOPS Syntax And Explanation DATA ABSTRACTION Constructor Inheritance 1.Single or simple Inheritance 2.Multilevel Inheritance 3.Hierarchical Inheritance 4.Multiple Inheritance 5.Hybrid Inheritance Operator Overloading File Operation Python Directory Python Exception Python - Multithreading Python - Database Access Python - CGI Python - Reg Exp Python - Date Python - XML Processing Python - GUI
  • iconPython Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

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

Image
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


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

    
if __name__=="__main__":
    myfun()
Output:
Addition is 30
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()
Output:
x value is: 20
y value is: 10
Swaping Of Two Numbers Without Using Third Variable
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()
Output:
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()

Output:
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()

Output:
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()
Output:
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()
Output:
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()
Output:
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()
Output:
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 is a operation

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()
Output:
EvenNumber
MAX Number Using Ternary Operator
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()
Output:
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.
Output:
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
Output:
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