Python Nested Tuples
Nested Tuples అంటే ఏమిటి?
ఒక tuple లో మరొక tuple ఉంటే దాన్ని Nested Tuple అంటారు. అంటే tuple లోపల tuple ఉంచడం.
Nested tuples ను related data ను groups గా store చేయడానికి ఉపయోగిస్తాము. ఉదాహరణకు, ప్రతి student యొక్క name మరియు age ను ఒక చిన్న tuple గా ఉంచి, అన్ని students ను ఒక పెద్ద tuple లో ఉంచవచ్చు.
Nested Tuples ఎందుకు ఉపయోగిస్తాము?
- Related values ను ఒక group గా ఉంచడానికి.
- Rows మరియు records లాంటి data ను store చేయడానికి.
- Data structure ను స్పష్టంగా మరియు క్రమబద్ధంగా ఉంచడానికి.
- ఒకే variable లో అనేక చిన్న tuples ను నిర్వహించడానికి.
Nested Tuple ఎలా పనిచేస్తుంది?
బయటి tuple లోని ఒక item ను మొదటి index తో access చేస్తాము. ఆ item కూడా tuple అయితే, దానిలోని value ను రెండవ index తో access చేస్తాము.
ఉదాహరణకు, students[1][0] లో మొదటి index రెండవ inner tuple ను సూచిస్తుంది. రెండవ index ఆ inner tuple లోని మొదటి value ను సూచిస్తుంది.
Indexing విధానం
nested_tuple[outer_index][inner_index] అనే విధానాన్ని ఉపయోగిస్తాము.
Simple Explanation
Nested tuple అంటే tuple inside another tuple.
ఉదాహరణకు, (("Anu", 18), ("Ravi", 19)) లో బయట tuple లో రెండు inner tuples ఉన్నాయి. data[0] మొదటి inner tuple ను ఇస్తుంది. data[0][1] మొదటి inner tuple లోని రెండవ value ను ఇస్తుంది.
మొదటి index బయట tuple లో ఏ inner tuple కావాలో చెబుతుంది. రెండవ index ఆ inner tuple లో ఏ value కావాలో చెబుతుంది.
Real-Life Example
ఒక school లో students వివరాలను ఊహించండి. ప్రతి student కోసం name మరియు age ను ఒక చిన్న tuple గా ఉంచవచ్చు. అన్ని students వివరాలను ఒక పెద్ద tuple లో ఉంచవచ్చు.
ఉదాహరణ: (("Anu", 18), ("Ravi", 19), ("Sita", 17)). ఇక్కడ ప్రతి student ఒక inner tuple. మొత్తం students data ఒక outer tuple.
Code Examples
Student Data ను Nested Tuple లో Store చేయడం
students = (("Anu", 18), ("Ravi", 19), ("Sita", 17))
print(students[0])
print(students[1][0])
print(students[2][1])
ఈ ఉదాహరణలో ప్రతి student యొక్క name మరియు age ఒక inner tuple లో ఉన్నాయి. ఈ inner tuples అన్నీ కలిసి ఒక outer tuple ను రూపొందించాయి. Indexing ఉపయోగించి పూర్తి inner tuple లేదా దానిలోని ఒక value ను access చేస్తున్నాము.
("Anu", 18)
Ravi
17
Line-by-Line Explanation
ఈ line లో students అనే variable లో ఒక outer tuple సృష్టించబడింది. ఇందులో మూడు inner tuples ఉన్నాయి. ప్రతి inner tuple లో student name మరియు age ఉన్నాయి.
ఇక్కడ outer tuple లోని index 0 ను access చేస్తున్నాము. అందువల్ల మొదటి inner tuple అయిన (“Anu”, 18) screen పై print అవుతుంది.
మొదటి index 1 వల్ల రెండవ inner tuple ఎంచుకోబడుతుంది. రెండవ index 0 వల్ల ఆ inner tuple లోని మొదటి value అయిన Ravi print అవుతుంది.
మొదటి index 2 వల్ల మూడవ inner tuple ఎంచుకోబడుతుంది. రెండవ index 1 వల్ల ఆ tuple లోని రెండవ value అయిన 17 print అవుతుంది.
Try It
Try this Python code:
school = (("Math", 40), ("Science", 35), ("English", 45))
print(school[0][0])
print(school[1][1])
Practice
Practice: cities అనే nested tuple ను ఉపయోగించి, రెండవ city name మరియు మూడవ city population ను print చేయండి.
Hint: రెండవ city కోసం outer index 1 ఉపయోగించండి. మూడవ city కోసం outer index 2 ఉపయోగించండి. Inner tuple లో name కోసం index 0, population కోసం index 1 ఉపయోగించండి.
Practice: ఒక nested tuple లో మూడు products మరియు వాటి prices ను store చేయండి. మొదటి product name మరియు రెండవ product price ను print చేయండి.
Hint: ప్రతి product ను (name, price) రూపంలో inner tuple గా రాయండి.
Common Mistakes
Inner tuple లోని value కోసం రెండు indexes ఉపయోగించాలి. City name కోసం cities[1][0] వంటి code రాయాలి.
మొదటి index ఏ inner tuple కావాలో చెబుతుంది. రెండవ index ఆ inner tuple లో ఏ value కావాలో చెబుతుంది.
మూడు items ఉంటే valid outer indexes 0, 1, 2 మాత్రమే. Python indexing 0 నుండి ప్రారంభమవుతుందని గుర్తుంచుకోండి.
ప్రతి inner tuple ను parentheses లో రాయాలి. ఉదాహరణకు ((“A”, 10), (“B”, 20)) అని రాయాలి.
Interview Questions
Nested tuple అంటే ఏమిటి?
Answer: ఒక tuple లో మరొక tuple లేదా అనేక tuples ఉంటే దాన్ని nested tuple అంటారు.
Nested tuple లోని value ను ఎలా access చేస్తారు?
Answer: Outer index మరియు inner index ఉపయోగిస్తారు. సాధారణ రూపం nested_tuple[outer_index][inner_index].
students = ((“Anu”, 18), (“Ravi”, 19)) లో Ravi ను ఎలా access చేస్తారు?
Answer: students[1][0] ఉపయోగిస్తే Ravi వస్తుంది.
Nested tuples ఎప్పుడు ఉపయోగపడతాయి?
Answer: Related data ను చిన్న groups గా ఉంచి, ఆ groups అన్నింటినీ ఒకే tuple లో నిర్వహించాల్సినప్పుడు nested tuples ఉపయోగపడతాయి.
Exam Questions
క్రింది code యొక్క output ఏమిటి? data = ((10, 20), (30, 40)); print(data[1][0])
Answer:
Output 30. Outer index 1 రెండవ inner tuple ను, inner index 0 దాని మొదటి value ను సూచిస్తుంది.
Nested tuple ను సృష్టించే సరైన syntax ఏది?
Answer:
((1, 2), (3, 4)) అనేది nested tuple యొక్క సరైన ఉదాహరణ.
data = ((“A”, 1), (“B”, 2)) లో రెండవ inner tuple ను ఎలా print చేస్తారు?
Answer:
print(data[1]) అని రాయాలి.
Nested tuple లో మొదటి index ఏమి సూచిస్తుంది?
Answer:
మొదటి index outer tuple లోని ఏ inner tuple ను ఎంచుకోవాలో సూచిస్తుంది.
Quick Revision
- Tuple లో మరో tuple ఉంటే దాన్ని Nested Tuple అంటారు.
- Nested tuple లో data groups గా store చేయవచ్చు.
- మొదటి index outer tuple లోని inner tuple ను ఎంచుకుంటుంది.
- రెండవ index inner tuple లోని value ను ఎంచుకుంటుంది.
- Nested tuple value access చేయడానికి nested_tuple[outer_index][inner_index] ఉపయోగించాలి.
- Python indexing 0 నుండి ప్రారంభమవుతుంది.
- Nested tuples లోని values ను మార్చలేము, ఎందుకంటే tuples immutable.
