Python Tuple Indexing
Tuple Indexing
Tuple లోని ప్రతి item ను దాని position ద్వారా access చేయడాన్ని Tuple Indexing అంటారు.
Python లో indexing ఎప్పుడూ 0 నుంచి ప్రారంభమవుతుంది. అంటే మొదటి item index 0, రెండవ item index 1, మూడవ item index 2 గా ఉంటుంది.
Indexing ఎందుకు ఉపయోగిస్తాము?
Tuple లోని మొత్తం data లో ఒక ప్రత్యేకమైన item ను తీసుకోవడానికి indexing ఉపయోగిస్తాము. ఉదాహరణకు, colors tuple లో మొదటి color లేదా చివరి color మాత్రమే కావాలంటే index ఉపయోగించవచ్చు.
Positive Indexing
ఎడమ నుంచి items ను access చేయడానికి positive indexes ఉపయోగిస్తాము. మొదటి item కు 0, తర్వాతి item కు 1 ఉంటుంది.
Negative Indexing
కుడి నుంచి items ను access చేయడానికి negative indexes ఉపయోగిస్తాము. చివరి item index -1, దాని ముందు item index -2 అవుతుంది.
ఒక tuple లో ఉన్న indexes కంటే ఎక్కువ index ఉపయోగిస్తే IndexError వస్తుంది. Tuple indexing ద్వారా item ను చదవవచ్చు; కానీ tuple item ను మార్చలేము, ఎందుకంటే tuples immutable.
Simple Explanation
Tuple ను ఒక వరుసలో ఉన్న boxes లా ఊహించుకోండి. ప్రతి box కు ఒక number ఉంటుంది. Python లో మొదటి box number 0 తో ప్రారంభమవుతుంది.
tuple_name[0]మొదటి item ను ఇస్తుంది.tuple_name[1]రెండవ item ను ఇస్తుంది.tuple_name[-1]చివరి item ను ఇస్తుంది.
Indexing syntax: tuple_name[index]
Real-Life Example
ఒక cinema hall లో seats వరుసగా numbered గా ఉంటాయి. మొదటి seat ను position 0, రెండవ seat ను position 1 గా గుర్తించినట్లు, tuple లోని items కూడా indexes తో గుర్తించబడతాయి. చివరి seat ను వెనుక నుంచి గుర్తించడానికి -1 ఉపయోగించినట్లు, tuple లో చివరి item కోసం కూడా -1 ఉపయోగిస్తాము.
Code Examples
Positive మరియు Negative Indexing
colors = ("red", "green", "blue")
print(colors[0])
print(colors[2])
print(colors[-1])
ఈ ఉదాహరణలో tuple లోని మొదటి item ను positive index తో, మూడవ item ను positive index తో, చివరి item ను negative index తో access చేస్తున్నాము.
red blue blue
Line-by-Line Explanation
ఈ line లో మూడు strings తో colors అనే tuple create చేస్తున్నాము. Python వాటిని వరుసగా indexes 0, 1, 2 వద్ద store చేస్తుంది.
ఈ line colors tuple లోని index 0 item ను తీసుకుంటుంది. Index 0 మొదటి item కాబట్టి output గా red print అవుతుంది.
ఈ line index 2 item ను తీసుకుంటుంది. ఇది tuple లోని మూడవ item, కాబట్టి blue output అవుతుంది.
ఈ line negative indexing ఉపయోగిస్తుంది. -1 ఎల్లప్పుడూ చివరి item ను సూచిస్తుంది. అందువల్ల blue output అవుతుంది.
Try It
Try this Python code:
fruits = ("apple", "banana", "mango")
print(fruits[1])
Practice
Practice:
scores = (72, 85, 91, 68) అనే tuple ఇచ్చారు. Indexing ఉపయోగించి మొదటి score మరియు చివరి score ను print చేయండి.
Hint:
మొదటి item కోసం [0], చివరి item కోసం [-1] ఉపయోగించండి.
Practice:
days = ("Monday", "Tuesday", "Wednesday", "Thursday") అనే tuple లో Wednesday ను index ఉపయోగించి print చేయండి.
Hint:
మొదటి item index 0 నుంచి ప్రారంభమవుతుంది. Wednesday యొక్క index ను లెక్కించండి.
Common Mistakes
మొదటి item కోసం tuple[1] ఉపయోగించడం.
Python indexing 0 నుంచి ప్రారంభమవుతుంది. కాబట్టి మొదటి item కోసం tuple[0] ఉపయోగించాలి.
చివరి item కోసం tuple length ను index గా ఉపయోగించడం. ఉదాహరణకు, నాలుగు items ఉన్న tuple కు tuple[4] రాయడం.
నాలుగు items కు valid indexes 0, 1, 2, 3 మాత్రమే. చివరి item కోసం సులభంగా tuple[-1] ఉపయోగించండి.
లేని index ను access చేయడం వల్ల IndexError రావడం.
Index ఉపయోగించే ముందు tuple లోని items సంఖ్యను గమనించండి. Valid index range లోనే index ఇవ్వాలి.
Indexing తో tuple item ను మార్చడానికి ప్రయత్నించడం.
Indexing tuple item ను చదవడానికి మాత్రమే ఉపయోగించండి. Tuple immutable కాబట్టి item ను నేరుగా మార్చలేము.
Interview Questions
Python tuple indexing ఏ index నుంచి ప్రారంభమవుతుంది?
Answer: Python tuple indexing 0 నుంచి ప్రారంభమవుతుంది. మొదటి item index 0 కలిగి ఉంటుంది.
Tuple లో చివరి item ను ఎలా access చేస్తారు?
Answer: చివరి item ను access చేయడానికి negative index -1 ఉపయోగిస్తారు. ఉదాహరణ: my_tuple[-1].
Tuple లో లేని index ను access చేస్తే ఏమి జరుగుతుంది?
Answer: Tuple లో లేని index ను access చేస్తే Python IndexError చూపిస్తుంది.
Exam Questions
numbers = (10, 20, 30) లో 20 ను access చేయడానికి సరైన code ఏమిటి?
Answer:
numbers[1]. ఎందుకంటే 20 రెండవ item, దాని index 1.
items = ("pen", "book", "bag") లో చివరి item ను access చేయడానికి సరైన index ఏమిటి?
Answer:
సరైన index -1. Code: items[-1].
ఐదు items ఉన్న tuple లో valid positive indexes ఏమిటి?
Answer:
Valid positive indexes 0, 1, 2, 3, 4.
Quick Revision
- Tuple indexing ద్వారా tuple లోని ఒక item ను access చేస్తాము.
- Python indexing 0 నుంచి ప్రారంభమవుతుంది.
- మొదటి item కోసం
[0]ఉపయోగించాలి. - చివరి item కోసం
[-1]ఉపయోగించాలి. - లేని index ఉపయోగిస్తే
IndexErrorవస్తుంది. - Tuple indexing item ను చదవడానికి ఉపయోగపడుతుంది; tuple items ను మార్చలేము.
