Python Operators Overview for Data Scientists: A How-To Guide

Python ek powerful aur flexible programming language hai jo data science me widely use hoti hai. Data scientists ko data manipulate aur analyze karne ke liye operators ki achi samajh honi chahiye. Yeh blog post aapko Python ke various operators ke types aur unhe use karne ka tarika easy words me step-by-step batayegi.

Operators in Python

Operators kuch specific operations ko perform karne ke liye use hote hain. Python me several types ke operators hote hain:

  1. Arithmetic Operators

  2. Comparison Operators

  3. Logical Operators

  4. Assignment Operators

  5. Bitwise Operators

  6. Membership Operators

  7. Identity Operators

Chaliye inhe detail me samjhte hain.

1. Arithmetic Operators

Arithmetic operators mathematical operations ko perform karte hain.

  • Addition (+): Do numbers ko add karta hai.

      a = 5
      b = 3
      result = a + b
      print(result)  # Output: 8
    
  • Subtraction (-): Ek number ko dusre se subtract karta hai.

      result = a - b
      print(result)  # Output: 2
    
  • Multiplication (*): Do numbers ko multiply karta hai.

      result = a * b
      print(result)  # Output: 15
    
  • Division (/): Ek number ko dusre se divide karta hai.

      result = a / b
      print(result)  # Output: 1.6667
    
  • Modulus (%): Ek number ko dusre se divide karne ke baad remainder return karta hai.

      result = a % b
      print(result)  # Output: 2
    
  • Exponentiation (**): Ek number ko dusre number ke power me raise karta hai.

      result = a ** b
      print(result)  # Output: 125
    
  • Floor Division (//): Ek number ko dusre se divide karta hai aur integer quotient return karta hai.

      result = a // b
      print(result)  # Output: 1
    

2. Comparison Operators

Comparison operators do values ko compare karte hain aur boolean result return karte hain.

  • Equal to (==): Agar do values equal hain to True return karta hai.

      result = (a == b)
      print(result)  # Output: False
    
  • Not equal to (!=): Agar do values equal nahi hain to True return karta hai.

      result = (a != b)
      print(result)  # Output: True
    
  • Greater than (>): Agar pehla value dusre se bada hai to True return karta hai.

      result = (a > b)
      print(result)  # Output: True
    
  • Less than (<): Agar pehla value dusre se chhota hai to True return karta hai.

      result = (a < b)
      print(result)  # Output: False
    
  • Greater than or equal to (>=): Agar pehla value dusre se bada ya equal hai to True return karta hai.

      result = (a >= b)
      print(result)  # Output: True
    
  • Less than or equal to (<=): Agar pehla value dusre se chhota ya equal hai to True return karta hai.

      result = (a <= b)
      print(result)  # Output: False
    

3. Logical Operators

Logical operators multiple conditions ko combine karte hain aur boolean result return karte hain.

  • AND (and): Dono conditions True hain to hi True return karta hai.

      result = (a > 2 and b < 4)
      print(result)  # Output: True
    
  • OR (or): Agar koi ek condition bhi True hai to True return karta hai.

      result = (a > 2 or b > 4)
      print(result)  # Output: True
    
  • NOT (not): Condition ko invert karta hai.

      result = not (a > 2)
      print(result)  # Output: False
    

4. Assignment Operators

Assignment operators variables ko values assign karte hain.

  • Assignment (=): Ek value ko variable me assign karta hai.

      c = 10
    
  • Add and assign (+=): Variable ke current value me add karta hai aur assign karta hai.

      c += 5
      print(c)  # Output: 15
    
  • Subtract and assign (-=): Variable ke current value me subtract karta hai aur assign karta hai.

      c -= 5
      print(c)  # Output: 10
    
  • Multiply and assign (*=): Variable ke current value me multiply karta hai aur assign karta hai.

      c *= 2
      print(c)  # Output: 20
    
  • Divide and assign (/=): Variable ke current value me divide karta hai aur assign karta hai.

      c /= 2
      print(c)  # Output: 10.0
    

5. Bitwise Operators

Bitwise operators binary numbers ke bits pe operate karte hain.

  • AND (&): Dono bits 1 hain to 1 return karta hai.

      result = a & b
      print(result)
    
  • OR (|): Koi bhi ek bit 1 hai to 1 return karta hai.

      result = a | b
      print(result)
    
  • NOT (~): Bits ko invert karta hai.

      result = ~a
      print(result)
    
  • XOR (^): Dono bits alag hain to 1 return karta hai.

      result = a ^ b
      print(result)
    
  • Left Shift (<<): Bits ko left shift karta hai.

      result = a << 1
      print(result)
    
  • Right Shift (>>): Bits ko right shift karta hai.

      result = a >> 1
      print(result)
    

6. Membership Operators

Membership operators sequence me element ke presence ko check karte hain.

  • In (in): Agar element sequence me present hai to True return karta hai.

      fruits = ["apple", "banana", "cherry"]
      result = "banana" in fruits
      print(result)  # Output: True
    
  • Not In (not in): Agar element sequence me present nahi hai to True return karta hai.

      result = "grape" not in fruits
      print(result)  # Output: True
    

7. Identity Operators

Identity operators do objects ke same memory location ko check karte hain.

  • Is (is): Agar do objects same memory location share karte hain to True return karta hai.

      x = [1, 2, 3]
      y = x
      result = (x is y)
      print(result)  # Output: True
    
  • Is Not (is not): Agar do objects different memory location share karte hain to True return karta hai.

      z = [1, 2, 3]
      result = (x is not z)
      print(result)  # Output: True
    

Conclusion

Yeh blog post Python ke different types ke operators aur unke usage ko cover karti hai, jo ek data scientist ke liye zaroori hai. In operators ko effectively use karke aap data ko manipulate aur analyze kar sakte hain. Yeh basic guide beginners ke liye kaafi helpful hogi jo Python me abhi-abhi coding start kar rahe hain.

Happy Coding!