What are the operators in the Python Programming Language

What are the operators in the Python Language?

 Python Arithmetic Operators are
+  +2, 2+3, 4+8 etc
- -5,5-9, 9-5
* 4*5
/    5/4
// floor division 5//3
** Exponent 2**3
% remainder  6%2
  • Bitwise operators
    • &   And
    • |     Or
    • ^    XOR



def tobinary(n):
    output=""
    while n>0:
        r=n % 2
        n=n // 2
        output= str(r) + output
    if output=="":
        return "0"
    return output
a=4
b=5
c=a | b
d=a & b
e=a ^ b
print("A = {0}, B = {1}, A | B = {2}, A & B = {3}, A ^ B = {4}".format(a,b,c,d,e))
print("A = {0}, B = {1}, A | B = {2}, A & B = {3}, A ^ B = {4}".format(tobinary( a),tobinary(b),tobinary(c),tobinary(d),tobinary(e)))


Assignment Operators
=. a=b, copies value of b into a. b=5, a=b. a becomes 5
a+=b. b=5, a=3, a+=b = a= a+b. a=3+5=8
a%=b equals a=a%b
a**=b equals a=a**b
a/=b equals a=a/b
a*=b equals a=a*b
a-=b equals a=a-b
a//=b equals a=a//b

a=3
b=5
a=a+b
print(a)
a=3
b=5
a+=b
print(a)

Varanasi Software Junction: Addition Operator Result



Membership operators
1. in
2. not in

s="1,2,3,4"
x="2"
print(x in s)
print( x not in s)
x="45"
print(x in s)
print( x not in s)




Identity Operators
1. is
2. is not
Checks if two variables are the same
a=[1,2,3,4]
b=a
print(a,b,a is b)
b[2]=88
print(a,b,a is b)
a=[1,2,3,4]
b=list(a)
print(a,b,a is b)
b[2]=88
print(a,b,a is b)
Relational
a<b, a>b, a<=b, a>=b, a==b, a!=b
a=1
b=1
print(a<b,a<=b,a>b,a>=b,a==b,a!=b)
print(not a<b, not a<=b,not a>b,not a>=b,not a==b,not a!=b)

Logical
a and b, a or b, not a 
a=False
b=False
print("{0} or {1}={2}".format(a,b,a or b))
a=False
b=True
print("{0} or {1}={2}".format(a,b,a or b))
a=True
b=False
print("{0} or {1}={2}".format(a,b,a or b))
a=True
b=True
print("{0} or {1}={2}".format(a,b,a or b))
a=False
b=False
print("{0} and {1}={2}".format(a,b,a and b))
a=False
b=True
print("{0} and {1}={2}".format(a,b,a and b))
a=True
b=False
print("{0} and {1}={2}".format(a,b,a and b))
a=True
b=True
print("{0} and {1}={2}".format(a,b,a and b))



Post a Comment

0 Comments

Me