Operators kya hote hai ?
Operators kuch symbols (Ex :- "+, -, *, /") hai jo programming me variables par kuch operations karte hai. Operators ka use calculations ya comparisons karne ke liye hota hai.
Python me mainly 7 types ke operators hote hai :-
- 1. Arithmetic Operators
- 2. Comparison (Relational) Operators
- 3. Assignment Operators
- 4. Logical Operators
- 5. Bitwise Operators
- 6. Membership Operators
- 7. Identity Operators
1. Arithmetic Operators
Ye operators mathematical calculations karne ke liye use hote hain. Operators: +, -, *, /, %, //, **
| Operator | Naam | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division | 10 / 3 | 3.333... |
| % | Modulus | 10 % 3 | 1 |
| // | Floor Division | 10 // 3 | 3 |
| ** | Exponentiation | 10 ** 3 | 1000 |
Example 1: Addition, Subtraction, Multiplication aur Division
Example 2: Modulus (%), Floor Division (//) aur Exponentiation (**)
2. Comparison (Relational) Operators
Ye operators do values ko compare karte hain aur result hamesha True ya False (Boolean) hota hai. Operators: ==, !=, >, <, >=, <=
| Operator | Matlab | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | True |
| != | Not equal to | 5 != 3 | True |
| > | Greater than | 5 > 3 | True |
| < | Less than | 5 < 3 | False |
| >= | Greater than or equal | 5 >= 5 | True |
| <= | Less than or equal | 5 <= 3 | False |
Example 1: Equal (==) aur Not Equal (!=)
Example 2: Greater / Less than Operators
3. Assignment Operators
Ye operators variable me value assign karne ke liye use hote hain. = simple assignment hai, baaki shorthand hain jaise +=, -=, *= etc.
| Operator | Example | Matlab |
|---|---|---|
| = | x = 5 | x me 5 assign karo |
| += | x += 3 | x = x + 3 |
| -= | x -= 3 | x = x - 3 |
| *= | x *= 3 | x = x * 3 |
| /= | x /= 3 | x = x / 3 |
| %= | x %= 3 | x = x % 3 |
| //= | x //= 3 | x = x // 3 |
| **= | x **= 3 | x = x ** 3 |
Example 1: Simple aur Compound Assignment (+=, -=)
Example 2: Multiply, Divide aur Power Assignment (*=, /=, **=)
4. Logical Operators
Ye operators multiple conditions ko combine karne ke liye use hote hain. Result hamesha True ya False hota hai. Operators: and, or, not
| Operator | Matlab | Example | Result |
|---|---|---|---|
| and | Dono conditions True hon tabhi True | True and False | False |
| or | Koi ek condition True ho toh True | True or False | True |
| not | Condition ko ulta kar deta hai | not True | False |
Example 1: and aur or Operator
Example 2: not Operator
5. Bitwise Operators
Ye operators binary level par (bits pe) kaam karte hain. Ye advanced programming aur low-level operations me use hote hain. Operators: &, |, ^, ~, <<, >>
| Operator | Naam | Example (a=5, b=3) | Result |
|---|---|---|---|
| & | AND | 5 & 3 | 1 |
| | | OR | 5 | 3 | 7 |
| ^ | XOR | 5 ^ 3 | 6 |
| ~ | NOT | ~5 | -6 |
| << | Left Shift | 5 << 1 | 10 |
| >> | Right Shift | 5 >> 1 | 2 |
Example 1: AND (&), OR (|) aur XOR (^)
Example 2: NOT (~), Left Shift (<<) aur Right Shift (>>)
6. Membership Operators
Ye operators check karte hain ki koi value sequence me hai ya nahi (jaise list, string, tuple, etc.). Operators: in, not in
| Operator | Matlab | Example | Result |
|---|---|---|---|
| in | Value present hai sequence me | "a" in "apple" | True |
| not in | Value present nahi hai sequence me | "z" not in "apple" | True |
Example 1: List me "in" operator
Example 2: String me "in" operator
7. Identity Operators
Ye operators check karte hain ki do variables same memory location ko point kar rahe hain ya nahi. Sirf values nahi, identity (object) compare hoti hai. Operators: is, is not
| Operator | Matlab | Example | Result |
|---|---|---|---|
| is | Dono same object hain? | a is b | True / False |
| is not | Dono alag objects hain? | a is not b | True / False |
Example 1: "is" Operator
Example 2: "is not" aur == vs is ka fark