Python Practical Tuple Programs
Practical Tuple Programs
ఇప్పటివరకు Tuple అంటే ఏమిటో, దాని లక్షణాలను తెలుసుకున్నాము. ఇప్పుడు Tuple ను ఉపయోగించి చిన్న చిన్న practical programs రాయడం నేర్చుకుందాం.
Tuple programs ఎందుకు ఉపయోగకరం?
Tuple లో ఒకదానికొకటి సంబంధించిన multiple values ను ఒకే variable లో store చేయవచ్చు. Tuple మార్చలేనిది కాబట్టి, fixed data ను భద్రంగా ఉంచడానికి ఉపయోగపడుతుంది.
- Student marks ను store చేయడానికి
- రోజుల పేర్లు లేదా నెలల పేర్లు store చేయడానికి
- Repeated values ఎన్ని ఉన్నాయో తెలుసుకోవడానికి
- Tuple లోని values ను loop ద్వారా చదవడానికి
- ఒకే సమయంలో multiple values ను variables లోకి తీసుకోవడానికి
Program 1: Student marks average
Student marks ను Tuple లో store చేసి, మొత్తం marks మరియు average ను కనుగొనవచ్చు. sum() values మొత్తాన్ని ఇస్తుంది. len() values సంఖ్యను ఇస్తుంది.
Program 2: Tuple లో value search చేయడం
Tuple లో ఒక value ఉందో లేదో in operator తో తెలుసుకోవచ్చు. ఇది True లేదా False ను ఇస్తుంది.
Program 3: Tuple లో repeated value count చేయడం
count() method ఒక value Tuple లో ఎన్నిసార్లు ఉందో చెబుతుంది.
Tuple program రాయడానికి సాధారణ విధానం
- అవసరమైన values తో Tuple ను create చేయండి.
- అవసరమైన operation ను ఎంచుకోండి.
sum(),len(),in,count()లేదా loop ఉపయోగించండి.- ఫలితాన్ని
print()తో చూపించండి.
Simple Explanation
Tuple ను fixed values యొక్క collection గా భావించండి. ఉదాహరణకు marks Tuple ఉంటే, దానిలోని marks ను మార్చకుండా total, average లేదా ఒక mark ఉందో లేదో తెలుసుకోవచ్చు.
Tuple లో values ను index ద్వారా access చేయవచ్చు, loop తో ఒక్కొక్కటిగా చదవవచ్చు. Tuple మార్చలేనిది కాబట్టి constant లేదా fixed data కోసం ఇది అనుకూలంగా ఉంటుంది.
Real-Life Example
ఒక విద్యార్థి ఒక పరీక్షలో పొందిన marks ను (78, 85, 90, 66) అనే Tuple లో ఉంచాడని అనుకుందాం. ఈ marks ను మార్చాల్సిన అవసరం లేకపోతే Tuple సరైన ఎంపిక. ఈ Tuple తో total marks, average marks లేదా 90 అనే mark ఉందా అనే విషయాలను తెలుసుకోవచ్చు.
Code Examples
Student marks యొక్క total మరియు average
marks = (78, 85, 90, 66)
total = sum(marks)
average = total / len(marks)
print("Total:", total)
print("Average:", average)
ఈ program marks Tuple నుండి మొత్తం marks మరియు average ను లెక్కిస్తుంది. Tuple లోని values ను మార్చకుండా calculations చేయవచ్చు.
Total: 319 Average: 79.75
Tuple లో value ఉందో లేదో తెలుసుకోవడం
days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
search_day = "Wednesday"
if search_day in days:
print(search_day, "is present")
else:
print(search_day, "is not present")
in operator ను ఉపయోగించి search_day అనే value Tuple లో ఉందో లేదో పరీక్షిస్తున్నాము. Value ఉంటే మొదటి message, లేకపోతే రెండవ message print అవుతుంది.
Wednesday is present
Tuple లో value ఎన్నిసార్లు ఉందో తెలుసుకోవడం
numbers = (2, 4, 2, 6, 2, 8)
target = 2
count = numbers.count(target)
print("Count:", count)
count() method target value Tuple లో ఎన్నిసార్లు ఉందో లెక్కిస్తుంది. ఇక్కడ 2 అనే value మూడు సార్లు ఉంది.
Count: 3
Line-by-Line Explanation
ఈ line లో నాలుగు marks తో ఒక Tuple create అవుతుంది. Python ఈ values ను marks అనే variable లో store చేస్తుంది.
sum() function Tuple లోని అన్ని numbers ను కలుపుతుంది. వచ్చిన మొత్తం total variable లో store అవుతుంది.
len(marks) marks సంఖ్యను ఇస్తుంది. Total ను marks సంఖ్యతో divide చేసి average కనుగొంటాము.
ఈ line total marks ను screen పై చూపిస్తుంది.
ఈ line calculated average ను screen పై చూపిస్తుంది.
ఈ line లో వారంలోని కొన్ని రోజుల పేర్లతో Tuple create అవుతుంది.
Search చేయాల్సిన రోజును search_day variable లో store చేస్తాము.
search_day value days Tuple లో ఉందా అని in operator పరీక్షిస్తుంది. Value ఉంటే if block execute అవుతుంది.
Value Tuple లో ఉన్నప్పుడు ఈ line ఆ value present అని message చూపిస్తుంది.
if condition False అయినప్పుడు else block ప్రారంభమవుతుంది.
Search చేసిన value Tuple లో లేకపోతే ఈ message print అవుతుంది.
ఈ line లో numbers తో ఒక Tuple create చేస్తాము. ఇందులో 2 value పలుమార్లు ఉంది.
ఎన్ని సార్లు ఉందో లెక్కించాల్సిన value ను target variable లో ఉంచుతాము.
count() method target value Tuple లో ఎన్నిసార్లు ఉందో లెక్కిస్తుంది. ఆ సంఖ్య count variable లో store అవుతుంది.
ఈ line target value యొక్క count ను screen పై చూపిస్తుంది.
Try It
Try this Python code:
colors = ("red", "blue", "green", "blue")
target = "blue"
count = colors.count(target)
print("Color count:", count)
Practice
Practice: fruits = (“apple”, “banana”, “apple”, “orange”, “apple”) అనే Tuple ఇవ్వబడింది. target value ఎన్నిసార్లు ఉందో లెక్కించి print చేయండి.
Hint: count() method ను ఉపయోగించండి.
Practice: marks = (70, 80, 90, 60) అనే Tuple నుండి total marks మరియు average marks ను print చేయండి.
Hint: sum() మరియు len() functions ను ఉపయోగించండి.
Practice: subjects = (“Python”, “SQL”, “HTML”) అనే Tuple లో “Python” ఉందో లేదో check చేయండి.
Hint: in operator మరియు if statement ను ఉపయోగించండి.
Common Mistakes
Tuple immutable. Tuple లోని value ను నేరుగా మార్చలేరు. మార్పులు అవసరమైతే List ఉపయోగించాలి.
Method ను execute చేయడానికి parentheses ఉపయోగించాలి: numbers.count(2).
Value ఉందో లేదో చూపించడానికి if search_value in tuple_name: వంటి condition రాయండి.
Values సంఖ్యను సరిగ్గా తెలుసుకోవడానికి len(tuple_name) ఉపయోగించండి.
One-item Tuple కోసం comma తప్పనిసరి: value = (5,).
Interview Questions
Tuple ను practical programs లో ఎందుకు ఉపయోగిస్తారు?
Answer: మార్చాల్సిన అవసరం లేని related values ను ఒకే collection లో store చేయడానికి Tuple ఉపయోగిస్తారు.
Tuple లో values మొత్తాన్ని ఎలా కనుగొంటారు?
Answer: Numbers ఉన్న Tuple కోసం sum(tuple_name) ఉపయోగించవచ్చు.
Tuple లో ఒక value ఉందో లేదో ఎలా పరీక్షిస్తారు?
Answer: in operator ను ఉపయోగిస్తారు. ఉదాహరణకు value in tuple_name.
count() method ఏమి చేస్తుంది?
Answer: ఇచ్చిన value Tuple లో ఎన్నిసార్లు ఉందో count() method చెబుతుంది.
Tuple లోని values ను మార్చవచ్చా?
Answer: లేదు. Tuple immutable కాబట్టి create చేసిన తర్వాత దాని items ను మార్చలేము.
Exam Questions
marks = (10, 20, 30) కోసం total కనుగొనే Python statement రాయండి.
Answer:
total = sum(marks)
numbers = (1, 2, 2, 3) లో 2 count ఎంత?
Answer:
numbers.count(2) యొక్క result 2.
days = (“Mon”, “Tue”) లో “Mon” ఉందో లేదో check చేసే condition రాయండి.
Answer:
if “Mon” in days:
Tuple లో average ఎలా కనుగొంటారు?
Answer:
ముందుగా total = sum(values) తీసుకుని, average = total / len(values) అని రాయాలి.
Quick Revision
- Tuple fixed values ను store చేసే collection.
- Tuple values ను indexing మరియు loop ద్వారా access చేయవచ్చు.
- sum() Tuple లోని numbers మొత్తాన్ని ఇస్తుంది.
- len() Tuple లోని items సంఖ్యను ఇస్తుంది.
- in operator తో value ఉందో లేదో check చేయవచ్చు.
- count() method ఒక value ఎన్నిసార్లు ఉందో చెబుతుంది.
- Tuple immutable; దాని items ను నేరుగా మార్చలేము.
