• In Python, operators are special symbols or keywords that perform operations on variables or values.
  • They are used to carry out tasks like arithmetic calculations, comparisons, logical operations, and more.

Types of Operators in Python

  • Python provides a rich set of operators that can be grouped into various categories. These are –
1. Arithmetic Operators

These operators perform basic mathematical/calculation operations.

Operator Description Example
+ Addition 5 + 3 = 8
- Subtraction 5 - 3 = 2
* Multiplication 5 * 3 = 15
/ Division (float) 5 / 2 = 2.5
// Floor division (integer) 5 // 2 = 2
% Modulus (remainder) 5 % 2 = 1
** Exponentiation (power) 5 ** 2 = 25
2. Comparison (Relational) Operators

These operators compare two values and return a Boolean value (either True/1 or False/0).

Operator Description Example
== Equal to 5 == 5
!= Not equal to 5 != 3
> Greater than 5 > 3
< Less than 3 < 5
>= Greater than or equal to 5 >= 5
<= Less than or equal to 3 <= 5
3. Logical Operators

These operators perform logical operations and return a Boolean value (either True/1 or False/0).

Operator Description Example
and Returns True if both operands are true A and B
or Returns True if at least one operand is true A or B
not Returns the negation of the operand not A/ not B
4. Assignment Operators

These operators are used to assign values to variables and can also be combined with arithmetic operations.

Operator Description Example
= Simple assignment x = 5
+= Add and assign x += 3 ( means  x = x + 3)
-= Subtract and assign x -= 3
*= Multiply and assign x *= 3
/= Divide and assign x /= 3
//= Floor divide and assign x //= 3
%= Modulus and assign x %= 3
**= Exponentiate and assign x **= 3
5. Bitwise Operators

These operators perform bit-level internal operations on integers.

Operator Description Example
& Bitwise AND 5 & 3
` ` Bitwise OR
^ Bitwise XOR 5 ^ 3
~ Bitwise NOT (inverts bits) ~5
<< Left shift (shift bits to the left) 5 << 1
>> Right shift (shift bits to the right) 5 >> 1
6. Identity Operators

These operators check if two objects refer to the same memory location.

Operator Description Example
is Returns True if both variables point to the same object x is y
is not Returns True if both variables point to different objects x is not y
7. Membership Operators

These operators are used to check if a value or variable is found in a sequence (like a list, string, or tuple).

Operator Description Example
in Returns True if a value is found in a sequence 'a' in 'apple'
not in Returns True if a value is not found in a sequence 'x' not in 'apple'
8. Ternary/Conditional Operator
This is a shorthand for conditional expressions (if-else).
result = x if x>y else y
9. Special Operators
  • is  ==:
    • Here, ‘is’ checks if two variables refer to the same object in memory, and ‘==’ checks if two variables have the same value.
    • For example – 
x = [10, 20, 30]
y = [10, 20, 30]
print(x == y)  # Output: – True (because same values)
print(a is b)  # Output: – False (because different objects)

Operator Precedence in Python

  • Operator precedence determines the order in which operations are performed.
  • In an expression, operators with higher precedence are evaluated first.
Operator Description
() Parentheses
** Exponentiation
+x, -x, ~x Unary plus, minus, NOT
*, /, //, % Multiplication, division, floor division, modulus
+, - Addition, subtraction
<<, >> Bitwise shift
& Bitwise AND
^ Bitwise XOR
` `
in, not in, is, is not, ==, !=, <, <=, >, >= Comparisons, identity, membership operators
not Logical NOT
and Logical AND
or Logical OR

Loading

Categories: Python Theory

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.