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 Class | Python Symbols | મુખ્ય Behavioral Property | PocketMoney Code |
|---|---|---|---|
| Arithmetic | /, //, %, ** | / float આપે છે, // નીચે truncate કરે છે, ** powers વધારે છે. | cost = 50 // 3 |
| Comparison | ==, !=, >, < | સંરચનાત્મક values evaluate કરે છે અને boolean આપે છે. | is_equal = (30 == 30.0) |
| Logical | and, or, not | short circuit logic થી conditions evaluate કરે છે. | valid = True and False |
| Identity | is, is not | તપાસે છે કે references બિલકુલ એ જ memory તરફ ઈશારો કરે છે. | same = x is y |
| Membership | in, not in | structures ની અંદર 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 શું છે?
- -2
- -3
- -2.75
- 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) વચ્ચેનો મહત્ત્વનો તફાવત શું છે?
- એ સંપૂર્ણપણે એકસરખા અને અદલાબદલી કરવા યોગ્ય symbols છે.
- == તપાસે છે કે values બરાબર છે, જ્યારે 'is' તપાસે છે કે variables memory block locations માં બિલકુલ એ જ address તરફ ઈશારો કરે છે.
- 'is' mathematical calculations ચલાવે છે, જ્યારે == logical boolean conditions process કરે છે.
- == માત્ર 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 ઈશારો કરે છે!