OPERATORS
-
ARITHMETIC OPERATOR:
Arithmetic operator is an operator, which performs operation on operands. If both operands are integers then it returns int.Thus any one operator is float then it returns float value. If,
a=10, b=2
print (a+b) #12
print (a-b) #8
print (a*b) #20
print (a/b) #5.0
print (a%b) #0
print (a//b) #5
print (a**b) #100
Ex-1:
a=”Sangeetha”
b=5
print(a+b) #ERROR
Ex-2:
a=”purna”
b=”tech”
print(a+b) #purnatech
Ex-3:
a=True
b=True
print(a+b) #2
Ex-4:
a=”purna”
b=3
print(a+b) #purna purna purna
Ex-5:
a=”purna”
b=”3”
print(a+b) # ERROR.
- RELATIONAL OPERATOR:
It returns a Boolean type data. If,
a=10,b=20
print (a>b) #False
print (a<b) #True
print (a>=b) #False
print (a<=b) #True
print (a%b) #False
print (a==b) #True
print (a!=b) #False
Ex-1:
x=”purna”
y=”tech”
print(x>y) #False
Ex-2:
x=true
y=false
print(x>y) #True
Ex-3:
x=false
y=false
print(x>y) #True
Ex-4:
x=10
y=”10”
print(x==y) #False
Ex-5:
x=”raju”
y=”srinu”
print(x==y) #False
Ex-6:
x=”abc”
y=”abc”
print(x!=y) #False
- LOGICAL OPERATOR:
- For Boolean values:
- If both operands are true then (and) operator returns true.
- If any one operands is true then (or) operator returns false.
1) AND: [ True and True #True
True and False #False
False and True #False
False and True #False ]
2) OR : [ True or False # True
True or True #True
False or False #False ]
- For non-boolean values:
- Non-zero means true
- Zero means false
- Empty string means false
- String means true
AND:
Ex-1: print(10 and 20) #20
Ex-2: print( 0 and 30) # 0
- If first operand is false the “and” operator returns first value otherwise sccond value return.
Ex-3: print( “raju” and “rani” ) #rani
Ex-4: print(‘ ‘ and “hari”) # ‘ ‘
OR:
Ex-1: print(10 or 20 ) #10
print(0 or 30 ) #30
print(“raju” or “rani” ) #raju
print( ‘ ‘ or “hari”) #hari.
4) BITWISE OPERATOR:
We can apply bitwise operators on integers and Boolean type data only.
Bitwise operator perform operations bitwise (0,1).
- AND:
Ex-1:
a=4
b=5
print (a &b) #4
If both operands are 1 “and operator” returns ‘1’, otherwise “0”.
- OR:
Ex-1:
a=4
b=5
print (a/b) #5
If any operand is “1” then “Or operator” returns ‘1”, otherwise “0.
- EXCLUSIVE:
Ex-1:
a=4
b=5
print (a^b) #1
If there are different operands then exclusive operator returns “1” otherwise “0”.
- NEGATION:
Ex-1:
a=~4
print(a) #-5
Negative numbers will be stored in computers memory in the form of 2’s compliment format.
- SHIFT LEFT:
Ex-1:
a=10
b=2
print(10<<2) #40
The left shift we have to add 0’s at right end.
- SHIFT RIGHT:
Ex-1:
a=10
b=2
print(10>>2) #2
The right shift we have to add 0’s at left end.
5) ASSIGNMENT OPERATOR:
To assign values to the variable. It is use for the compound statement. Pre-increement , post increment are not allowed in python.
Ex-1:
a=5
a ,b ,c= 10,3,15
We need to specify to reduce the code
- a+=1
- a-=1
- a*=1
- a/=1
Ex-2:
i=1
print(i++) # ERROR
print(++i) #1 [ It represents as a positive number]
Ex-3:
a=10
print(a+=5) #15
print(a) #15
6) SPECIAL OPERATORS:
According, Two reference variables have same value then it becomes true. It can perform reference comparision.
Ex-1:
a=10
b=10
c=100
print( a is b) # True
print ( b is c) # False
7) MEMBERSHIP OPERATOR:
So, It checks whether the value in list or not.
Ex-1:
l=[10,20,30,40]
print( 10 in l) # True
Ex-2:
s=”purna techs print(‘s’ in s) #True
print(‘ ‘ in s) #True
print(‘z’ not in s) # True
Now to find the length of purna techs.
Ex-3:
l=”purna techs”
c=0
for x in l:
c=c+1
print( c ) #11