Operators: Arithmetic, Assignment, Comparison, Logical, Identity/Membership

Operators Python के functional इंजन हैं, data के बुनियादी टुकड़ों को dynamic decisions, mathematical balances, और membership filters में बदलते हुए।

12 min read · 12 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Cash Register के गणितीय रहस्य

आपके Semester 1 BCA104 C labs में, 5 / 2 जैसे दो integers को भाग देना मशहूर रूप से decimal काटकर एक सपाट whole number 2 देता था। पर अगर आप वही बिल्कुल calculation Python में करें, यह आपको सहजता से एक fractional decimal 2.5 देता है। Python गणित का बुनियादी व्याकरण क्यों बदलता है, और यह कौन से ख़ास tools देता है ताकि values inspect करें, logical conditions जाँचें, या लंबे loops के बिना list records के अंदर data खोजें?

Theory

Mechanical Assembly Line

एक factory conveyor belt पर बढ़ते raw materials की कल्पना कीजिए। Variables items रखने वाले containers हैं, पर operators line के साथ भारी machinery tools हैं। कुछ tools टुकड़े एक साथ weld करते हैं (arithmetic addition), कुछ जाँचते हैं कि क्या एक product एक quality checklist box से मेल खाता है (comparison), और कुछ security scanners की तरह काम करते हैं जो एक approved shipping manifest पर न मिले items को filter करते हैं (membership)।

Theory

Operator Ecosystem औपचारिक रूप से

एक operator एक ख़ास symbol है जो Python interpreter को data tokens (जिन्हें operands कहते हैं) पर ख़ास mathematical, logical, या relational transformations करने को कहता है। Python इन symbols को साफ़ संरचनात्मक समूहों में classify करता है: mathematical calculations के लिए arithmetic operators, conditions test करने के लिए comparison operators, criteria जोड़ने के लिए logical operators, और identity verification (is) या collection membership (in) के लिए विशेष operators।

At a glance

Table 1: विशिष्ट processing features के साथ ज़रूरी Python operator श्रेणियाँ।

Operator ClassPython Symbolsमुख्य Behavioral PropertyPocketMoney Code
Arithmetic/, //, %, **/ float देता है, // नीचे truncate करता है, ** powers बढ़ाता है।cost = 50 // 3
Comparison==, !=, >, <संरचनात्मक values evaluate करता है और boolean देता है।is_equal = (30 == 30.0)
Logicaland, or, notshort circuit logic से conditions evaluate करता है।valid = True and False
Identityis, is notजाँचता है कि references बिल्कुल एक ही memory की ओर इशारा करते हैं।same = x is y
Membershipin, not instructures के अंदर target matching elements के लिए scan करता है।found = "samosa" in inventory

Theory

Worked Example: रोज़ की Transactions का Audit

आइए इन operators को हमारे PocketMoney tracker में एक साथ काम करते देखें। हम floor division सँभालेंगे, values बनाम identical memory references test करेंगे, और membership checking tools इस्तेमाल करके एक group list scan करेंगे।

Practical

PocketMoney Operator Processing Engine

item_prices = [30, 15, 45, 10]

# Floor division vs standard division calculation
shares_floor = 10 // 4
shares_normal = 10 / 4

# Checking items using membership
samosa_tracked = 30 in item_prices

# Inspecting small integer identity mechanisms
a = 250
b = 250
identical_refs = a is b

print(shares_floor, shares_normal)
print(samosa_tracked)
print(identical_refs)

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Operator Output Results को ट्रेस करें

ऊपर के Python script का मन में विश्लेषण कीजिए। floor calculations, membership checks, और memory references के लिए कौन से बिल्कुल results print होंगे? click करने से पहले इसे ध्यान से ट्रेस कीजिए।

Show the answer

script output करेगी:

2 2.5

True

True

क्यों? Floor division 10 // 4 निकटतम whole integer 2 तक नीचे काटती है, जबकि standard division 2.5 देती है। integer 30 हमारी list के अंदर मौजूद है, तो in True देता है। Python छोटे integers को memory में cache करता है, मेल खाते छोटे numeric definitions के लिए a is b को True बनाते हुए।

Quiz

एक मानक Python university examination answer sheet में expression: result = -11 // 4 का बिल्कुल evaluation value क्या है?

  1. -2
  2. -3
  3. -2.75
  4. SyntaxError
Show the answer

-3

students अक्सर -2 अनुमान लगाते हैं, यह सोचते हुए कि Python बस decimals काट देता है। पर, floor division // छोटे negative integer की ओर नीचे round करता है। चूँकि -2.75 नीचे गिरता है, यह सख़्ती से -3 पर resolve होता है।

Quiz

assignment double equal sign (==) और identity keyword operator (is) के बीच अहम अंतर क्या है?

  1. वे पूरी तरह एक जैसे और अदल-बदल करने योग्य symbols हैं।
  2. == जाँचता है कि values बराबर हैं, जबकि 'is' जाँचता है कि variables memory block locations में बिल्कुल एक ही address की ओर इशारा करते हैं।
  3. 'is' mathematical calculations चलाता है, जबकि == logical boolean conditions process करता है।
  4. == सिर्फ़ strings पर काम करता है, जबकि 'is' सिर्फ़ numbers पर चलता है।
Show the answer

== जाँचता है कि values बराबर हैं, जबकि 'is' जाँचता है कि variables memory block locations में बिल्कुल एक ही address की ओर इशारा करते हैं।

value comparison operator == संरचनात्मक content मेल test करता है (जैसे 2.0 == 2 True है)। identity operator 'is' variable addresses के ज़रिए memory location reference identity जाँचता है, यह सुनिश्चित करते हुए कि वे बिल्कुल एक ही internal object साझा करते हैं।

Watch out

Classic जाल: Negative Floor Collapse

laboratory viva सवालों में सबसे बड़ा marks-गँवाने वाला जाल floor operations इस्तेमाल करके negative values को भाग देना है। जबकि 11 // 4 2 देता है, -11 // 4 चलाना -3 तक नीचे गिरता है। Python floor division हमेशा number line पर बाईं ओर negative infinity की ओर बढ़ता है, जो C-trained students को पूरी तरह चौंका देता है!

Theory

भविष्य के Semesters से जोड़ना

ये operations data frameworks filter करने के लिए आपकी नींव बनाते हैं। Semester 3 (BCA303) में, जब संरचनात्मक records से entries retrieve करते हैं, आप if 'Fraud' not in transaction_status: जैसे membership operators इस्तेमाल करेंगे ताकि गंदी nested sorting routines लिखे बिना दोषपूर्ण indices तुरंत drop करें।

Summary

Key takeaways

  • Standard division / Python में हमेशा एक floating point value देता है।
  • Floor division // संख्यात्मक values को निचली integer सीमा तक मजबूर करता है।
  • Comparison operators संरचनात्मक समानता evaluate करते हैं जबकि identity absolute address references जाँचता है।
  • Logical expressions symbols के बजाय and, or, और not जैसे word commands इस्तेमाल करके चलते हैं।
  • Membership markers collection lists के अंदर elements सीधे खोजने के लिए 'in' इस्तेमाल करते हैं।
  • Memory Hook: संरचनात्मक value बराबर करती है, पर identity इशारा करती है!

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Python Fundamentals

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati