Python Operators

In this tutorial, we will learn about Operators in Python and we will also cover different examples related to Python operators. And, we will cover these topics.

  • Python operators
  • Arithmetic operators
  • Comparison operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Identity operators
  • Membership operators
  • Precedence operators
  • Associativity operators

Python operators

Python operators are generally used to perform arithmetical and logical operations. The value that the operator operates is called an operand.

Example1:

In the following code, we will use an arithmetic operator. In the arithmetic operator, we use add function and print the output of this function on the screen.

# Examples of Arithmetic Operator
a = 12
b = 8
 
# Addition of numbers
add = a + b
print(add)

Output:

After running the above code we get the following output in which we can see that the + operator performs addition and the result is shown on the command prompt.

Python arithmetic operators
Python arithmetic operators

Example2:

# Examples of Logical Operator
a = True
b = False
 
# Print a and b is False
print(a and b)

Output:

After running the above code we get the following output in which we can see that the logical operator is printed on the command prompt.

Python logical operators
Python logical operators

Arithmetic operators

The arithmetic operators are defined as the operator which performs a mathematical operation such as addition, subtraction, multiplication, and addition.

Operators Use of operators Syntax
++ is addition operator: Add two operandsa+b
– is subtraction operator: Subtract two operandsa-b
** is multiplication operator: Multiply two operandsa*b
// is division operator: Divides that first operand by the seconda/b
%% is modulus operator: Modulus return the operator when the first operand is divided by the seconda%b
**** is Power operator: Power operator return first raised to power seconda ** b
Arithmetic operators

Example:

a = 15
b = 4

# Output: a + b = 19
print('a + b =',a+b)

# Output: a - b = 11
print('a - b =',a-b)

# Output: a * b = 60
print('a * b =',a*b)

# Output: a / b = 3.75
print('a / b =',a/b)

# Output: a // b = 3
print('a // b =',a//b)

# Output: a ** b = 50625
print('a ** b =',a**b)

Output:

After running the above code we get the following output in which we can see that arithmetic operations are performed on the screen.

Python arithmetic Operator Example
Python arithmetic operator example

Comparison operators

The comparison operator is defined as it compares the values. It returns the true value or false it depends upon the condition we use or apply.

OperatorsUse of operatorsSyntax
>> is Greater than operator: True if the left operand a is greater than the right operand ba>b
<< is Less than operator: True if the right operand b is grated than the left operand aa<b
==== Equal to an operator: True if both operands are equal to each othera==b
!=!= Not Equal to an operator: True if two operands are not equala!=b
>=>= Greater than equal to an operator: True if the left operand a is greater than equal to right operand ba>=b
<=<= Less than or equal to an operator: True if the left operand a is less than equal to right operand ba<=b
Comparison operators

Code:

# Examples of Relational Operators
a = 15
b = 45
 

print(a > b)
 

print(a < b)
 

print(a == b)
 

print(a != b)
 

print(a >= b)
 

print(a <= b)

Output:

After running the above code we get the following output in which we can see that the comparison operations are performed on the screen.

Python comparison operator
Python comparison operator

Logical operators

Logical operators perform Logical AND, Logical OR, and Logical NOT operation. It also combines conditional statements.

OperatorsUse of operatorsSyntax
andLogical AND: True is both the operand a and b is truea and b
orLogical OR: True if either a is true or b is truea or b
notLogical NOT: True if both the operand is falsea not b
Logical operators

Example:

In the following code, we will see how the logical operator works.

# Examples of Logical Operator
a = False
b = True
 
# Print a and b is False
print(a and b)
 
# Print a or b is True
print(a or b)
 
# Print not a is False
print(not a)

Output:

After running the above code we get the following output in which we can see those logical operations are performed on the screen.

Python logical operators
Python logical operators

Bitwise operators

Bitwise operator is defined as the operator which acts as a bit and performs bit by bit operations.

OperatorsUse of operatorsSyntax
&Bitwise ANDa&b
|Bitwise ORa|b
~Bitwise NOTa~b
^Bitwise XORa^b
>>Bitwise right shifta>>b
<<Bitwise left shifta<<b
Bitwise operators

Example:

# Examples of Bitwise operators
a = 20
b = 11
 
# Print bitwise AND operation
print(a & b)
 
# Print bitwise OR operation
print(a | b)
 
# Print bitwise NOT operation
print(~a)
 
# print bitwise XOR operation
print(a ^ b)
 
# print bitwise right shift operation
print(a >> 9)
 
# print bitwise left shift operation
print(a << 9)

Output:

After running the above code we get the following output in which we can see that bitwise operations are performed.

Python bitwise operators
Python bitwise operators

Assignment operators

The assignment operator is used to define that operator is used to assign the value to the variable.

OperatorsUse of operatorsSyntax
=We can assign the value to the right-side expression to the left side of the operanda=b+c
+=Add right operand with the left side operand and then assign it to the left operanda+=b a=a+b
-=Subtract left operand from the right operand then assign the value to the left operanda-=b a=a-b
*=Multiply right operand with the left operand and then assign value to the left operanda*=b a=a*b
/=Divide left operand with right operand and then assign value to the left operand a/=b a=a/b
%=We can take the modulus using the left and right operand and then assign the value to the left operanda%=b a=a%b
**=We can calculate the exponent value using operands and assign the value to the left operanda**=b
a=a**b
&=It can perform Bitwise And on operands and assign value to the left operanda&=b a=a&b
|=It can perform Bitwise Or on operands and assign value to the left operanda|=b a=a|b
^=It can perform bitwise XOR on operands and assign value to the left operanda^=b a^b
>>=We can perform bitwise right sift on operand and assign the value to the left operanda>>=b a>>b
<<=We can perform a bitwise left shift on operand and assign the value to the left operanda<<=b a<<b
Assignment operators

Example:

# Examples of Assignment Operators
a = 25
 
# Assign value
b = a
print(b)
 
# Add and assign value
b += a
print(b)
 
# Subtract and assign value
b -= a
print(b)
 
# multiply and assign
b *= a
print(b)
 
# bitwise lishift operator
b <<= a
print(b)

Output:

After running the above code we get the following output in which we can see that the assignment operation is performed on the screen,

Python assignment operators
Python assignment operators

Identity operators

is or is not these operators are used to check the values are located on the same part of the memory.

isThis is true if the operand is identical
is notThis is true if the operand is not identical
Identity operators

Example:


x = 20
y = 30
z = x
 
print(x is z)
print(x is not y)

Output:

After running the above code we get the following output in which we can see that the identity operation is performed on the screen.

Python identity operators
Python identity operators

Membership operators

in and not in are the two membership operators this operator is used to test the values or variables are in sequence.

inTrue if the value is found in the sequence
not inTrue if the value is not found in the sequence
Membership operators

Example:


x = 34
y = 30
list = [20, 30, 40, 50, 60]
 
if (x not in list):
    print("x is not present in given list")
else:
    print("x is present in given list")
 
if (y in list):
    print("y is present in given list")
else:
    print("y is not present in given list")

Output:

After running the above code we get the following output in which we can see that membership operations are performed on the screen.

python membership operators
python membership operators

Precedence operators

The precedence operator is used as an expression with one or more operators with different precedence to determine which operation to perform first.

Example:

# Examples of Operator Precedence
 
# Precedence of '+' & '*'
expr = 20 + 30 * 40
print(expr)
 
# Precedence of 'or' & 'and'
name = "Elbert"
age = 0
 
if name == "Alex" or name == "John" and age >= 2:
    print("Welcom to Pythontpoint.")
else:
    print("Thank You For visit!!")

Output:

After running the above code we get the following output in which we can see that the precedence operation is performed on the screen.

Python precendence operator
Python precedence operator

Associativity operators

The associative operator is defined as an expression containing two or more precedence than the associative operator is used to determine.

Example:

# Examples of Associativity operator

print(1000 / 100 * 100)


print(50 - 20 + 30)
 
print(50 - (20 + 30))


print(20 ** 30 ** 20)

Output:

After running the above code we get the following output in which we can see that the associativity operation is performed on the screen.

Python associativity operators
Python associativity operators

So, in this tutorial, we discussed Python operators and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • Python operators
  • Arithmetic operators
  • Comparison operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Identity operators
  • Membership operators
  • Precedence operators
  • Associativity operators

Leave a Comment