Khalid | Python Tasks Adventurer 🐍

271 posts

Khalid | Python Tasks Adventurer 🐍 banner
Khalid | Python Tasks Adventurer 🐍

Khalid | Python Tasks Adventurer 🐍

@Python__Task

🐍 Python enthusiast | Sharing Python tasks to solve 🖥️ | Let's code together! 🚀 #Python_Task 🐍 #Py_Wisdom

参加日 Eylül 2021
25 フォロー中8 フォロワー
Python Programming
Python Programming@PythonPr·
Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!
Python Programming tweet media
English
20
8
113
11.1K
Khalid | Python Tasks Adventurer 🐍
Wait… aren’t they the same? 🐍 sample() makes sure you get different items — no repeats allowed. ⚠️ But if you ask for more items than are in the list, it will raise an error! 🎲 choices() is totally okay with duplicates — you might see the same item twice (or more). #Python
English
0
0
0
29
Khalid | Python Tasks Adventurer 🐍
❌ Let’s look at the other options: 1. List – ✅ Valid, but it's mutable, not ideal for constants. 3. Set – ❌ Not allowed with random.choice() – sets are unordered. 4. String – Technically works, but it's not clear you're choosing from multiple items.
English
0
0
0
26
Khalid | Python Tasks Adventurer 🐍
✅ Answer The correct answer is: 2. x = ("A", "B", "C") (Tuple) 🎉 Why? ✔️ Tuples are immutable, so they’re safer for fixed choices. ⚡ More memory-efficient than lists. 🧭 They're ordered and indexable, so random.choice() works perfectly
English
1
0
0
33