Welcome back to Pandas Daily! Master Python in just 5 minutes a day. β Today: Understand Python's "random" module to generate random numbers, create random samples for simulations, make random choices etc. Mini exercise at the end! β Before we start, a quick question for you!
Thanks! Now, lets begin! What is random module and why you should care?Simply put- random module generates random numbers and random choices. Some use cases:
Lets go into the code β π« Import/Load the module
In:
import random
Out:
β π§© Generate a floating (decimal) number between 0 and 1
In:
print(random.random())
Out: 0.7905682961988124
β π± Seeding: To reproduce exact same results (very important as "random" will not give same output) 1οΈβ£ We produce a random number 'a' once
In:
random.seed(42) a = random.random() print(a)
Out: 0.6394267984578837
β 2οΈβ£ Can we get the same random number again as 'b'?
In:
b = random.random() print(b)
Out: 0.025010755222666936
β 3οΈβ£ How to make sure we get the same output every time? Use same seed
In:
random.seed(42) b = random.random() print(b)
Out: 0.6394267984578837
Moving on.. β π² Any floating number between two numbers
In:
print(random.uniform(10.5, 20.5))
Out: 13.745401188473625
β #οΈβ£ Generate integer in a given range using randint(start, end)
In:
print(random.randint(1, 10))
Out: 7
β πͺ 1 step further - Produce numbers in a given range but only at specific intervals: randrange(start, end, interval) chose from 0,5,10,15,20
In:
print(random.randrange(0, 20, 5))
Out: 15
β βοΈ Random Choice: When you don't know the right answer in the exam π
In:
print(random.choice([True, False]))
Out: True
β πΈ Another Random Choice: Selecting a fruit or a lottery
In:
items = ["apple", "banana", "cherry"] print(random.choice(items))
Out: banana
ποΈ Mini Exercise (Reply with your code) β Use
βπ£ That's it for today! If you liked it, please share it with anyone who will find it useful and share your feedback below πΌ β
|
Beginner to Expert in Python in just 5 minutes
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] =...