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

Bergabung Eylรผl 2021
25 Mengikuti8 Pengikut
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