❓با توجه به کد زیر، خروجی نهایی دو دستور print چیست؟
و کدام یک از دو آبجکت a و b شناسهٔ (id) جدید میگیرد؟
❓️Given the code below, what is the final output of the two print?
And which one of the two objects, a or b, receives a new id?
🔍 نکتهٔ
● تاپل (tuple) غیرقابلتغییر است و += یک آبجکت جدید میسازد.
● لیست (list) قابلتغییر است و += روی همان آبجکت قبلی اعمال میشود.
#️⃣ #برنامه_نویسی #آموزش_پایتون #پای_ویژن
#Python #PythonChallenge #PythonTypes #CodingChallenge #PyVision
🌐 @PyVision
و کدام یک از دو آبجکت a و b شناسهٔ (id) جدید میگیرد؟
❓️Given the code below, what is the final output of the two print?
And which one of the two objects, a or b, receives a new id?
a = (1, 2, 3)
b = [1, 2, 3]
a += (4, 5)
b += [4, 5]
print(a)
print(b)
🔍 نکتهٔ
● تاپل (tuple) غیرقابلتغییر است و += یک آبجکت جدید میسازد.
● لیست (list) قابلتغییر است و += روی همان آبجکت قبلی اعمال میشود.
#️⃣ #برنامه_نویسی #آموزش_پایتون #پای_ویژن
#Python #PythonChallenge #PythonTypes #CodingChallenge #PyVision
🌐 @PyVision
🔥3
PyVision | پایویژن
❓با توجه به کد زیر، خروجی نهایی دو دستور print چیست؟ و کدام یک از دو آبجکت a و b شناسهٔ (id) جدید میگیرد؟ ❓️Given the code below, what is the final output of the two print? And which one of the two objects, a or b, receives a new id? a = (1, 2, 3) b = [1…
✅ پاسخ چالش
🔢 خروجی دستور های ()print:
🟢 در این چالش با تفاوت بین دو نوع دادهی مهم روبهرو هستیم:
● تاپل (tuple) یک نوع داده غیرقابلتغییر (immutable) است.
بنابراین عملگر += نمیتواند آن را در همان حافظه تغییر دهد و یک آبجکت جدید میسازد؛ به همین دلیل id(a) تغییر میکند.
● لیست (list) یک نوع داده قابلتغییر (mutable) است.
عملگر += روی لیست بهصورت in-place اجرا میشود و عناصر جدید به همان آبجکت قبلی اضافه میشوند؛ بنابراین id(b) تغییر نمیکند.
📌 نتیجه:
● a → آبجکت جدید میگیرد
● b → همان آبجکت قبلی باقی میماند
🟢 This challenge highlights the difference between mutable and immutable types in Python:
● A tuple is immutable, so the += operator cannot modify it in place.
Instead, Python creates a new object, which means the id of a changes.
● A list is mutable, and += modifies it in place.
New elements are added to the same object, so the id of b remains unchanged.
📌 Final result:
● a → gets a new object (id changes)
● b → stays the same object (id does not change)
#️⃣ #آموزش_پایتون #چالش #نوع_داده #برنامه_نویسی
#Python #PythonChallenge #PythonTypes #Programming #PyVision
🌐 @PyVision
🔢 خروجی دستور های ()print:
(1, 2, 3, 4, 5)
[1, 2, 3, 4, 5]
🟢 در این چالش با تفاوت بین دو نوع دادهی مهم روبهرو هستیم:
● تاپل (tuple) یک نوع داده غیرقابلتغییر (immutable) است.
بنابراین عملگر += نمیتواند آن را در همان حافظه تغییر دهد و یک آبجکت جدید میسازد؛ به همین دلیل id(a) تغییر میکند.
● لیست (list) یک نوع داده قابلتغییر (mutable) است.
عملگر += روی لیست بهصورت in-place اجرا میشود و عناصر جدید به همان آبجکت قبلی اضافه میشوند؛ بنابراین id(b) تغییر نمیکند.
📌 نتیجه:
● a → آبجکت جدید میگیرد
● b → همان آبجکت قبلی باقی میماند
🟢 This challenge highlights the difference between mutable and immutable types in Python:
● A tuple is immutable, so the += operator cannot modify it in place.
Instead, Python creates a new object, which means the id of a changes.
● A list is mutable, and += modifies it in place.
New elements are added to the same object, so the id of b remains unchanged.
📌 Final result:
● a → gets a new object (id changes)
● b → stays the same object (id does not change)
#️⃣ #آموزش_پایتون #چالش #نوع_داده #برنامه_نویسی
#Python #PythonChallenge #PythonTypes #Programming #PyVision
🌐 @PyVision
👍3🤩1