Python Operators in Hindi — Complete Guide with Examples for Beginners | (Arithmetic, Comparison, Logical, Bitwise)
Python Operators in Hindi (Complete Guide)
Python आज दुनिया की सबसे लोकप्रिय programming languages में से एक है। इसका उपयोग Web Development, Data Science, Artificial Intelligence, Machine Learning, Automation, Cyber Security और Software Development जैसे कई क्षेत्रों में किया जाता है। लेकिन किसी भी Python Program को सही तरीके से लिखने के लिए सबसे पहले Operators को समझना बहुत जरूरी होता है।
जब भी हम किसी Program में दो numbers को जोड़ते हैं, किसी value की तुलना करते हैं, conditions check करते हैं या किसी variable में नया value store करते हैं, तब हम Operators का उपयोग करते हैं। इसलिए Operators Python की सबसे महत्वपूर्ण fundamental concepts में से एक हैं।
अगर आपने अभी Python सीखना शुरू किया है, तो यह tutorial आपके लिए एक मजबूत foundation तैयार करेगा। और यदि आप Python Interview की तैयारी कर रहे हैं, तब भी यह topic आपके लिए बेहद महत्वपूर्ण है क्योंकि लगभग हर Python Interview में Operators से जुड़े सवाल पूछे जाते हैं।
इस tutorial में हम Python Operators को आसान भाषा में समझेंगे। प्रत्येक Operator के साथ Practical Examples, Real-world Use Cases और Interview Tips भी दिए जाएंगे ताकि आपको केवल theory ही नहीं बल्कि उसका वास्तविक उपयोग भी समझ आए।
Operator क्या होता है?
Simple भाषा में कहें तो, Operator एक symbol होता है जो किसी operation को perform करने के लिए use होता है। जैसे अगर आपको दो numbers जोड़ने हैं, तो आप + operator use करेंगे।
a = 10
b = 5
print(a + b) # Output: 15यहाँ + एक operator है, और a, b operands हैं।
Python में मुख्य रूप से 7 प्रकार के operators होते हैं:
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
- Identity Operators
- Membership Operators
आज हम Part 1 में पहले दो types — Arithmetic और Comparison Operators — detail में देखेंगे।
1. Arithmetic Operators (गणितीय Operators)
Arithmetic Operators का इस्तेमाल basic math calculations के लिए होता है — जोड़, घटाव, गुणा, भाग वगैरह।
List of Arithmetic Operators
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 5 / 2 | 2.5 |
// | Floor Division | 5 // 2 | 2 |
% | Modulus | 5 % 2 | 1 |
** | Exponentiation | 5 ** 2 | 25 |
Code Example
a = 10
b = 3
print("Addition:", a + b) # 13
print("Subtraction:", a - b) # 7
print("Multiplication:", a * b) # 30
print("Division:", a / b) # 3.333...
print("Floor Division:", a // b) # 3
print("Modulus:", a % b) # 1
print("Exponent:", a ** b) # 1000सबसे ज्यादा confuse करने वाले Operators
/ vs // — बहुत सारे beginners यहाँ confuse होते हैं।
/हमेशा float value return करता है (decimal के साथ)//हमेशा floor value return करता है (decimal हटा कर, नीचे की तरफ round करके)
print(7 / 2) # 3.5
print(7 // 2) # 3
print(-7 // 2) # -4 (floor की तरफ जाता है, ऊपर नहीं)यह negative numbers वाला case अक्सर interview में पूछा जाता है, इसे याद रखिए।
Modulus Operator (%) का Real Use
Modulus operator सबसे ज्यादा use होता है यह check करने के लिए कि कोई number even है या odd।
num = 7
if num % 2 == 0:
print("Even number")
else:
print("Odd number")यह पैटर्न आपको almost हर Python program में दिखेगा — चाहे वो loops हों, या data filtering।
2. Comparison Operators (तुलना करने वाले Operators)
Comparison Operators का काम है दो values को compare करना, और result हमेशा Boolean (True या False) होता है।
List of Comparison Operators
| Operator | Name | 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 |
Code Example
x = 10
y = 20
print(x == y) # False
print(x != y) # True
print(x > y) # False
print(x < y) # True
print(x >= 10) # True
print(y <= 15) # Falseसबसे बड़ी Beginner Mistake
Python सीखने वाले ज्यादातर students = और == में गलती करते हैं।
# गलत तरीका (यह assignment है, comparison नहीं)
if x = 10:
print("Equal")ऊपर वाला code error देगा, क्योंकि = assignment operator है, comparison operator नहीं। सही तरीका यह है:
# सही तरीका
if x == 10:
print("Equal")यह गलती हर beginner करता है, इसलिए ध्यान से practice कीजिए।
Real-World Example: Comparison Operators का Use
मान लीजिए आप एक login system बना रहे हैं, जहाँ user की age check करनी है कि वो 18 साल से ऊपर है या नहीं:
age = 20
if age >= 18:
print("आप वोट डाल सकते हैं")
else:
print("आप अभी वोट डालने के योग्य नहीं हैं")यह एक बहुत common real-life use case है जो comparison operators को समझने में मदद करता है।
Part 2
इस Part में हम Cover करेंगे:
- Logical Operators
- Assignment Operators
- Bitwise Operators
- Real-world Examples
- Common Mistakes
- Interview Tips
Logical Operators in Python
Logical Operators का उपयोग एक से अधिक Conditions को Combine करने के लिए किया जाता है। जब Program में कई Conditions को एक साथ Check करना होता है, तब Logical Operators सबसे महत्वपूर्ण भूमिका निभाते हैं।
Python में मुख्य रूप से तीन Logical Operators होते हैं—
-
and -
or -
not
इनका उपयोग सबसे अधिक if, elif, while और Decision Making Programs में किया जाता है।
1. AND Operator (and)
and Operator तभी True Return करता है जब दोनों Conditions True हों।
यदि एक भी Condition False हो जाए, तो Result False होगा।
Example
age = 22
has_license = True
print(age >= 18 and has_license)
Output
True
दूसरा Example
age = 16
has_license = True
print(age >= 18 and has_license)
Output
False
Real-world Example
Driving License Verification
यदि किसी व्यक्ति की उम्र 18 वर्ष या उससे अधिक है और उसके पास Driving License भी है, तभी उसे वाहन चलाने की अनुमति मिलेगी।
2. OR Operator (or)
or Operator तब True Return करता है जब कम से कम एक Condition True हो।
Example
marks = 45
sports_quota = True
print(marks >= 50 or sports_quota)
Output
True
यहाँ Marks कम हैं लेकिन Sports Quota उपलब्ध है, इसलिए Result True आया।
दूसरा Example
print(False or False)
Output
False
Real-world Example
College Admission में Student को Admission मिल सकता है यदि—
- Entrance Exam Clear हो
- या Sports Quota हो
3. NOT Operator (not)
not किसी भी Boolean Value को उल्टा (Reverse) कर देता है।
Example
logged_in = True
print(not logged_in)
Output
False
दूसरा Example
print(not False)
Output
True
Real-world Example
Website Login System
यदि User Login नहीं है (not logged_in) तो Login Page Show किया जाता है।
Summary of Logical Operators
| Operator | Meaning | Example |
|---|---|---|
| and | दोनों Conditions True | a > 5 and b < 10 |
| or | कोई एक Condition True | a > 5 or b < 10 |
| not | Boolean उल्टा करता है | not True |
बहुत सारे beginners And / Or की जगह गलती से &, | इस्तेमाल कर लेते हैं (जो असल में Bitwise Operators हैं, आगे इसी part में पढ़ेंगे)।
# गलत (यह bitwise है, boolean logic के लिए recommended नहीं)
if age >= 18 & has_id:
...
# सही
if age >= 18 and has_id:
...Boolean conditions के लिए हमेशा and, or, not का ही इस्तेमाल करें।
Assignment Operators (Assignment वाले Operators)
Assignment Operators का इस्तेमाल किसी variable में value assign (store) करने के लिए होता है। साथ ही, यह shortcut तरीके से calculation करके भी value update कर सकते हैं।
List of Assignment Operators
| Operator | Example | Same As |
|---|---|---|
= | x = 5 | x = 5 |
+= | 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 |
Code Example
x = 10
x += 5 # x = 15
print(x)
x -= 3 # x = 12
print(x)
x *= 2 # x = 24
print(x)
x //= 5 # x = 4
print(x)Real-World Use Case: Counter बनाना
Assignment operators सबसे ज्यादा use होते हैं counters और loops में:
total_orders = 0
# हर नए order पर counter बढ़ाना
total_orders += 1
total_orders += 1
total_orders += 1
print("Total Orders:", total_orders) # 3
अगर आप e-commerce या inventory जैसे projects बना रहे हैं, तो यह pattern हर जगह मिलेगा — जैसे cart items count करना, stock घटाना वगैरह।
Bitwise Operators in Python
Bitwise Operators Binary Numbers (0 और 1) पर काम करते हैं।
शुरुआत में ये थोड़े कठिन लग सकते हैं, लेकिन System Programming, Networking, Cryptography और Performance Optimization में इनका उपयोग होता है।
Python में मुख्य Bitwise Operators हैं—
- &
- |
- ^
- ~
- <<
Bitwise AND (&)
a = 5
b = 3
print(a & b)
Output
1
Bitwise OR (|)
print(5 | 3)
Output
7
Bitwise XOR (^)
print(5 ^ 3)
Output
6
Bitwise NOT (~)
print(~5)
Output
-6
Left Shift (<<)
print(5 << 1)
Output
10
Right Shift (>>)
print(10 >> 1)
Output
5
Real-world Uses of Bitwise Operators
Bitwise Operators का उपयोग निम्न क्षेत्रों में किया जाता है—
- Operating Systems
- Embedded Systems
- Image Processing
- Network Programming
- Cryptography
- High Performance Applications
यदि आप केवल Python Beginner हैं, तो Bitwise Operators को Basic Level तक समझना पर्याप्त है।
Common Mistakes Beginners Make
Confusing = with ==
गलत
if age = 18:
सही
if age == 18:
Using and instead of &
Logical Programs में हमेशा and उपयोग करें।
Bitwise Calculations में & उपयोग करें।
Forgetting Operator Precedence
गलत तरीके से Brackets न लगाने पर Result बदल सकता है।
बेहतर है कि Complex Expressions में Parentheses का उपयोग करें।
Interview Tips
Python Interviews में अक्सर ये Questions पूछे जाते हैं—
Q1. Difference between and and &?
-
and→ Logical Operator -
&→ Bitwise Operator
Q2. Difference between = and +=?
-
=नई Value Assign करता है। -
+=Existing Value में Addition करके Update करता है।
Q3. Why are Assignment Operators useful?
क्योंकि ये Code को Short, Readable और Efficient बनाते हैं।
Part 3
Membership Operators (Membership Operators)
Membership Operators का उपयोग यह जांचने के लिए किया जाता है कि कोई Value किसी Collection (List, Tuple, Set, Dictionary, String आदि) के अंदर मौजूद है या नहीं।
List of Membership Operators
| Operator | काम |
|---|---|
in | अगर value sequence में मौजूद हो तो True |
not in | अगर value sequence में मौजूद न हो तो True |
Code Example
fruits = ["apple", "banana", "mango"]
print("apple" in fruits) # True
print("orange" in fruits) # False
print("orange" not in fruits) # TrueString के साथ Example
name = "Ankit Kumar"
print("Ankit" in name) # True
print("xyz" in name) # FalseReal-World Use Case
मान लीजिए आप check करना चाहते हैं कि कोई user की email valid domain से आई है या नहीं:
allowed_domains = ["gmail.com", "outlook.com", "yahoo.com"]
email = "ankit@gmail.com"
domain = email.split("@")[1]
if domain in allowed_domains:
print("Valid email domain ✅")
else:
print("Invalid email domain ❌")यह pattern form validation, signup systems, और filtering logic में बहुत काम आता है।
Real-World Examples — सभी Operators एक साथ
चलिए एक practical उदाहरण देखते हैं जहाँ एक साथ कई operators use होते हैं — एक simple Discount Calculator:
price = 1500
is_member = True
coupon_code = "SAVE10"
valid_coupons = ["SAVE10", "SAVE20", "FESTIVE50"]
discount = 0
# Membership operator
if coupon_code in valid_coupons:
discount += 10 # Assignment operator
# Logical + Comparison operator
if is_member and price > 1000:
discount += 5
final_price = price - (price * discount / 100) # Arithmetic operators
print(f"Discount: {discount}%")
print(f"Final Price: ₹{final_price}")Output:
Discount: 15%
Final Price: ₹1275.0यह example दिखाता है कि real projects में operators अकेले नहीं, बल्कि एक साथ मिलकर काम करते हैं।
Interview Questions on Python Operators
अगर आप Python interviews की तैयारी कर रहे हैं, तो यह questions जरूर practice करें:
/और//में क्या फर्क है?
/float division देता है,//floor division देता है (decimal हटाकर नीचे की तरफ round करता है)।==औरisमें क्या अंतर है?
==value compare करता है,ismemory location (identity) compare करता है।- Python में
andऔर&एक जैसे हैं क्या?
नहीं।andlogical operator है (boolean values के लिए), जबकि&bitwise operator है (bit-level operations के लिए)। -7 // 2का output क्या होगा और क्यों?
Output होगा-4, क्योंकि floor division हमेशा नीचे की तरफ (negative infinity की दिशा में) round करता है।- Python में short-circuit evaluation क्या है?
जबandमें पहली condition False हो, तो Python दूसरी condition check ही नहीं करता। इसी तरहorमें अगर पहली condition True हो, तो दूसरी check नहीं होती। इससे performance बेहतर होती है। inoperator dictionary में कैसे काम करता है?
Dictionary मेंinby default keys को check करता है, values को नहीं।
d = {"name": "Ankit"}
print("name" in d) # True
print("Ankit" in d) # False- Chained comparison operators क्या होते हैं?
Python में आप एक साथ कई comparisons chain कर सकते हैं:
x = 5
print(1 < x < 10) # TrueCommon Mistakes जो Beginners करते हैं
=और==में confuse होना — Assignment के लिए=, comparison के लिए==इस्तेमाल करें।- Boolean logic में
and/orकी जगह&/|इस्तेमाल करना — यह अलग-अलग purpose के operators हैं। isको value comparison के लिए इस्तेमाल करना — values compare करने के लिए हमेशा==इस्तेमाल करें,isसिर्फ identity check के लिए है।- Floor division को समझे बिना negative numbers पर इस्तेमाल करना — याद रखें, यह हमेशा नीचे की तरफ round करता है, zero की तरफ नहीं।
- Operator precedence को नजरअंदाज़ करना — जैसे
2 + 3 * 4का output20नहीं बल्कि14होगा, क्योंकि multiplication पहले होता है। Confusion से बचने के लिए brackets()का इस्तेमाल करें।
Best Practices
- Readability के लिए brackets का इस्तेमाल करें — भले ही operator precedence सही हो, complex expressions में
()लगाना code को समझने में आसान बनाता है। - Boolean checks के लिए हमेशा
and,or,notइस्तेमाल करें, bitwise operators नहीं (जब तक specifically bit-level काम न हो)। Nonecheck करने के लिएis Noneइस्तेमाल करें,== Noneनहीं — यह Python की recommended (PEP 8) style है।
if value is None:
print("Value is empty")
- Membership check (
in) को loops से बेहतर समझें — जहाँ possible हो,inoperator इस्तेमाल करें बजाय manual loop लिखने के। - Assignment operators (
+=,-=आदि) का इस्तेमाल code को concise बनाने के लिए करें, लेकिन readability से समझौता न करें।
FAQs (Frequently Asked Questions)
1. Python में कितने प्रकार के operators होते हैं?
Python में मुख्य रूप से 7 प्रकार के operators होते हैं — Arithmetic, Comparison, Logical, Assignment, Bitwise, Identity, और Membership Operators।
हर Operator का उपयोग अलग-अलग Programming Situations में किया जाता है।
2. Python में / और // में क्या अंतर है?
/ operator हमेशा float (decimal) value return करता है, जबकि // operator floor division करके decimal हटा देता है और नीचे की तरफ round करता है।
3. == और is operator में क्या फर्क है?
== दो values को compare करता है, जबकि is यह check करता है कि दोनों variables memory में same object को point कर रहे हैं या नहीं।
4. Python में Logical Operators कौन-कौन से हैं?
Python में तीन Logical Operators होते हैं — and, or, और not।
5. Bitwise Operators का इस्तेमाल कब किया जाता है?
Bitwise Operators का इस्तेमाल तब होता है जब हमें numbers पर bit-level operations करने हों — जैसे low-level programming, flags manage करना, या performance optimization।
6. Python में Identity Operators क्यों जरूरी हैं?
Identity Operators (is, is not) यह पता करने में मदद करते हैं कि दो variables एक ही memory location को point कर रहे हैं या अलग-अलग objects हैं — जो debugging और mutable objects समझने में उपयोगी है।
7. Membership Operators किन data types पर काम करते हैं?
Membership Operators (in, not in) list, tuple, string, set, और dictionary जैसे sequences पर काम करते हैं।
8. and और & में क्या अंतर है?
and एक logical operator है जो boolean values (True/False) पर काम करता है, जबकि & एक bitwise operator है जो numbers के individual bits पर काम करता है।
9. Assignment Operators code को कैसे बेहतर बनाते हैं?
Assignment Operators (+=, -=, *= आदि) code को छोटा और readable बनाते हैं, जैसे x = x + 1 की जगह x += 1 लिखना।
10. What should I learn after Python Operators?
Python Operators के बाद आपको निम्न Topics सीखने चाहिए—
- Python Keywords
- Data Types
- Type Casting
- Input & Output
- Conditional Statements
- Loops
- Functions
- Lists
- Dictionaries
इन्हें सीखने से आपकी Python Foundation और मजबूत होगी।
Common Interview Questions
यदि आप Python Interview की तैयारी कर रहे हैं, तो नीचे दिए गए Questions की Practice जरूर करें—
- What are Python Operators?
- Explain Arithmetic Operators with examples.
-
Difference between
=and==. -
Difference between
==andis. - Explain Logical Operators.
- What are Assignment Operators?
- What is Operator Precedence?
- Explain Membership Operators.
- What are Bitwise Operators?
-
Which Operators are used in Machine Learning projects?
Conclusion
Python Operators, programming सीखने की नींव हैं। चाहे आप एक simple calculator बना रहे हों, या एक पूरा e-commerce system जैसे AnguliMart — हर जगह Arithmetic, Comparison, Logical, Assignment, Bitwise, Identity, और Membership Operators किसी न किसी रूप में इस्तेमाल होते हैं।
अगर आपने यह पूरा article ध्यान से पढ़ा और examples खुद practice किए, तो अब आप confidently कह सकते हैं कि आपको Python Operators की अच्छी समझ आ गई है। अगला कदम है — इन्हें conditions (if-else) और loops के साथ मिलाकर real programs बनाना, जिससे आपकी concepts और मजबूत होंगी।
Practice करते रहिए, और अगर कोई doubt हो तो comment में जरूर पूछें!
अगर आपने अभी Python सीखना शुरू किया है, तो पहले हमारी Python Kya Hai – Beginner Guide वाली post जरूर पढ़ें।
Variables की बेसिक समझ के लिए Python Variables in Hindi पढ़ना भी फायदेमंद रहेगा।
Input लेना और Output दिखाना सीखने के लिए Python Input and Output in Hindi वाली guide जरूर देखें।
Interview की तैयारी के लिए हमारी Top 100 Python Interview Questions in Hindi पढ़ें।
कोई टिप्पणी नहीं:
एक टिप्पणी भेजें