Python Basic – How to use Operators in Python

Table of Contents

Related Tutorials

Python Operators

Operators in Python are used to perform various operations on variables and values. They are an essential part of the language and are used to control the flow of the program, perform calculations, and make decisions.

Python divides the operators into the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Bitwise operators

Arithmetic Operator

These operators are used to perform basic arithmetic operations such as addition, subtraction, multiplication, and division. They are essential for performing mathematical calculations and are used in a wide range of applications. Mathematical operators are used to performing any simple mathematical operation with numeric values:

NameOperatorTask
Addition+P + Q
SubtractionP – Q
Multiplication*P * Q
Division/P / Q
Floor Division//P // Q
Modulus%P % Q
Exponential**P ** Q

Example:

   >>> P = 38
   >>> Q = 10
   >>> print( P + Q )
   48
   >>> print( P - Q )
   28
   >>> print( P * Q )
   380
   >>> print( P / Q )
   3.8
   >>> print( P // Q )
   3
   >>> print( P % Q )
   8
   >>> print( P ** Q )
   6278211847988224
   >>>

Assignment Operator

Assignment operators are used to assigning a value to a variable. The basic assignment operator is =, which assigns the value on the right side of the operator to the variable on the left side. There are also shorthand assignment operators that allow you to perform an operation and an assignment in one step. To assign values to variables, assignment operators and in-place operators are used:

Main TaskOperated TaskAssignment Operator
P = 81P = 81=
P = P + 81P += 81+=
P = P – 81P -= 81-=
P = P * 81P *= 81*=
P = P / 81P /= 81/=
P = P // 81P //= 81//=
P = P % 81P %= 81%=
P = P ** 81P **= 81**=
P = P & 81P &= 81&=     [‘&’ Bitwise AND]
P = P | 81P |= 81|=       [‘|’ Bitwise OR]
P = P ^ 81P ^= 81^=      [‘^’ Bitwise XOR]
P = P >> 81P >>= 81>>=    [‘>>’ Right Shift ]
P = p << 81P <<= 81<<=    [‘<<’ Left Shift]

Example:

x = 5
print(x)

x += 2 # equivalent to x = x + 2
print(x)

x -= 2 # equivalent to x = x - 2
print(x)

x *= 2 # equivalent to x = x * 2
print(x)

x /= 2 # equivalent to x = x / 2
print(x)

x %= 2 # equivalent to x = x % 2
print(x)

Output:

5
7
5
10
5.0
1.0

Comparison Operators

These operators are used to compare values and are essential for making decisions in control flow statements such as if-else statements. They are used to check if a value is equal to, greater than, or less than another value. Comparison operators are used to compare two values:

NameOperatorTaskReturn Type
Equal==P == QTrue / False
Not equal!=P != QTrue / False
Greater than>P > QTrue / False
Less than<P < QTrue / False
Greater than and equal>=P >= QTrue / False
Less than and equal<=P <= QTrue / False

Example:

x = 5
y = 3
print(x == y) # False

x = 5
y = 3
print(x != y) # True

x = 5
y = 3
print(x > y) # True

x = 5
y = 3
print(x < y) # False

x = 5
y = 3
print(x >= y) # True

x = 5
y = 3
print(x <= y) # False

x = 5
print(1<x<10) # True


Output:

False
True
True
False
True
False
True

It’s important to keep in mind that when comparing different types of data, such as integers and strings, Python follows a specific set of rules for type coercion. For example, when comparing a string and an integer, Python will automatically convert the string to an integer if it can, and then perform the comparison.

Logical Operators

Logical operators are used to combining conditions and make decisions. They are used to check if multiple conditions are true or false. There are three main logical operators and, or, not. Logical operators are used to combine conditional statements:

DetailsOperatorTask
Returns True if both statements are trueandP > 38 and Q < 10
Returns True if one of the statements is trueorP < 23 or Q > 20
Reverse the result, and returns False if the result is truenotnot ( P > 38 and Q < 10 )

Example:

x = 5
y = 10
if x > 0 and y > 0:
    print("x and y are positive")

x = 5
y = -10
if x > 0 or y > 0:
    print("x or y is positive")

x = 5
if not x > 0:
    print("x is not positive")

Output:

x and y are positive
x or y is positive

Identity Operators

These operators are used to check if two variables refer to the same object in memory. Identity operators are used to comparing the objects, not if they are equal, but if they are actually the same object, with the same memory location:

DetailsOperatorTask
Returns True if both variables are the same objectisP is Q
Returns True if both variables are not the same objectis notQ is not P
Returns True if a sequence with the specified value is present in the objectinP in Q
Returns True if a sequence with the specified value is not present in the objectnot inP not in Q

Example:

x = [1, 2, 3]
y = [1, 2, 3]
z = x

print(x is y) # False
print(x is z) # True


x = [1, 2, 3]
y = [1, 2, 3]
z = x

print(x is not y) # True
print(x is not z) # False

Output:

False
True
True
False

Bitwise Operators

These operators are used to manipulate the binary representation of integers. They are used to perform bit-level manipulation and are particularly useful when working with low-level systems or embedded systems. Bitwise operators are used to comparing (binary) numbers:

DetailsOperatorName
Sets each bit to 1 if both bits are 1&AND
Sets each bit to 1 if one of the two bits is 1|OR
Sets each bit to 1 if only one of the two bits is 1^XOR
Inverts all the bits~NOT
Shift left by pushing zeros in from the right and let the leftmost bits fall off<<Zero-fill left shift
Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off>>Zero-fill Right shift

Example:

x = 5  # binary representation: 00000101
y = 3  # binary representation: 00000011
result = x & y  # binary representation: 00000001, equivalent to 1 in decimal
print(x,y,result)

x = 5  # binary representation: 00000101
y = 3  # binary representation: 00000011
result = x | y  # binary representation: 00000111, equivalent to 7 in decimal
print(x,y,result)

x = 5  # binary representation: 00000101
y = 3  # binary representation: 00000011
result = x ^ y  # binary representation: 00000010, equivalent to 2 in decimal
print(x,y,result)


x = 5  # binary representation:  00000101
result = x << 2  # binary representation: 00010100
print(x,2,result)

x = 20  # binary representation:  00010100
result = x >> 2  # binary representation:  00000101
print(x,2,result)



Output:

5 3 1
5 3 7
5 3 6
5 2 20
20 2 5

These are just a few examples of the importance and use of operators in Python. They are essential for controlling the flow of the program, making decisions, performing calculations, and manipulating data, which is fundamental in any programming language.

Share The Tutorial With Your Friends
Facebook
Twitter
LinkedIn
Email
WhatsApp
Skype