The Python Operators

In the Python Language, variables hold data values that convey the message. These values are either for logical or arithmetic operations. Meaning on python, all the process of writing codes is done using the standard python Operators. In a very simple and clear manner, you’ll be learning the python Operators, what is it, the features of python Operators, and also you’ll be knowing the types and functions of Python Operators. For the practical aspect, you’ll know how to apply them to Python Language, before that you will learn how to access the python operator Values. In conclusion, you’ll be able to update the python Operators and delete some unwanted operations.

What are Python Operators

Python Operators are standard symbols that are used for logical and arithmetic operations in Python. Operators are used with operands to return a value e.g 2+8=10, 2 and 8 are the operands,(+) is the operator and the returned Value or output is 10. Python has seven (7) Operators and they are:

  1. Arithmetic Operators
  1. Comparison/Relational Operators
  1. Assignment Operators
  1. Logical Operators
  1. Bitwise Operators
  1. Membership Operators
  1. Identity Operators

This page is dedicated to giving complete knowledge of these operators.

Arithmetic Operators

The Arithmetic Operators are used to carry out all arithmetic or mathematical activities in python. Mathematical operations like addition, subtraction, multiplication, and division are enabled by the mathematical symbols: ( +, -, /, *). See the table for complete arithmetic Operators symbols

OperatorDescription based on operands x and y
+The addition adds two operands x+y
Subtraction: subtracts two operands x-y
*Multiplication: multiply two operands x*y
/Division(float): divides two operands x/y
//Floor division: divides two operands x//y
%Modulus: gives the remainder after the operands are been divided x%y
**Exponent: gives the result of x raised to power y: x**y

Example of Arithmetic Operators

Note: all the above examples will be shown at once. Take x=9 and y=4

Python Input:

# Examples of Arithmetic Operator
x = 9
y = 4
# Addition of numbers
add = x + y
# Subtraction of numbers
sub = x - y
# Multiplication of a number
mul = x * y
# Division(float) of number
div1 = x / y
# Division(floor) of number
div2 = x // y
# Modulo of both numbers
mod = x % y
# Power
p = x ** y
# print results
print(add)
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
print(p)

Output:

13
5
36
2.25
2
1
6561

The above returns explain the arithmetic Operators practically

Comparison Operators in Python

Comparison Operators in Python are used to compare values. It gives True or False according to the condition. The python comparison Operators are also called Relational Operators. Take x and y as our two values

OperatorDescription based on operands x and y
Greater than: True if the left is greater than the right x>y
Less than: True if the left is less than the right x<y
==True if two operands are equal x==y
!=Not equal to: True if two operands are not equal x!=y
>=Greater than or equal to True if the left operand is greater than or equal to the right operand x>=y
<=Less than or equal to True if the left operand is less than or equal to the right operand x<=y

Example of comparison Operators in Python

Take x=11 and y=22

Python input:

# Examples of Relational Operators
x = 11
y = 22
# x > y is False
print(x > y)
# x < y is True
print(x < y)
# x == y is False
print(x == y)
# x != y is True
print(x != y)
# x >= y is False
print(x >= y)
# x <= y is True
print(x <= y)

Output:

False
True
False
True
False
True

The above returns a practical explanation of the python comparison Operators

Assignment Operators

As the name implies, assignment Operators are used to assigning values to variables in Python. Meaning all characters must have the assignment symbol (=).  Let’s see the tablet with x and y as our operands

OperatorDescription based on operands x and y
=Assign Values at the right operand to the left v=x+y
+=ad ANDAdds right operand to left operand and assigning returns to left operand v+=x is equivalent to v=v+x
-= subtract ANDSubtracts right operand from left operand and assign returns to left operand v-=x is equivalent to v=v-x
*= Multiply ANDMultiplies right operand with the left operand and assign returns to the left operand v*=x is equivalent to v=v*x
/= Divide ANDDivides the right operand with the left operand and assigns returns to the left operand v/=x is equivalent to v=v/x
% Modulus ANDIt takes modulus with left and right operands and assigns the result to the left operand v%=x is equivalent to v=v%x
**= Exponent ANDCalculates the exponential power value of both operands and assigns Values to left v**=x is equivalent to v=v**x  
&= Bitwise ANDCalculates the bitwise operation on both operators and assigns the value to the left operand v&=x is equivalent to v=v&x
|= Bitwise ORCalculates the bitwise OR and assigns value to the left operand v|=x is equivalent to v=v|x
^= Bitwise xORCalculates bitwise xOR and assigns Values to the left operand v^=x is equivalent to v=v^x
>>= Bitwise right shiftPerforms bitwise right shift on operands and assigns Values to left operand v>>=x is equivalent to v=v>>x
<<= Bitwise left shiftPerforms bitwise left shift on operands and assigns Values to the left operand v<<=x is equivalent to v=v<<x

Some examples of assignment Operators in Python

Python Input:

# Examples of Assignment Operators
x = 20
# Assign value
y = x
print(y)
# Add and assign value
y  += x
print(y)
# Subtract and assign value
y -= x
print(y)
# multiply and assign
y *= x
print(y)
# bitwise shift operator
y <<= x
print(y)

Output:

20
40
20
200
204800

The above examples show the assignment Operators practically

Python Logical Operators

Logical operators are used to joining conditional statements to give logical output. It uses the Logical AND, Logical OR, and Logical NOT operations.

See the table for all Logical Operators in Python

OperatorsDescription based on operands x and y
and Logical ANDReturns True if (x and y) are true
or Logical ORReturns True if either (x or y) is true
not Logical NOTReturns True if neither ( x or y) is true

Example of Python Logical Operators

Python input:

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

Output:

False
True
False

The above expresses the Python Logical Operators in a practical order

Bitwise Operators in Python

These are the bit-by-bit binary numbers operations in Python. Some examples of the Bitwise operators and syntax are expressed in the table below

OperatorsDescription based on operands x and y
& Binary ANDCopies a bit to the results if both operands have the bit value x&y
| Binary ORCopies a bit of the result if any one of the operands has the bit value x|y
^ Binary XORCopies the bit if it is set in either x or y (x^y)
~ Bitwise NOTIt indicates a Flippo bit on one operand ~x
>> Bitwise right shiftThe left operand(x) is moved right to the number of bits indicated by the right operand (x>>)
<< Bitwise left shiftThe left operand(x) is moved left to the number of bits indicated by the right operand (x<<)

Example of Python Bitwise operators

Python Input:

# Examples of Bitwise operators
x = 10
y = 4
# Print bitwise AND operation
print(x & y)
# Print bitwise OR operation
print(x | y)
# Print bitwise NOT operation
print(~x)
# print bitwise XOR operation
print(x ^ y)
# print bitwise right shift operation
print(x >> y)
# print bitwise left shift operation
print(x << y)

Output:

0
14
-11
14
2
40

Membership Operators

The membership Operators check whether a variable or value is included in a sequence. It uses not or not as its operators.

  • it signifies true if a value or variable is found in a sequence
  • not in returns false if a value or variable is not in a sequence

Example of Python Membership Operators

# Python program to illustrate
# not 'in' operator
x = 24
y = 20
list = [10, 20, 30, 40, 50]
if (x is not in the list):
    print("x is NOT present in the given list")
else:
    print("x is present in the given list")
if (y in the list):
    print("y is present in the given list")
else:
    print("y is NOT present in the given list

Output:

x is NOT present in the given list
y is present in the given list

Python Identify Operators

The identity operators identify and compare the memory location of two objects. The identity operators operate in two categories which are:

  • is: Simply true if the Values are similar( x is y)
  • is not: Signifies true if the values are not similar (x is not y)

Example Of Python Identify Operators

Python Input:

x = 10
y = 20
v = a
print(x is not y)
print(x is c)

Output:

True
True

Python Precedence Operators

Python Precedence Operators are used in an operation with more than one operator to indicate which operator should operate first. It determines operations according to priority.

The Table Below Shows all of python’s precedence from highest to lowest

OperatorDescription
**ExponentRaise the power
~+-complement, urinary plus, and minus
*/%//Multiplication, divide, modulus, and floor division
+-Addition and subtraction
>><< Right Bitwise shift and Left Bitwise shift
&Bitwise AND
|^Bitwise normal OR and exclusive OR
<= < > >=Comparison operators
<> == !=Equality operators
= %= /= //= -= += *= **=Assignment operators
is is notIdentity operators
in, not inMembership operators
not, or, andLogical operators

Example of Python Precedence Operators

Python Input:

# Examples of Operator Precedence
# Precedence of '+' & '*'
expr = 1 + 50 * 10
print(expr)
# Precedence of 'or' & 'and'
name = "Simon"
age = 0
if name == "Simon" or name == "Monday" and age >= 2:
    print("Hello! Tanya.")
else:
    print("Good-Bye!!")

Output:

501
Hello! Tanya.

From the above output, python prints according to precedence.

Summary

In the Python Language, variables hold data values that convey the message. These values are either for logical or arithmetic operations.

Python Operators are standard symbols that are used for logical and arithmetic operations in Python.

Python Precedence Operators are used in an operation with more than one operator to indicate which operator should operate first. It determines operations according to priority.

In this topic, you have been taught extensively on Python Operators and all the Python Precedence Operators. It’s all on you to give the theory some practice for maximum results. For a headstart check out the python tutorials. Good luck Coding!

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment