Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Python Randfacts Library



Python randfacts library is used for generating random fun facts. It allows you to quickly fetch random facts from a predefined set of categories or generate random facts with minimal setup.

Installing randfacts Library

To work with randfacts library, you first need to install it. This can be done easily using pip, Python's package installer. Open your terminal or command prompt and run the following command −

pip install randfacts

Importing randfacts Library

Once library is installed, you can import randfacts into your Python script to access its functionality. Here's how you do it −

import randfacts

Generating Random Facts

The primary function of randfacts is to generate random facts. You can retrieve a single random fact using the get_fact() method.

Example

import randfacts
fact = randfacts.get_fact()
print(fact)

Output

RUN 1:
Did you know? A crocodile cannot stick its tongue out
RUN 2:
Taipan snakes have 50 times more toxic than a cobra snake

Generating Multiple Random Facts

To generate multiple random facts, you can use get_fact() multiple times within a loop.

Example

In the following example, we are generating and printing ten random facts −

import randfacts
for _ in range(10):
   fact = randfacts.get_fact()
   print(fact)

Output

The dot that appears over the letter i is called a "tittle."
A Father's Diet Before Conception Plays a Crucial Role in a Child's Health.
The hottest chili in the world is the Tezpur chili pepper
The U.S. Navy has 75 trained dolphins to detect enemy swimmers and underwater mines.
Brazil was once called "United States of Brazil."
The MGM lion roar is trademarked
Parts of the Great Wall of China were made with sticky rice.
The moon is moving away from us by 3.78 cm (1.48 in) a year.
In October 1986, Pepsi paid close to $840 million to Nabisco for the Kentucky Fried Chicken empire
Over 800 languages are spoken in Papua New Guinea.

Content Filtering while Generating Random Facts

The randfacts library allows you to filter the content of the facts generated. By default, get_fact() filters out explicit content. However, you can control this behavior by passing a boolean argument to the function. Passing True enables the filter (default behavior), while passing False disables it.

Example

import randfacts

# Generate a fact with content filtering enabled (default)
print("Fact with content filtering enabled")
safe_fact = randfacts.get_fact(True)
print(safe_fact)

# Generate a fact with content filtering disabled
print("Fact with content filtering disabled")
unsafe_fact = randfacts.get_fact(False)
print(unsafe_fact)

Output

Fact with content filtering enabled
The characters Bert and Ernie on Sesame Street were named after Bert the cop and Ernie the taxi driver in Frank Capra's It's a Wonderful Life.
Fact with content filtering disabled
In Utah, it is illegal to swear in front of a dead person.

Randomfacts Library Using Command Line

The randfacts can also be executed directly from the command line.

Here are some commands you can use −

Generate a random fact

python3 -m randfacts

Generate a fact with explicit content filtering disabled

python3 -m randfacts --unsafe

Generate a mix of safe and unsafe facts

python3 -m randfacts mixed
python_projects_from_basic_to_advanced.htm
Advertisements