Python Operators

Python operators are used to perform arithmetic, comparison, logical, and bitwise operations. In this article, we will discuss the different types of operators in Python and how they can be used.

  1. Arithmetic Operators: Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, division, modulus, and exponentiation. Here is a list of arithmetic operators in Python:
  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)
  • Exponentiation (**)
  • Floor Division (//)

Example 1:

x = 10
y = 5
print(x + y) # 15
print(x - y) # 5
print(x * y) # 50
print(x / y) # 2.0
print(x % y) # 0
print(x ** y) # 100000
print(x // y) # 2

Output 1.1

Arithmetic Operators
  1. Comparison Operators: Comparison operators are used to compare two values and return a Boolean value (True or False). Here is a list of comparison operators in Python:
  • Equal to (==)
  • Not equal to (!=)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)

Example 2:

x = 10
y = 5
print(x == y) # False
print(x != y) # True
print(x > y) # True
print(x < y) # False
print(x >= y) # True
print(x <= y) # False

Output 2

Comparison Operators:
  1. Logical Operators: Logical operators are used to combine multiple conditions and return a Boolean value (True or False). Here is a list of logical operators in Python:
  • And (and)
  • Or (or)
  • Not (not)

Example 3:

x = 10
y = 5
z = 7
print(x > y and x < z) # True
print(x > y or x < z) # True
print(not x > y) # False

Output 3

Logical Operators:
  1. Assignment Operators: Assignment operators are used to assign a value to a variable. Here is a list of assignment operators in Python:
  • Assignment (=)
  • Addition assignment (+=)
  • Subtraction assignment (-=)
  • Multiplication assignment (*=)
  • Division assignment (/=)
  • Modulus assignment (%=)
  • Exponentiation assignment (**=)
  • Floor Division assignment (//=)

Example 4:

x = 10
x += 5 # equivalent to x = x + 5
print(x) # 15
x = 10
x += 5 # equivalent to x = x + 5
print(x) # 15

Output 4

Assignment Operators:
  1. Bitwise Operators: Bitwise operators are used to perform operations on binary numbers. Here is a list of bitwise operators in Python:
  • Bitwise AND (&)
  • Bitwise OR (|)
  • Bitwise XOR (^)
  • Bitwise NOT (~)
  • Left Shift (<<)
  • Right Shift (>>)

Example 4:

x = 10 # equivalent to 0b1010
y = 5 # equivalent to 0b0101
print(x & y) # 0
print(x | y) # 15
print(x ^ y) # 15
print(~x) # -11
print(x << 1) # 20
print(y >> 1) # 2

Output 4

Bitwise Operators

Taking Input in Python Programming Language

Can I use multiple operators in a single expression in Python?

Yes, you can use multiple operators in a single expression in Python. However, you should be careful about the order of precedence of operators. Python follows the order of precedence, which means it evaluates expressions based on the priority of operators. For example, in the expression 2 + 3 * 4, Python will first evaluate 3 * 4 and then add the result to 2.

What is the difference between the “is” and “==” operators in Python?

The “is” operator in Python checks whether two variables refer to the same object in memory. On the other hand, the “==” operator checks whether two variables have the same value.

1 thought on “Python Operators”

Leave a Comment