🎯 choice() vs choices()


Welcome back to Pandas Daily! Master Python in just 5 minutes a day.

​

Yesterday, we learned the basics of "random" module β€” generating random numbers, setting seeds, and making simple picks.

Today, we go beyond numbers - learn how to shuffle lists, pick items fairly or by weight, split datasets to train AI models.

​

♠️ "Shuffle" to change the order

In:
import random
cards = [1, 2, 3, 4, 5]
random.shuffle(cards)
print(cards)
Out: [3, 1, 5, 2, 4]

​

πŸͺΉ Nested Lists can be shuffled too

In:
seats = [[1,2], [3,4], [5,6], [7,8]]
random.shuffle(seats)
print(seats)
Out: [[7, 8], [5, 6], [1, 2], [3, 4]]

​

βœ… To select 1 item πŸ‘‰ "choice"

works with lists too

In:
letters = "abcde"
char = random.choice(letters)
print(char)
Out: c

​

βœ…βœ… To select multiple πŸ‘‰ "choices" (same item can be picked multiple times with choices)

k is number of items. See that same item is chosen twice

In:
tickets = ["A10", "B22", "C15", "D09"]
winners = random.choices(tickets, k=2)
print(winners)
Out: ['C15', 'C15']

​

β˜‘οΈβœ… To not repeat selection πŸ‘‰ "sample"

In:
colors = ["red", "blue", "green", "yellow", "purple"]
sampled = random.sample(colors, 3)
print(sampled)
Out: ['blue', 'yellow', 'green']

​

πŸ’ͺπŸ’ͺ Weights: Till now items were selected fairly, meaning they had equal chance of being picked up.

What if I like "cherry" more? Then we assign weights i.e. higher weight to item we want more (cherry) and lower to we want less (apple)

In:
items = ["apple", "banana", "cherry"]
probs = [0.1, 0.3, 0.6]
result = random.choices(items, weights=probs, k=1)
print(result)
Out: ['cherry']

​

πŸ€– Train-Test split for AI models

train - data on which model learns (7 items). test - independent data on which the model is tested (rest 3)

In:
rows = list(range(10))
random.shuffle(rows)
train = rows[0:7]
test = rows[7:]
print(train, test)
Out: [8, 3, 1, 4, 7, 0, 9] [6, 2, 5]

​

🎲 getrandbits(1) - pick True or False randomly

In:
flag = bool(random.getrandbits(1))
print(flag)
Out: True

πŸ‹οΈ Mini Exercise (Reply with your code)

​

Make a list of 10 numbers.

  1. Shuffle it twice with random.shuffle() and print the results.
  2. Use random.sample() to pick 3 numbers without replacement.

β­πŸ“£ That's it for today! If you liked it, please share it with anyone who will find it useful and share your feedback below 🐼

​

Pandas Daily

Beginner to Expert in Python in just 5 minutes

Read more from Pandas Daily

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. We all agree that dashboards, emails, logs should read like English. Meaning 1.2 million beats 1234567 and 3.1M is more readable than 3145728. Stakeholders scan - and they get pissed if they have to parse messy numbers 😭😭 So today, we explore a third-party humanize library. Format numbers, file sizes, and dates/times into human-readable text. Mini exercise at the end. πŸ₯‡ Ready, Get, Set, Go! Install and...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Amazon reviews, chats and social media text (posts, comments, stories etc) are full of emojis πŸ”₯πŸ”₯ Great for expression! But give a hard time for text interpretation (Amazon, Meta would be going crazy). That is where Python's demoji library comes in. From a given text you can: Extract emojis Remove them if needed Replace with textual meaning Understand sentiment and what not... Let's begin!! Install demoji...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. We all know Python’s basic container data types β€” lists, dictionaries, tuples, sets. "Container" is just a data type that holds multiple items instead of single values. However, many times basics are not enough. Like if you want count of different items in the list you will run a loop like below. In: fruits = ["apple", "banana", "apple", "cherry"] counts = {} for item in fruits: counts[item] =...