Tuple: declaring, indexing, changing values, adding/removing data, tuple() method, count(), index()

Tuples Python ના locked collections છે, એક વાર બન્યા પછી, એમના elements પથ્થર પર લખાયેલા હોય છે, એક કાયમી data safety આપતાં જે raw lists guarantee કરી શકતી નથી.

10 min read · 12 cards · 3 checks

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


Theory

Tamper-Proof Record System

તમારા Semester 1 BCA104 C labs માં, જો તમે configuration settings કે transaction rules ના એક sequence ને lock કરવા માંગત જેથી કોઈ accidental code logic એમના પર ન લખી શકે, તમારે જટિલ pointer tricks કે constant macro definitions પર આધાર રાખવો પડત. Python માં, lists ખૂબ flexible છે કારણ કે એ runtime પર વધી અને shift થઈ શકે છે. પણ શું થાય છે જ્યારે તમે સંપૂર્ણ safety માંગો? તમે એવો એક ordered collection કેવી રીતે બનાવો છો જે બન્યાની ક્ષણથી ક્યારેય ન બદલવાની guarantee હોય?

Theory

Stamped Certificate

એક list ને એક હંગામી scratch-pad ની જેમ વિચારો જ્યાં તમે રોજના grocery expenses એક pencil થી લખો છો; તમે entries ભૂંસી શકો, items swap કરી શકો, કે જ્યારે માંગો lines ઉમેરી શકો. એક tuple, બીજી બાજુ, એક ઔપચારિક university graduation certificate ની જેમ છે જે permanent ink માં print અને એક laminated plastic pouch ની અંદર sealed છે. તમે data જોઈ શકો, roll number વાંચી શકો, અને graduation year verify કરી શકો, પણ એ certificate પર એક એકલો letter બદલવાનો કોઈ પણ પ્રયાસ system ને તોડી નાખશે.

Theory

Tuple ના પાયા ઔપચારિક રીતે

એક tuple parentheses () ની અંદર લપેટાયેલા elements નો એક ordered, immutable collection છે. કારણ કે tuples immutable છે, Python interpreter guarantee આપે છે કે એમના internal cell addresses initialization પછી ક્યારેય modify, delete, કે append કરી શકાતા નથી. Tuples duplicate elements support કરે છે અને mixed data types store કરી શકે છે. lists ની જ જેમ, એ values dynamically fetch કરવા ડાબેથી zero-based positive indexing અને જમણેથી negative indexing વાપરે છે.

At a glance

Table 1: Python tuples માટે મુખ્ય declaration syntaxes અને built-in inspection methods.

Operation PatternCode Syntax TemplateMemory પર Action/Impact
Single-Element Declarationsingleton = (50,)ફરજિયાત trailing comma Python ને કહે છે એ એક tuple છે, એક integer નહીં.
The tuple() Constructortuple(['a', 'b'])એક mutable sequence (જેમ એક list) ને એક immutable tuple માં બદલે છે.
Frequency Analysisrecord.count(value)target value ની ઘટનાઓ ગણતો એક integer return કરે છે.
Position Lookuprecord.index(value)index 0 થી શોધે છે અને value સાથે મેળ ખાતો પહેલો index return કરે છે.

Theory

Worked Example: System Configurations સુરક્ષિત કરવી

ચાલો જોઈએ કે આપણું PocketMoney system fixed regulatory information કેવી રીતે સંભાળે છે. આપણે currency rules રાખતું એક immutable tuple વ્યાખ્યાયિત કરીશું, એક item ને index થી જોઈશું, એક asset code ની ઘટનાઓ verify કરીશું, અને standard indirect conversion workaround દેખાડીશું જે tuple data બદલવા વપરાય છે જ્યારે updates બિલકુલ જરૂરી બને.

Practical

PocketMoney Immutable Token Governance

# Step 1: Declaring a rigid configuration tuple
currency_config = ("INR", "Rs.", "Digital Wallet", "INR")

# Step 2: Accessing specific index values
primary_code = currency_config[0]

# Step 3: Counting occurrences of a specific item
inr_count = currency_config.count("INR")

# Step 4: The Workaround, modifying data indirectly via type conversion
temp_list = list(currency_config)
temp_list[2] = "Smart Wallet"
currency_config = tuple(temp_list)

print("Primary Token:", primary_code)
print("Count of 'INR':", inr_count)
print("Updated Configuration:", currency_config)

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

Think first

Immutability Workaround ને ટ્રેસ કરો

ઉપરના script નું વિશ્લેષણ કરો. આપણે માત્ર index 2 બદલવા line 10 પર આપણા tuple ને એક list માં convert કેમ કરવું પડ્યું? currency_config ની અંતિમ state તરીકે શું print થશે?

Show the answer

script output કરશે:

Primary Token: INR

Count of 'INR': 2

Updated Configuration: ('INR', 'Rs.', 'Smart Wallet', 'INR')

કેમ? currency_config[2] = 'Smart Wallet' જેવા સીધા item reassignments Python દ્વારા block કરાય છે અને એક crash trigger કરે છે. એક tuple નો data સુરક્ષિત રીતે બદલવા, તમારે એને list() દ્વારા એક mutable list માં convert કરવું, target slot cell index update કરવું, અને પૂરા container ને tuple() ની અંદર ફરી લપેટવું પડશે.

Quiz

એક university laboratory examination માં આ statement ચલાવવાનું બિલકુલ પરિણામ શું છે: score = (100)?

  1. એ 1 ની length વાળું એક valid tuple બનાવે છે.
  2. એ value 100 રાખતું એક standard integer variable initialize કરે છે.
  3. એ એક તરત SyntaxError ફેંકે છે કારણ કે tuples માં single elements પ્રતિબંધિત છે.
  4. એ score નામનું એક empty list wrapper બનાવે છે.
Show the answer

એ value 100 રાખતું એક standard integer variable initialize કરે છે.

Parentheses mathematical grouping માટે પણ વપરાય છે. જો તમે parentheses ની અંદર એક single item વગર trailing comma લખો, Python statement ને એક સાદા data primitive (integer, float, કે string) તરીકે resolve કરે છે. એક સાચું single-element tuple બનાવવા, તમારે એક comma ઉમેરવો પડશે, જેમ: score = (100,).

Quiz

આ script chunk પર વિચાર કરો: records = (10, 20, 30) records[1] = 99 print(records)

  1. (10, 99, 30)
  2. (10, 20, 30)
  3. TypeError: 'tuple' object does not support item assignment
  4. SyntaxError: immutable sequence breach
Show the answer

TypeError: 'tuple' object does not support item assignment

Tuples સખ્તાઈથી immutable collections છે. કોઈ પણ code ની line જે એક individual element slot ને સીધું memory માં force-write કે overwrite કરવાનો પ્રયાસ કરે છે, Python runtime execution engine ને એક TypeError સાથે તરત અટકાવવાનું કારણ બને છે.

Watch out

Classic ફાંદો: Modification નો ભ્રમ

university theory papers માં સૌથી વારંવાર marks-ગુમાવતી ભૂલ એ માનવું છે કે એક tuple પર methods call કરવું કે variables ઉમેરવા બિલકુલ એ જ રીતે કામ કરે છે જેમ lists માટે. Tuples માં append(), insert(), extend(), કે pop() જેવી methods સંપૂર્ણપણે નથી! એમની પાસે માત્ર બે built-in methods છે: count() અને index(). જો તમે my_tuple.append(40) execute કરવાનો પ્રયાસ કરો, તમારી script કોઈ પણ પછીની logic line process કરતાં પહેલાં એક AttributeError સાથે crash થશે.

Theory

Collections ને Semester 3 સાથે જોડવા

Tuples relational applications માં મૂળ data return structure તરીકે કામ કરે છે. Semester 3 (BCA303) માં, જ્યારે તમારું Python back-end cursor.fetchone() જેવી commands વાપરીને એક external SQLite data table થી user login data fetch કરવા એક query trigger કરે છે, library એ ખાસ database record ને એક immutable tuple તરીકે return કરે છે. આ guarantee આપે છે કે તમારા core data fields સંપૂર્ણપણે locked અને corruption થી safe રહે જ્યારે એ તમારા software layers માં move કરે છે.

Summary

Key takeaways

  • Tuples ordered, immutable collection objects છે જે enclosing parentheses વાપરીને declare કરાય છે.
  • Immutability data sets ને program execution દરમિયાન accidental manipulation થી બચાવે છે.
  • Single-element tuples માં એમને standard expressions થી અલગ કરવા એક trailing comma હોવો જોઈએ.
  • Tuples data fetch કરવા standard zero-based indexing અને slicing operations support કરે છે.
  • tuple items બદલવા list() દ્વારા casting, list બદલવી, અને tuple() દ્વારા પાછું casting જોઈએ.
  • Memory Hook: Lists જગ્યાએ બદલાય છે; tuples શરૂથી અંત સુધી locked રહે છે!

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 Collections

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

Tuple: declaring, indexing, changing values, adding/removing data, tuple() method, count(), index() · Programming Skills · Gri-Learn