Python Tuple Unpacking
Tuple Unpacking అంటే ఏమిటి?
Tuple లో ఉన్న values ను వేర్వేరు variables లోకి విడిగా assign చేయడాన్ని Tuple Unpacking అంటారు.
ఒక tuple లో మూడు values ఉంటే, వాటిని మూడు variables కు ఒకేసారి assign చేయవచ్చు. Python ప్రతి value ను క్రమం ప్రకారం సంబంధిత variable లో ఉంచుతుంది.
Syntax
variable1, variable2, variable3 = tuple_name
ఇక్కడ tuple లోని మొదటి value, మొదటి variable కు; రెండవ value, రెండవ variable కు; మూడవ value, మూడవ variable కు assign అవుతుంది.
ఎందుకు ఉపయోగిస్తాము?
- Tuple లోని values ను సులభంగా వేర్వేరు variables గా పొందడానికి.
- ఒకేసారి అనేక variables కు values assign చేయడానికి.
- Code ను చిన్నగా మరియు readable గా ఉంచడానికి.
- Functions నుండి వచ్చే multiple values ను సులభంగా ఉపయోగించడానికి.
ముఖ్యమైన నియమం
Unpacking చేసేటప్పుడు variables సంఖ్య మరియు tuple values సంఖ్య సమానంగా ఉండాలి. సంఖ్యలు సమానంగా లేకపోతే Python error ఇస్తుంది.
Simple Explanation
Tuple unpacking ను ఒక పెట్టెలో ఉన్న వస్తువులను ఒక్కొక్కటి వేర్వేరు పెట్టెల్లో పెట్టినట్లుగా ఊహించండి.
student = ("Anu", 18, "Python") అనే tuple లో మూడు values ఉన్నాయి. name, age, subject = student అని రాస్తే, Anu name లోకి, 18 age లోకి, Python subject లోకి వెళ్తాయి.
Values యొక్క order చాలా ముఖ్యం. Tuple లో మొదటి value ఎప్పుడూ మొదటి variable కు assign అవుతుంది.
Real-Life Example
ఒక విద్యార్థి వివరాలు ఒక card పై పేరు, వయస్సు, కోర్సు అనే క్రమంలో ఉన్నాయని అనుకుందాం. ఆ card ను చదివి మూడు వేర్వేరు forms లో వివరాలను రాసినట్లే tuple unpacking పనిచేస్తుంది. Tuple లోని values ను వరుసగా name, age, course variables లోకి తీసుకోవచ్చు.
Code Examples
Basic Tuple Unpacking
student = ("Anu", 18, "Python")
name, age, subject = student
print(name)
print(age)
print(subject)
ఈ ఉదాహరణలో ఒక student tuple లో మూడు values ఉన్నాయి. ఆ values ను name, age, subject అనే మూడు variables లోకి unpack చేస్తున్నాము. Assignment order tuple order తో సమానంగా ఉంటుంది.
Anu 18 Python
Line-by-Line Explanation
ఈ line లో మూడు values ఉన్న tuple ను
student variable లో store చేస్తున్నాము. Python ఈ values ను tuple క్రమంలో ఉంచుతుంది.
ఈ line tuple unpacking చేస్తుంది. మొదటి value
Anu ను name లో, రెండవ value 18 ను age లో, మూడవ value Python ను subject లో assign చేస్తుంది.
ఈ line
name variable లో ఉన్న Anu value ను output గా చూపిస్తుంది.
ఈ line
age variable లో ఉన్న 18 value ను output గా చూపిస్తుంది.
ఈ line
subject variable లో ఉన్న Python value ను output గా చూపిస్తుంది.
Try It
Try this Python code:
colors = ("red", "green", "blue")
first, second, third = colors
print(first)
print(second)
print(third)
Practice
Practice: ఒక person tuple లో "Ravi" మరియు 20 ఉన్నాయి. Tuple unpacking ఉపయోగించి వాటిని name మరియు age variables లోకి తీసుకొని, రెండింటినీ print చేయండి.
Hint: name, age = person అనే విధానాన్ని ఉపయోగించండి.
Practice: product = ("Laptop", 50000, "Electronics") అనే tuple ను item, price, category variables లోకి unpack చేసి వాటిని print చేయండి.
Hint: Variables ను tuple values ఉన్న అదే క్రమంలో రాయండి.
Common Mistakes
a, b = (10, 20, 30).Tuple values సంఖ్యకు సమానంగా variables సంఖ్యను ఇవ్వాలి. సరైన విధానం
a, b, c = (10, 20, 30).
Tuple లో values ఉన్న క్రమాన్నే variables లో కూడా ఉంచాలి. మొదటి value మొదటి variable కు assign అవుతుంది.
Unpacking statement రాయడానికి ముందు tuple లో ఎన్ని values ఉన్నాయో గమనించండి. అదే సంఖ్యలో variables సిద్ధం చేయండి.
Indexing లో ఒక్కో value ను index ద్వారా తీసుకుంటారు. Unpacking లో అన్ని values ను ఒకేసారి వేర్వేరు variables లోకి assign చేస్తారు.
Interview Questions
Python లో Tuple Unpacking అంటే ఏమిటి?
Answer: Tuple లోని values ను ఒకేసారి వేర్వేరు variables లోకి assign చేయడాన్ని Tuple Unpacking అంటారు. ఉదాహరణకు a, b = (10, 20).
Tuple Unpacking చేసేటప్పుడు variables సంఖ్య మరియు tuple values సంఖ్య సమానంగా లేకపోతే ఏమవుతుంది?
Answer: Python ValueError ను చూపిస్తుంది, ఎందుకంటే ప్రతి value కు ఒక variable అవసరం.
Tuple Unpacking లో order ఎందుకు ముఖ్యం?
Answer: Python values ను ఎడమ నుంచి కుడికి క్రమంలో assign చేస్తుంది. అందువల్ల మొదటి tuple value మొదటి variable కు assign అవుతుంది.
Exam Questions
క్రింది code output ఏమిటి? x, y = (5, 10) తరువాత print(x) రాస్తే.
Answer:
Output 5 వస్తుంది, ఎందుకంటే tuple లోని మొదటి value x కు assign అవుతుంది.
Tuple unpacking ఉపయోగించి data = ("A", "B", "C") ను మూడు variables లోకి ఎలా assign చేస్తారు?
Answer:
first, second, third = data అని రాయాలి.
క్రింది code లో error ఎందుకు వస్తుంది? a, b = (1, 2, 3).
Answer:
Tuple లో మూడు values ఉన్నా variables రెండు మాత్రమే ఉన్నాయి. Values మరియు variables సంఖ్య సమానంగా లేకపోవడం వల్ల error వస్తుంది.
Quick Revision
- Tuple లోని values ను వేర్వేరు variables లోకి assign చేయడమే Tuple Unpacking.
- Syntax: variable1, variable2 = tuple_name
- Values ను tuple order ప్రకారం variables లోకి assign చేస్తారు.
- Variables సంఖ్య మరియు tuple values సంఖ్య సాధారణంగా సమానంగా ఉండాలి.
- Tuple Unpacking code ను చిన్నగా మరియు readable గా చేస్తుంది.
- Order తప్పితే values తప్పు variables లోకి assign అవుతాయి.
