MyAI

Python - Operators

Python - Operators

Types of Operators

Comparison Operators

Comparison Operators

Logical Operators

Bitwise Operators

Membership Operators

Identity Operators

Warning: Refer Operator Precedence in Python

import os
print('Hello '+os.getlogin() + '! Executing python script')
print("\n")

# Arithmetic Operators
print("------------Arithmetic Operators-------------------")
a = 20
b = 10
print("Additions of int",a,"&",b,"is",a+b)
print("Subtraction of int",a,"&",b,"is",a-b)
print("Multiplication of int",a,"&",b,"is",a*b)
print("Division of int",a,"&",b,"is",a/b)
print("Module operator",a,"&",b,"is",a%b)
print("Exponent operator",a,"&",b,"is",a**b)
print("Floor Division operator",a,"&",b,"is",a//b)

# Assignment Operators
x = 5
y = 6
x+=y # This is equal to x=x+y
print("Augmented Addition Operator (int to int) - x=5, y=6 is - ",x,"")

x = 5
y = 6.5
x+=y # This is equal to x=x+y
print("Augmented Addition Operator (int to float) - x=5, y=6.5 is - ",x)

x = 5
y = 5+6j
x+=y # This is equal to x=x+y
print("Augmented Addition Operator (int to float) - x=5, y=5+6j is - ",x)

# Membership Operators

variable = "sunil@email.com"
a= "@"
b = "s"
print(b, "not in", variable," :", b not in variable)
print(a," in ",variable," :", a in variable)

Result:

    Hello sunil! Executing python script

    ------------Arithmetic Operators-------------------
    Additions of int 20 & 10 is 30
    Subtraction of int 20 & 10 is 10
    Multiplication of int 20 & 10 is 200
    Division of int 20 & 10 is 2.0
    Module operator 20 & 10 is 0
    Exponent operator 20 & 10 is 10240000000000
    Floor Division operator 20 & 10 is 2
    Augmented Addition Operator (int to int) - x=5, y=6 is -  11 
    Augmented Addition Operator (int to float) - x=5, y=6.5 is -  11.5
    Augmented Addition Operator (int to float) - x=5, y=5+6j is -  (10+6j)
    s not in sunil@email.com  : False
    @  in  sunil@email.com  : True

Main Page   Next Chapter: Python - Control Statement