Ad Code

✨🎆 Codex 1.0 PLACEMENT READY PROGRAM! 🎆✨

Get 75% Discount Early bird offer CLICK to JOIN CodeX 1.0 click

Operator Tutorials in Python, What is Operator in Python, Type of Operator in Python

Python Operators

Python Operators – Complete Tutorial

Operators are symbols used to perform operations on operands. Operands may be variables, constants, or literals. Python internally uses predefined methods such as __add__() for addition, __sub__() for subtraction, etc.


🔹 Types of Operators in Python

  1. Arithmetic Operators
  2. Relational / Conditional Operators
  3. Comparison Operators
  4. Assignment Operators
  5. Logical Operators
  6. Membership Operators
  7. Identity Operators
  8. Bitwise Operators

1️⃣ Arithmetic Operators

Used to perform mathematical operations.

  • + → Addition (also used for string concatenation)
  • - → Subtraction
  • * → Multiplication
  • / → True Division (returns float)
  • // → Floor Division (returns integer result)
  • % → Modulus (remainder)
  • ** → Exponent / Power

✔ Examples

a = 10

# True Division
b = a / 3
print(b)   # 3.3333

# Floor Division
b = a // 3
print(b)   # 3

# Power
b = a ** 2
print(b)   # 100

# Modulus
b = a % 3
print(b)   # 1

2️⃣ Relational (Conditional) Operators

Used inside conditions or loops. Always return True or False.

  • < → Less than
  • > → Greater than
  • <= → Less than or equal
  • >= → Greater than or equal
  • != → Not equal

3️⃣ Comparison Operator

Used to compare values of two variables. Python compares values, not memory addresses. To compare memory addresses, use the is operator.

✔ Example

x = 5
y = 15

z = (x == y)
print(z)    # False

4️⃣ Assignment Operators

Used to assign values to variables.

✔ Types

  • = → Simple assignment
  • += → a += 2 → a = a + 2
  • -= → a -= 3 → a = a - 3
  • *= → a *= 5 → a = a * 5
  • /= → a /= 4 → a = a / 4
  • %= → a %= 3 → a = a % 3
  • //= → a//=2 → a=a//2

✔ Example

a = 2
print(a)

a += 5
print(a)

a -= 3
print(a)

a *= 2
print(a)

a /= 2
print(a)

a %= 3
print(a)

5️⃣ Logical Operators

Used to combine multiple conditions.

  • and → Returns True only if all conditions are True
  • or → Returns True if any condition is True
  • not → Reverses boolean value

✔ Examples

mark = 500

# AND
s = mark >= 0 and mark <= 50
print(s)

# OR
s = mark >= 0 or mark <= 50
print(s)

# NOT
s = not(mark >= 0 or mark <= 50)
print(s)

6️⃣ Membership Operators

Used to check whether an element exists in a collection (List, Tuple, Dictionary, Set).

  • in → True if element exists
  • not in → True if element does not exist

✔ Example

X = [1, 2, 3]
print(3 in X)       # True
print(5 not in X)   # True

7️⃣ Identity Operators

Used to compare memory addresses.

  • is → True if two references point to the same object
  • is not → False if both references point to the same object

8️⃣ Bitwise Operators

Used to perform operations on binary numbers.

  • & → Binary AND
  • | → Binary OR
  • ^ → Binary XOR
  • ~ → One's complement
  • << → Binary Left Shift
  • >> → Binary Right Shift

✔ Examples

a = 60   # 0011 1100
b = 13   # 0000 1101

print(a & b)    # 12   → 0000 1100
print(a | b)    # 61   → 0011 1101
print(a ^ b)    # 49   → 0011 0001
print(~a)       # -61
print(a << 2)   # 240  → 1111 0000
print(a >> 2)   # 15   → 0000 1111

📚 ASSIGNMENTS

  1. WAP to convert feet to inches and inches to feet.
  2. WAP to enter total square feet and calculate how many 100 sq ft rooms can be made.
  3. WAP to input five items with their prices and display total amount with GST.
  4. WAP to print table of 5 using complex assignment operator.
  5. WAP to move a ball 5 steps up, 5 steps down, then 10 steps up and 15 steps down. Count total movement.

Post a Comment

1 Comments

  1. deependra singh jadaunNovember 4, 2020 at 1:19 PM

    sir ye binary ka concept smz nhi aya

    ReplyDelete

POST Answer of Questions and ASK to Doubt