Python Statements
Python return Statements
Last Updated on April 16, 2023 by Masud Alam

Python return Statement
Python return statement হচ্ছে ফাংশন এবং মেথড গুলোর একটি key component বা মূল উপাদান। পাইথনে ফাংশন থেকে কোনো ফাইনাল value পেতে return statement ব্যবহার করা হয়। আমরা শুধুমাত্র একটি ফাংশনে একবার return statement ব্যবহার করতে পারি। এটি পাইথন ফাংশনের বাইরে ব্যবহার করা যায় না। এইটাকে নিম্নোক্ত কয়েকটি ভাগে সংজ্ঞায়িত করতে পারি :
- python return statement একটি ফাংশনে ব্যবহার করা হয় যাতে কলার প্রোগ্রামে কিছু রিটার্ন করা হয়।
- আমরা শুধুমাত্র একটি ফাংশনের ভিতরে return statement ব্যবহার করতে পারি।
- পাইথনে, প্রতিটি ফাংশন কিছু return করে। যদি কোন return statement না থাকে, তাহলে এটি None রিটার্ন করে ।
- যদি return statement একটি expression থাকে, তবে এটি প্রথমে মূল্যায়ন করা হয় এবং তারপর value টি return দেওয়া হয়।
- return statement ফাংশন execution বন্ধ করে দেয়।
- একটি ফাংশনে একাধিক return statement থাকতে পারে। যখন তাদের যে কোনটি কার্যকর করা হয়, ফাংশনটি বন্ধ হয়ে যায়।
- একটি ফাংশন একাধিক ধরনের value return দিতে পারে।
- পাইথন ফাংশন single return statement এ একাধিক value return দিতে পারে।
Python Beginning To Advance with Blockchain, Machine Learning and Data Science Course
১. Python return Statement Example
চলুন প্রথমে দুটি সংখ্যা যোগ করার এবং ফাঙ্কশন কলকারীকে কে total value return দেওয়ার জন্য একটি সাধারণ উদাহরণ দেখি:
def add(x, y):
total = x + y
return total
আমরা return statement এ expression রেখে function টি optimize করতে পারি:
def add(x, y):
return x + y
২.প্রতিটি ফাংশনই কিছু না কিছু রিটার্ন করে
চলুন দেখি কোন ফাংশনের রিটার্ন স্টেটমেন্ট না থাকলে কি রিটার্ন করা হয়:
def foo():
pass
print(foo()) #None
৩.Python return statement এ কিছু না থাকলে কি হবে?
যখন রিটার্ন স্টেটমেন্টের কোন মান থাকে না তখন ফাংশনটি None প্রদান করে:
def foo():
return
print(foo()) #None
Python Beginning To Advance with Blockchain, Machine Learning and Data Science Course
৪.পাইথন ফাংশনে একাধিক রিটার্ন স্টেটমেন্ট থাকতে পারে:
def type_of_int(i):
if i % 2 == 0:
return 'even'
else:
return 'odd'
৫.পাইথন ফাংশন গুলো একাধিক type এর value রিটার্ন করতে পারে
আসুন একটি উদাহরণ দেখি যেখানে ফাংশনটি একাধিক ধরণের মান প্রদান করবে।
def get_demo_data(object_type):
if 'str' == object_type:
return 'test'
elif 'tuple' == object_type:
return (1, 2, 3)
elif 'list' == object_type:
return [1, 2, 3]
elif 'dict' == object_type:
return {"1": 1, "2": 2, "3": 3}
else:
return None
get_demo_data("tuple")
৬.একটি সিঙ্গেল রিটার্ন স্টেটমেন্টে একাধিক মান রিটার্ন করা
আমরা একটি রিটার্ন স্টেটমেন্ট থেকে একাধিক value কে return করতে পারি। এই value গুলি একটি কমা (,) দ্বারা পৃথক করা হয় এবং caller প্রোগ্রামে একটি tuple হিসাবে ফিরে আসে।
def return_multiple_values():
return 1, 2, 3
print(return_multiple_values())
print(type(return_multiple_values()))
Python Beginning To Advance with Blockchain, Machine Learning and Data Science Course
৭.finally block সহ Python return statements
যখন রিটার্ন স্টেটমেন্ট একটি try-except block এর ভিতরে execute করা হয়, finally block code টি কলারের কাছে value টি return করার আগে প্রথমে execute করা হয়।
def hello():
try:
return 'hello try'
finally:
print('finally block')
def hello_new():
try:
raise TypeError
except TypeError as te:
return 'hello except'
finally:
print('finally block')
print(hello())
print(hello_new())
ফাইনালি ব্লকের যদি python return statement থাকে, তাহলে আগের return statement ইগনোর করা হয় এবং finally block থেকে ভ্যালু রিটার্ন করা হয়।
def hello():
try:
return 'hello try'
finally:
print('finally block')
return 'hello from finally'
print(hello())