Topic4:- Operators in Python


What are Operators in Python

Operators are symbols or special characters that perform operations on variables and values.
For example:

a = 15
b = 5
print(a + b)   # Output: 120

Here, + is an operator that adds two numbers.

Operators
Python Operators



Types of Basic Operators in Python

Python operators are broadly classified as:

  1. Arithmetic Operators

  2. Comparison (Relational) Operators

  3. Logical Operators

  4. Assignment Operators

  5. Bitwise Operators

  6. Membership Operators

  7. Identity Operators

1️⃣ Arithmetic Operators

Used for mathematical operations.

Operator Description    Example        Output
+            Addition 10 + 5        15
-           Subtraction 10 - 5        5
*          Multiplication 10 * 5        50
/          Division 10 / 5        2.0
%         Modulus (remainder) 10 % 3        1
**         Exponentiation 2 ** 3        8
//         Floor Division 10 // 3        3

📘 Example:

a = 10
b = 3
print(a + b)   # 13
print(a % b)   # 1
print(a ** b)  # 1000

2️⃣ Comparison Operators

Used to compare two values; returns True or False.


Operator
 Meaning         Example      Output
==         Equal to   10 == 5      False
!=                  Not equal to  10 != 5      True
>        Greater than  10 > 5      True
<        Less than  10 < 5       False
>=       Greater than or equal  10 >= 5      True
<=       Less than or equal  10 <= 5      False

3️⃣ Logical Operators

Used to combine conditional statements.

Operator Meaning Example    Output
and                 True if both are True        (10 > 5) and (5 < 2)     False
or                 True if at least one is True        (10 > 5) or (5 < 2)    True
not                 Reverses True/False         not(10 > 5)    False
 4️⃣ Assignment Operators

Used to assign values to variables.

Operator           Example      Equivalent
=      a = 5       Assign 5 to a
+=      a += 2     a = a + 2
-=      a -= 2     a = a - 2
*=      a *= 2     a = a * 2
/=      a /= 2     a = a / 2
%=      a %= 2     a = a % 2

5️⃣ Bitwise Operators

Used to perform bit-level operations.

Operator   Name      Example       Binary Result
&       AND     5 & 3     1
      `        OR       `5
^       XOR     5 ^ 3     6
~       NOT     ~5     -6
<<      Left Shift     5 << 1     10
>>      Right Shift 5   >> 1       2

6️⃣ Membership Operators

Used to test if a value is present in a sequence (like list, string, etc.)

Operator           Example        Output
in           'a' in 'apple'        True
not in                  'x' not in 'apple'              True

7️⃣ Identity Operators

Used to compare memory locations.

Operator                   Example Output
is           a is b                        True if same object
is not            a is not b                        True if different objects

📘 Example:

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

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

Summary 

Python operators are symbols that perform operations on variables and values. They include arithmetic, comparison, logical, assignment, bitwise, membership, and identity operators, which together enable powerful and flexible program logic.


Comments

Popular posts from this blog

Topic1 :- What is Python Programming?

Topic2: -Why Python?

Topic7: What is Numpy?