Python Tuple Methods

Tuple Methods

Tuple పై కొన్ని operations చేయడానికి Python మనకు built-in methods ఇస్తుంది. Tuple immutable కాబట్టి list లాగా values ను మార్చడానికి methods ఉండవు. Tuple లో సాధారణంగా ఎక్కువగా ఉపయోగించే methods count() మరియు index().

1. count() Method

count() method ఒక value tuple లో ఎన్ని సార్లు ఉందో చెబుతుంది.

Syntax: tuple_name.count(value)

2. index() Method

index() method ఒక value మొదటిసారి ఏ index వద్ద ఉందో చెబుతుంది.

Syntax: tuple_name.index(value)

ఒక value tuple లో లేకపోతే index() method ValueError ఇస్తుంది.

index() లో start మరియు stop

index(value, start, stop) రూపంలో కూడా ఉపయోగించవచ్చు. ఇందులో start index నుంచి search ప్రారంభమవుతుంది. stop index ముందు వరకు మాత్రమే search జరుగుతుంది.

Tuple Methods ఎందుకు ఉపయోగిస్తాము?

  • ఒక value ఎన్నిసార్లు ఉందో తెలుసుకోవడానికి count() ఉపయోగిస్తాము.
  • ఒక value యొక్క position తెలుసుకోవడానికి index() ఉపయోగిస్తాము.
  • Tuple values ను మార్చకుండా information ను పరిశీలించడానికి ఇవి ఉపయోగపడతాయి.

ఈ methods tuple ను మార్చవు. అవి tuple లోని data గురించి information మాత్రమే return చేస్తాయి.

Simple Explanation

ఒక tuple ను ఒక fixed collection లాగా ఊహించండి. అందులోని value ను మార్చలేము, కానీ దాని గురించి తెలుసుకోవచ్చు.

  • count() అంటే: ఈ value ఎన్ని సార్లు ఉంది?
  • index() అంటే: ఈ value మొదటిసారి ఏ position లో ఉంది?

ఉదాహరణకు ('pen', 'book', 'pen') అనే tuple లో count('pen') ఫలితం 2. index('book') ఫలితం 1.

Real-Life Example

ఒక classroom లో students ఎంచుకున్న colors ను tuple లో store చేశామని అనుకుందాం. count() ఉపయోగించి ఎన్ని మంది ఒకే color ఎంచుకున్నారో తెలుసుకోవచ్చు. index() ఉపయోగించి ఒక color list లో మొదటిసారి ఏ position లో వచ్చిందో తెలుసుకోవచ్చు.

Code Examples

count() మరియు index() ఉపయోగించడం

marks = (80, 90, 80, 75, 80)
print(marks.count(80))
print(marks.index(75))

ఈ code లో count() ద్వారా 80 ఎన్ని సార్లు ఉందో, index() ద్వారా 75 మొదటిసారి ఏ index వద్ద ఉందో తెలుసుకుంటాము. Tuple indexing zero-based కాబట్టి 75 index 3 వద్ద ఉంది.

3
3

index() ను start మరియు stop తో ఉపయోగించడం

items = ('pen', 'book', 'pen', 'bag', 'pen')
print(items.count('pen'))
print(items.index('bag', 1, 4))

మొదటి line pen ఎన్ని సార్లు ఉందో లెక్కిస్తుంది. రెండవ line లో index 1 నుంచి index 4 ముందు వరకు bag కోసం search చేస్తుంది. bag index 3 వద్ద ఉంది.

3
3

Line-by-Line Explanation

marks = (80, 90, 80, 75, 80)
ఈ line లో ఐదు numbers తో ఒక tuple create చేస్తున్నాము. Tuple values parentheses లో ఉంటాయి మరియు ఈ values ను తరువాత మార్చలేము.
print(marks.count(80))
ఈ line tuple లోని 80 value ఎన్ని సార్లు ఉందో count చేస్తుంది. 80 మూడు సార్లు ఉన్నందున 3 ను print చేస్తుంది.
print(marks.index(75))
ఈ line 75 value మొదటిసారి ఏ index వద్ద ఉందో తెలుసుకుంటుంది. 75 నాల్గవ value కాబట్టి దాని zero-based index 3. అందువల్ల 3 print అవుతుంది.
items = (‘pen’, ‘book’, ‘pen’, ‘bag’, ‘pen’)
ఈ line strings తో items అనే tuple ను create చేస్తుంది. ఇందులో pen మూడు సార్లు ఉంది.
print(items.count(‘pen’))
ఈ line items tuple లో pen ఎన్ని సార్లు ఉందో లెక్కిస్తుంది. pen మూడు సార్లు ఉన్నందున 3 print అవుతుంది.
print(items.index(‘bag’, 1, 4))
ఈ line index 1 నుంచి index 4 ముందు వరకు bag కోసం search చేస్తుంది. bag index 3 వద్ద ఉన్నందున 3 print అవుతుంది.

Try It

Try this Python code:

Example

colors = ('red', 'blue', 'red', 'green')
print(colors.count('red'))
print(colors.index('green'))

Try It Yourself »

Practice

Practice: numbers = (5, 2, 5, 8, 5, 2) అనే tuple లో 5 ఎన్ని సార్లు ఉందో print చేయండి. తరువాత 8 యొక్క index ను print చేయండి.

Hint: count() value యొక్క occurrences ను లెక్కిస్తుంది. index() value యొక్క మొదటి position ను ఇస్తుంది.

Practice: ఒక tuple days = ('Mon', 'Tue', 'Mon', 'Wed') ఇచ్చినప్పుడు Mon ఎన్ని సార్లు ఉందో మరియు Wed index ఏమిటో కనుగొనండి.

Hint: days.count('Mon') మరియు days.index('Wed') ఉపయోగించండి.

Common Mistakes

count() ను assignment లాగా ఉపయోగించి tuple లోని value ను మార్చాలని ప్రయత్నించడం.
count() value ను మార్చదు. అది value ఎన్ని సార్లు ఉందో number గా return చేస్తుంది.
ఒక value tuple లో లేకపోయినా index() ఉపయోగించడం.
index() ఉపయోగించే ముందు value tuple లో ఉందో చూసుకోవాలి. లేకపోతే ValueError వస్తుంది.
Tuple index counting 1 నుంచి ప్రారంభమవుతుందని అనుకోవడం.
Python లో index counting ఎప్పుడూ 0 నుంచి ప్రారంభమవుతుంది.
index() లో stop index కూడా search లో కలుస్తుందని అనుకోవడం.
Search start నుంచి ప్రారంభమై stop index ముందు వరకు మాత్రమే జరుగుతుంది.

Interview Questions

Tuple లో value occurrences తెలుసుకోవడానికి ఏ method ఉపయోగిస్తారు?

Answer: count() method ఉపయోగిస్తారు. ఇది ఇచ్చిన value tuple లో ఎన్ని సార్లు ఉందో return చేస్తుంది.

Tuple లో value యొక్క position తెలుసుకోవడానికి ఏ method ఉపయోగిస్తారు?

Answer: index() method ఉపయోగిస్తారు. ఇది value యొక్క మొదటి occurrence index ను return చేస్తుంది.

Tuple లో value లేకపోతే index() ఏమి చేస్తుంది?

Answer: Tuple లో value లేకపోతే index() ValueError ఇస్తుంది.

Tuple methods tuple ను మార్చగలవా?

Answer: లేదు. count() మరియు index() tuple ను మార్చవు. అవి tuple గురించి information మాత్రమే ఇస్తాయి.

Exam Questions

ఈ code output ఏమిటి? data = (1, 2, 1, 3); print(data.count(1))

Answer:

Output 2, ఎందుకంటే 1 tuple లో రెండు సార్లు ఉంది.

ఈ code output ఏమిటి? names = ('A', 'B', 'C'); print(names.index('C'))

Answer:

Output 2, ఎందుకంటే C యొక్క zero-based index 2.

Tuple methods లో count() మరియు index() మధ్య తేడా ఏమిటి?

Answer:

count() value ఎన్ని సార్లు ఉందో చెబుతుంది. index() value మొదటిసారి ఏ index వద్ద ఉందో చెబుతుంది.

ఈ syntax లో items.index('book', 1, 4) లో 1 మరియు 4 అర్థం ఏమిటి?

Answer:

1 search ప్రారంభ index. 4 search ఆగే stop index; 4 index search లో కలవదు.

Quick Revision

  • count(value) tuple లో value ఎన్నిసార్లు ఉందో చెబుతుంది.
  • index(value) value యొక్క మొదటి index ను చెబుతుంది.
  • Python indexing 0 నుంచి ప్రారంభమవుతుంది.
  • Value లేకపోతే index() ValueError ఇస్తుంది.
  • index(value, start, stop) తో నిర్దిష్ట range లో search చేయవచ్చు.
  • Tuple methods tuple values ను మార్చవు.
  • తర్వాతి lesson లో Tuple Packing నేర్చుకుంటాము.

Test Your Knowledge

Take a short quiz based on this lesson.

10 Questions • Results
Scroll to Top