Python Tuple Packing
Tuple Packing అంటే ఏమిటి?
చాలా values ను ఒకే tuple లో కలిపి నిల్వ చేయడాన్ని Tuple Packing అంటారు.
Python లో values ను comma తో వేరు చేసి ఒక variable కు assign చేస్తే, Python ఆ values అన్నింటినీ ఒక tuple గా pack చేస్తుంది.
ఎందుకు ఉపయోగిస్తాము?
- చాలా values ను ఒకే variable లో ఉంచడానికి.
- Related data ను ఒక group గా నిర్వహించడానికి.
- Function నుండి multiple values ను ఒకే tuple రూపంలో పొందడానికి.
- Data ను క్రమబద్ధంగా మరియు సులభంగా pass చేయడానికి.
ఇది ఎలా పనిచేస్తుంది?
ఉదాహరణకు student = "Ravi", 21, "Python" అని రాస్తే, Python మూడు values ను ఒక tuple లో pack చేస్తుంది. Parentheses లేకపోయినా comma ఉన్నందున tuple తయారవుతుంది.
Tuple packing లో ముఖ్యమైన విషయం comma. Parentheses optional కావచ్చు, కానీ values మధ్య comma ఉండాలి.
గమనించాల్సిన విషయం
ఒక value మాత్రమే ఉన్న tuple తయారు చేయాలంటే trailing comma అవసరం. ఉదాహరణకు item = "Book", అనేది one-item tuple. కానీ item = "Book" సాధారణ string మాత్రమే.
Simple Explanation
Tuple Packing అంటే అనేక values ను ఒకే tuple లో పెట్టడం. Values ను comma తో వేరు చేసి ఒక variable కు assign చేస్తే Python వాటిని tuple గా pack చేస్తుంది.
ఉదాహరణ: data = "A", "B", "C". ఇక్కడ data లో ("A", "B", "C") tuple ఉంటుంది.
Tuple packing ద్వారా related values ను ఒక group గా ఉంచవచ్చు. Parentheses రాసినా, రాయకపోయినా comma ఉంటే tuple packing జరుగుతుంది.
Real-Life Example
ఒక student record లో పేరు, వయస్సు, course అనే మూడు వివరాలు ఉన్నాయని అనుకోండి. వాటిని మూడు వేర్వేరు variables లో ఉంచడం కంటే ఒక tuple లో కలిపి ఉంచవచ్చు.
student = "Ravi", 21, "Python" అనే code లో పేరు, వయస్సు, course మూడు values ఒకే student tuple లో pack అవుతాయి. ఇది ఒక student record ను group గా నిర్వహించడానికి ఉపయోగపడుతుంది.
Code Examples
మూడు values ను tuple గా pack చేయడం
student = "Ravi", 21, "Python"
print(student)
ఇక్కడ పేరు, వయస్సు, course అనే మూడు values ను student variable లో pack చేస్తున్నాము. Python వీటిని ఒక tuple గా గుర్తిస్తుంది. print() ఆ tuple ను చూపిస్తుంది.
('Ravi', 21, 'Python')
Parentheses లేకుండా Tuple Packing
colors = "Red", "Green", "Blue"
print(colors)
item = "Book",
print(item)
మొదటి assignment లో మూడు colors ఒక tuple గా pack అవుతాయి. రెండవ assignment లో ఒక value మాత్రమే ఉన్నప్పటికీ comma ఉండటం వల్ల item ఒక one-item tuple అవుతుంది.
('Red', 'Green', 'Blue')
('Book',)
Line-by-Line Explanation
ఈ line లో మూడు values ను comma తో వేరు చేసి
student variable కు assign చేస్తున్నాము. Python ఈ values ను ఒక tuple గా pack చేస్తుంది.
ఈ line
student variable లో ఉన్న packed tuple ను screen పై చూపిస్తుంది.
ఈ line లో మూడు color values ను parentheses లేకుండా ఒక tuple గా pack చేస్తున్నాము. Values మధ్య comma ఉండటం వల్ల Python tuple ను సృష్టిస్తుంది.
ఈ line
colors tuple లోని values ను tuple రూపంలో output గా చూపిస్తుంది.
ఇక్కడ ఒక value మాత్రమే ఉంది. అయినప్పటికీ చివరలో comma ఉండటం వల్ల
item ఒక one-item tuple గా pack అవుతుంది.
ఈ line one-item tuple ను చూపిస్తుంది. Output లో
('Book',) గా చివరి comma కనిపిస్తుంది.
Try It
Try this Python code:
fruit = "Apple"
quantity = 4
price = 25
item = fruit, quantity, price
print(item)
Practice
Practice:
city, temperature, weather అనే values ను ఒకే tuple లో pack చేసి report variable లో ఉంచండి. తరువాత report ను print చేయండి.
Hint:
మూడు variables ను comma తో వేరు చేసి report కు assign చేయండి: report = city, temperature, weather.
Practice:
name, age, course అనే values ను ఒక tuple గా pack చేయండి. Parentheses ఉపయోగించకుండా code రాయండి.
Hint:
Values మధ్య comma పెట్టి ఒక variable కు assign చేయండి.
Practice:
language = "Python" అనే value ను one-item tuple గా మార్చండి.
Hint:
Value తర్వాత trailing comma పెట్టాలి: language = "Python",.
Common Mistakes
Values మధ్య comma పెట్టకుండా parentheses మాత్రమే ఉపయోగించడం. ఉదాహరణకు data = ("A" "B") అని రాయడం.
Tuple values మధ్య comma తప్పనిసరిగా పెట్టాలి: data = ("A", "B").
One-item tuple కోసం comma మర్చిపోవడం: item = "Book".
One-item tuple కోసం value తర్వాత comma పెట్టాలి: item = "Book",.
Tuple packing ను tuple unpacking తో గందరగోళం చేయడం.
చాలా values ను ఒక tuple లో కలపడం packing. Tuple లోని values ను వేర్వేరు variables లోకి తీసుకోవడం unpacking. ఈ lesson లో packing మాత్రమే నేర్చుకుంటున్నాము.
Tuple లో values pack చేసిన తర్వాత వాటిని మార్చవచ్చని అనుకోవడం.
Tuple immutable కాబట్టి దాని values ను నేరుగా మార్చలేము. Packing సమయంలో సరైన values ను ఉంచండి.
Interview Questions
Tuple Packing అంటే ఏమిటి?
Answer: చాలా values ను ఒకే tuple లో కలిపి ఒక variable లో నిల్వ చేయడాన్ని Tuple Packing అంటారు.
Python లో Tuple Packing ఎలా చేస్తారు?
Answer: Values ను comma తో వేరు చేసి ఒక variable కు assign చేస్తే Python వాటిని tuple గా pack చేస్తుంది. ఉదాహరణ: data = 10, 20, 30.
Tuple Packing లో parentheses తప్పనిసరా?
Answer: కాదు. Parentheses optional కావచ్చు. కానీ values మధ్య comma ఉండాలి.
One-item tuple కోసం comma ఎందుకు అవసరం?
Answer: ఒక value మాత్రమే ఉన్నప్పుడు trailing comma ఉంటేనే Python దాన్ని tuple గా గుర్తిస్తుంది. ఉదాహరణ: item = "Pen",.
Exam Questions
క్రింది code output ఏమిటి? numbers = 1, 2, 3
print(numbers)
Answer:
Output: (1, 2, 3). మూడు values ఒక tuple గా pack అవుతాయి.
క్రింది code లో value ఏ data type? value = "Python",
Answer:
value ఒక one-item tuple. దాని value ('Python',).
Tuple Packing లో comma పాత్ర ఏమిటి?
Answer:
Comma values ను వేరు చేసి tuple structure ను గుర్తించడానికి సహాయపడుతుంది. Parentheses లేకపోయినా comma ఉంటే tuple packing జరుగుతుంది.
Tuple Packing ను ఒక ఉదాహరణతో రాయండి.
Answer:
student = "Anu", 20, "Python". ఇక్కడ మూడు values student అనే tuple లో pack అవుతాయి.
Quick Revision
- చాలా values ను ఒకే tuple లో కలపడం Tuple Packing.
- Values ను comma తో వేరు చేసి ఒక variable కు assign చేయాలి.
- Tuple packing కోసం parentheses తప్పనిసరి కాదు.
- One-item tuple కోసం trailing comma తప్పనిసరి.
- ఉదాహరణ: data = 10, 20, 30.
- Tuple packing మరియు tuple unpacking వేర్వేరు concepts.
