Playing with Jupyter Notebooks and Python
Python demonstration with Jupyter Notebooks
Playing with Jupyter Notebooks and Python
import wikipedia # import wikipedia library to generate random titles and access page contents
import emoji
# store number of articles as a constant
ARTICLES = 5
titles = wikipedia.random(5) # random list of 5 article titles on Wikipedia
count = 0 # store average word count
emojis = [":zero:", ":one:", ":two:", ":three:", ":four:", ":five:", ":six:", ":seven:", ":eight:", ":nine:"]
mean = ""
# iterate through titles
for title in titles:
while True:
try:
# attempt to find the page corresponding to the title
page = wikipedia.page(title)
break
except wikipedia.exceptions.PageError:
# else find a new article
title = wikipedia.random()
# iterate through the 5 article contents and count words
for i in range(ARTICLES):
# split it by spaces using python .split() method
length = len(wikipedia.page(titles[i]).content.split())
# show user the title and the word count of each article
print(str(i + 1) + ". " + titles[i] + ": " + str(length))
# add word count to length
count += length
# divide total word count by 5
count /= ARTICLES
# we round the word count to avoid the period
# iterate through the digits of the number
for num in str(round(count)):
# turn each digit into an emoji and add it to the mean
mean += emoji.emojize(emojis[int(num)], language='alias')
# print out final average word count
print(f"Average word count of the {ARTICLES} random articles: {mean}")
1. Battery L, 1st Illinois Light Artillery Regiment: 429
2. Steffen Mellemseter: 45
3. Aleksandrs Leimanis: 71
4. James Sullivan Lincoln: 532
5. Lobesia euphorbiana: 20
Average word count of the 5 random articles: 2️⃣1️⃣9️⃣