TUPLES AS RETURN VALUES TRB CSI › Python Programming › Tuples as return values

TUPLES AS RETURN VALUES

Python function-ல் ஒரே ஒரு value மட்டும் return செய்ய வேண்டும் என்ற rule இல்லை. ஒரு function: def student(): return "Arun", 85 இப்படி multiple values return செய்யலாம். இந்த: return "Arun", 85 Python-ல் conceptually: return ("Arun", 85) அதா…

Premium — locked Advanced 23 min 193 cards 47 programs 7 MCQs v4
This is a premium lesson. You can see the shape of every card — unlock to read them. Unlock

TUPLES AS RETURN VALUES - Pyt……

Premium This card is part of the premium lesson. Unlock
Definition

Python function-ல் ஒரே ஒரு value மட்டும் return செய்ய வேண்டும் என்ற rule இல்லை. ஒரு function: def student(): ……

Premium This card is part of the premium lesson. Unlock
Simple Explanation

Normal function: def add(): return 10 இது ஒரு value return செய்கிறது. 10 ஆனால்: def student(): return "Ravi",……

Premium This card is part of the premium lesson. Unlock
Flow
Basic flow

def get_values(): return 10, 20 Function call:……

Premium This card is part of the premium lesson. Unlock
Syntax
Basic syntax
Syntax
Call
Explanation

or tuple unpacking: a, b = fu……

Premium This card is part of the premium lesson. Unlock
Program
Program 1
Explanation

def get_numbers(): get_numbers() என்ற function create செய்கிறோம். return 10, 20 இங்கு 10 and 20 return செய்கி……

Premium This card is part of the premium lesson. Unlock
Explanation

Execution: get_numbers() called ↓ return 10, 20 ↓ Pyt……

Premium This card is part of the premium lesson. Unlock
Explanation
Check type

def get_data(): return 100, 2……

Premium This card is part of the premium lesson. Unlock

This proves function returns ……

Premium This card is part of the premium lesson. Unlock
Explanation
Tuple packing in return

Consider: return 10, 20, 30 Right side contains comma-separated values. Pytho……

Premium This card is part of the premium lesson. Unlock
Explanation
Parentheses optional

Both are valid: def test(): return 10……

Premium This card is part of the premium lesson. Unlock
Important

Tuple உருவாக parentheses முக்……

Premium This card is part of the premium lesson. Unlock
Explanation
Return value unpacking

Function tuple return செய்தால்……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

name, mark = get_data()…

Premium This card is part of the premium lesson. Unlock
Explanation
How unpacking works

Function: def get_data(): return "Raja", 80 Returns: ("Raja", 80) Then: name……

Premium This card is part of the premium lesson. Unlock
Explanation
Packing and unpacking

இந்த concept மிகவும் important. Function side: return "Raja", 80 Here: PACKING Caller side: name, mark = get_……

Premium This card is part of the premium lesson. Unlock
Explanation

return name, age, mark…

Premium This card is part of the premium lesson. Unlock
Explanation
Return directly to variables

Same function: def student(): name = "Kumar" age = 20 mark……

Premium This card is part of the premium lesson. Unlock
Explanation
Line by line explanation

def student(): Function definition. name = "Kumar" Local variable name. age = 20 Local variable age. mark = 9……

Premium This card is part of the premium lesson. Unlock
Explanation
Why use tuple return values

Suppose ஒரு function இரண்டு r……

Premium This card is part of the premium lesson. Unlock
Comparison
difference

இரண்டும் தேவை. Instead of creating two separate functions: def add(a, b): return a + b def subtract(a, b): ……

Premium This card is part of the premium lesson. Unlock
Program
Program 3 calculation
Explanation

return addition, subtraction ……

Premium This card is part of the premium lesson. Unlock
Dry Run
Dry run calculation

Function call: calculate(30, 10) Parameters: a = 30 b = 10 Calculation: addition = 30 + 10……

Premium This card is part of the premium lesson. Unlock
Explanation
Multiple calculations

A function can return many calculated results. def operations(a, b): return a + b, ……

Premium This card is part of the premium lesson. Unlock
Explanation
Return quotient and remainder

Very common example: Division செய்யும்போது: Quotient Remainder இரண்டும் return செய்ய……

Premium This card is part of the premium lesson. Unlock
Explanation

17 // 5 = 3 17 % 5 = 2 Return……

Premium This card is part of the premium lesson. Unlock
Real Life Example
Built in example divmod

Python-ல் இதே concept use செய……

Premium This card is part of the premium lesson. Unlock
Program
Example
Program
Check
Explanation
Divmod unpacking

q, r = divmod(17, 5) print(q)……

Premium This card is part of the premium lesson. Unlock
Program
Here
Program
Example
Explanation

small, large = find_values([1……

Premium This card is part of the premium lesson. Unlock

Returned tuple: (2, 50)…

Premium This card is part of the premium lesson. Unlock
Explanation
Average and total

def marks(m1, m2, m3): total = m1 + m2 + m3 average = t……

Premium This card is part of the premium lesson. Unlock
Real Life Example
Real time example

Student result function: def result(m1, m2, m3): total = m1 + m2 + m3 average = total / 3 if average >=……

Premium This card is part of the premium lesson. Unlock

One function returns: Total A……

Premium This card is part of the premium lesson. Unlock
Explanation
Returning different data types

Tuple return values எல்லாம் same datatype இருக்க வேண்டிய அவசியம் இல்லை. d……

Premium This card is part of the premium lesson. Unlock
Explanation

name, age, mark, passed = stu……

Premium This card is part of the premium lesson. Unlock
Explanation
Return value as single variable

Function return tuple-ஐ unpac……

Premium This card is part of the premium lesson. Unlock
Program
Example
Program
Now tuple indexing பயன்படுத்தலாம்
Explanation
Two ways to receive

Function: def get_data(): return 1……

Premium This card is part of the premium lesson. Unlock
Output
Result
Program
Access
Output
Result

Both are valid.…

Premium This card is part of the premium lesson. Unlock
Comparison

Entire Tuple result = get_dat……

Premium This card is part of the premium lesson. Unlock
Output
Result

Access through indexing: resu……

Premium This card is part of the premium lesson. Unlock
Output
Result

Access directly using variabl……

Premium This card is part of the premium lesson. Unlock
Important
Important count rule

Returned values count and receiving variables count normally equal ……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 1

def test(): return 10, 20, 30 a, b = test(……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Error

ValueError: too many values t……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 2

def test(): return 10, 20 a, ……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Error

ValueError: not enough values……

Premium This card is part of the premium lesson. Unlock
Explanation
Star unpacking

Function நிறைய values return செய்தால் starred unpacking u……

Premium This card is part of the premium lesson. Unlock
Important

*b remaining values-ஐ list-ஆக……

Premium This card is part of the premium lesson. Unlock
Explanation
Star in middle

def numbers(): return 10, 20, 30, ……

Premium This card is part of the premium lesson. Unlock

Flow: Returned: (10, 20, 30, ……

Premium This card is part of the premium lesson. Unlock
Explanation
Ignore return value

Function சில values return செய்……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

name, _, mark = student()…

Premium This card is part of the premium lesson. Unlock

Here: 20 தேவையில்லை. Conventi……

Premium This card is part of the premium lesson. Unlock
Explanation
Returning nested tuple

Function nested tuple return செய்யலாம். d……

Premium This card is part of the premium lesson. Unlock

Nested unpacking: name, (m1, ……

Premium This card is part of the premium lesson. Unlock
Explanation
Return statement stops function

Important return concept: Once return executed, func……

Premium This card is part of the premium lesson. Unlock

print("Hello") execute ஆகாது.……

Premium This card is part of the premium lesson. Unlock
Explanation
Return without value

If: def test(): return Then r……

Premium This card is part of the premium lesson. Unlock
Program
Example

This is different from return……

Premium This card is part of the premium lesson. Unlock
Explanation
Empty tuple return

This: def test(): return () returns: () which is……

Premium This card is part of the premium lesson. Unlock
Explanation
Single item tuple return

Very important exam concept. Suppose: d……

Premium This card is part of the premium lesson. Unlock

Because: (10) is only grouped ……

Premium This card is part of the premium lesson. Unlock
Program
Now
Remember

return (10) returns: 10 Type: int But: retu……

Premium This card is part of the premium lesson. Unlock
Comparison
Function return vs print

Students commonly confuse ret……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

This displays values, but doe……

Premium This card is part of the premium lesson. Unlock
Explanation

x becomes: None Correct retur……

Premium This card is part of the premium lesson. Unlock
Program
Now
Explanation

x becomes: ('Ravi', 90)…

Premium This card is part of the premium lesson. Unlock
Comparison
Return vs print comparison
Program
Example
Explanation

does not return tuple. But:…

Premium This card is part of the premium lesson. Unlock
Explanation

returns: (10, 20)…

Premium This card is part of the premium lesson. Unlock
Explanation
Function composition use

Tuple returns can be passed t……

Premium This card is part of the premium lesson. Unlock
Program
Example

Flow: get_numbers() ↓ (10, 20……

Premium This card is part of the premium lesson. Unlock
Explanation
Better unpacking version

def get_numbers(): return 10, 20 def a……

Premium This card is part of the premium lesson. Unlock
Explanation
Real world use cases

Tuples as return values மிகவும் useful when function needs to return related information. Examples: Student: ……

Premium This card is part of the premium lesson. Unlock
Explanation
Advantages

Tuples as return values use செய்தால்: Multiple related values ஒரே function-ல் return செய்யலாம். Multiple sepa……

Premium This card is part of the premium lesson. Unlock
Comparison
Comparison with list

Function tuple return: def te……

Premium This card is part of the premium lesson. Unlock
Output
Result

Tuple is immutable. Function ……

Premium This card is part of the premium lesson. Unlock
Output
Result

List is mutable.…

Premium This card is part of the premium lesson. Unlock
Comparison
Difference
Important

return a, b means: return (a, b) not: return two independent objects s……

Premium This card is part of the premium lesson. Unlock
Remember

Function says: return a, b, c Python does: PACK → (a, b, c) Calle……

Premium This card is part of the premium lesson. Unlock
TRB Point
Question

def fun(): return 10, 20 x = fun() print(type(x)) ……

Premium This card is part of the premium lesson. Unlock
TRB Point
Trb point 2

def fun(): return 1, 2, 3 a, ……

Premium This card is part of the premium lesson. Unlock
TRB Point
Trb point 3

def fun(): return (10) print(type(fun())) An……

Premium This card is part of the premium lesson. Unlock
TRB Point
Trb point 4

What is returned by: def fun(……

Premium This card is part of the premium lesson. Unlock
TRB Point
Trb point 5

def fun(): return 1, 2 a, b, ……

Premium This card is part of the premium lesson. Unlock
Output
Result

Reason: 2 returned elements b……

Premium This card is part of the premium lesson. Unlock
Interview Point
Question

Does Python really return multiple values from a function? Best answer: Technically, Python function returns ……

Premium This card is part of the premium lesson. Unlock
Interview Point
Interview point 2

What is the difference between: result = fun() and: a, b = fun()……

Premium This card is part of the premium lesson. Unlock
Common Mistake

Wrong assumption: return a, b means two separate retur……

Premium This card is part of the premium lesson. Unlock
MCQ

What is the output? def fun()……

Premium This card is part of the premium lesson. Unlock
MCQ

What is the type? def fun(): ……

Premium This card is part of the premium lesson. Unlock
MCQ

What is the output? def fun()……

Premium This card is part of the premium lesson. Unlock
MCQ

What happens? def fun(): retu……

Premium This card is part of the premium lesson. Unlock
MCQ

What is the output? def fun()……

Premium This card is part of the premium lesson. Unlock
Mcq 6

Which creates a one-element tuple? A) return (……

Premium This card is part of the premium lesson. Unlock
MCQ

What is the value of b? def f……

Premium This card is part of the premium lesson. Unlock
MCQ

What does this function retur……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 1

Predict: def get(): return 10……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 2

Predict: def get(): return "A……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 3

Predict: def calculate(x, y): return ……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 4

Predict: def test(): return 1, 2,……

Premium This card is part of the premium lesson. Unlock
Summary

Tuples as Return Values என்பது Python f……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

Actually returns: ("Ravi", 90……

Premium This card is part of the premium lesson. Unlock
Explanation

or unpack: name, mark = stude……

Premium This card is part of the premium lesson. Unlock
Explanation

Most important exam statement: Python function appears t……

Premium This card is part of the premium lesson. Unlock
Text size
17px
Theme
Contents
Print / PDF
Program