Home » Tech Tutorials » Coding & Programming » Python » Python Practical Tuple Programs

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 రాయడానికి సాధారణ విధానం

  1. అవసరమైన values తో Tuple ను create చేయండి.
  2. అవసరమైన operation ను ఎంచుకోండి.
  3. sum(), len(), in, count() లేదా loop ఉపయోగించండి.
  4. ఫలితాన్ని 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

marks = (78, 85, 90, 66)
ఈ line లో నాలుగు marks తో ఒక Tuple create అవుతుంది. Python ఈ values ను marks అనే variable లో store చేస్తుంది.
total = sum(marks)
sum() function Tuple లోని అన్ని numbers ను కలుపుతుంది. వచ్చిన మొత్తం total variable లో store అవుతుంది.
average = total / len(marks)
len(marks) marks సంఖ్యను ఇస్తుంది. Total ను marks సంఖ్యతో divide చేసి average కనుగొంటాము.
print(“Total:”, total)
ఈ line total marks ను screen పై చూపిస్తుంది.
print(“Average:”, average)
ఈ line calculated average ను screen పై చూపిస్తుంది.
days = (“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”)
ఈ line లో వారంలోని కొన్ని రోజుల పేర్లతో Tuple create అవుతుంది.
search_day = “Wednesday”
Search చేయాల్సిన రోజును search_day variable లో store చేస్తాము.
if search_day in days:
search_day value days Tuple లో ఉందా అని in operator పరీక్షిస్తుంది. Value ఉంటే if block execute అవుతుంది.
print(search_day, “is present”)
Value Tuple లో ఉన్నప్పుడు ఈ line ఆ value present అని message చూపిస్తుంది.
else:
if condition False అయినప్పుడు else block ప్రారంభమవుతుంది.
print(search_day, “is not present”)
Search చేసిన value Tuple లో లేకపోతే ఈ message print అవుతుంది.
numbers = (2, 4, 2, 6, 2, 8)
ఈ line లో numbers తో ఒక Tuple create చేస్తాము. ఇందులో 2 value పలుమార్లు ఉంది.
target = 2
ఎన్ని సార్లు ఉందో లెక్కించాల్సిన value ను target variable లో ఉంచుతాము.
count = numbers.count(target)
count() method target value Tuple లో ఎన్నిసార్లు ఉందో లెక్కిస్తుంది. ఆ సంఖ్య count variable లో store అవుతుంది.
print(“Count:”, count)
ఈ line target value యొక్క count ను screen పై చూపిస్తుంది.

Try It

Try this Python code:

Example

colors = ("red", "blue", "green", "blue")
target = "blue"
count = colors.count(target)
print("Color count:", count)

Try It Yourself »

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 value ను మార్చడానికి numbers[0] = 10 అని రాయడం.
Tuple immutable. Tuple లోని value ను నేరుగా మార్చలేరు. మార్పులు అవసరమైతే List ఉపయోగించాలి.
count() method ను variable లేకుండా తప్పుగా ఉపయోగించడం, ఉదాహరణకు numbers.count.
Method ను execute చేయడానికి parentheses ఉపయోగించాలి: numbers.count(2).
in operator ను if condition లేకుండా ఉపయోగించి result ను print చేయకపోవడం.
Value ఉందో లేదో చూపించడానికి if search_value in tuple_name: వంటి condition రాయండి.
Average కోసం Tuple length బదులుగా తప్పు సంఖ్యతో divide చేయడం.
Values సంఖ్యను సరిగ్గా తెలుసుకోవడానికి len(tuple_name) ఉపయోగించండి.
ఒక value ఉన్న Tuple ను comma లేకుండా రాయడం, ఉదాహరణకు value = (5).
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 ను నేరుగా మార్చలేము.

Test Your Knowledge

Take a short quiz based on this lesson.

10 Questions • Results
Scroll to Top