Python and Jupyter Notebooks

Python is a highly versatile and widely-used programming language, renowned for its readability and broad library support. Jupyter Notebooks, on the other hand, is an interactive computing environment that enables users to create and share documents containing live code, equations, visualizations, and narrative text. Together, they form a powerful toolkit for data analysis, scientific research, and educational purposes.

We will play with Python and Jupyter Notebooks to get a feel for both. This is a great interactive way to start development.

Emoji Print

It is easy to add an emoji to a message in code. However, using the emoji library or other libraries often requires you to install code on your machine. Before using a library, that is not part of Python distribution, you must install with pip

# terminal command to install library
$ pip install emoji
Collecting emoji
  Downloading emoji-2.5.1.tar.gz (356 kB)
...
Successfully installed emoji-2.5.1
#!pip install emoji
from emoji import emojize 
print(emojize(":thumbs_up: Python is awesome! :grinning_face:"))
👍 Python is awesome! 😀

Extracting Data

Web sites become a lot more interesting when you are working with data, not trying to create it. Here is some code using a library called newspaper, this extracts a couple of writeups from the CNN Entertainment site.

#!pip install newspaper3k
from newspaper import Article
from IPython.display import display, Markdown


urls = ["http://cnn.com/2023/03/29/entertainment/the-mandalorian-episode-5-recap/index.html", 
        "https://www.cnn.com/2023/06/09/entertainment/jurassic-park-anniversary/index.html"]

for url in urls:
    article = Article(url)
    article.download()
    article.parse()
    # Jupyter Notebook Display
    # print(article.title)
    display(Markdown(article.title)) # Jupyter display only
    display(Markdown(article.text)) # Jupyter display only
    print("\n")

#!pip install wikipedia
import wikipedia
from IPython.display import display, Markdown # add for Jupyter

terms = ["Python (programming language)", "JavaScript"]
for term in terms:
    # Search for a page 
    result = wikipedia.search(term)
    # Get the summary of the first result
    summary = wikipedia.summary(result[0])
    print(term) 
    # print(summary) # console display
    display(Markdown(summary)) # Jupyter display
Python (programming language)

Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation. Python is dynamically typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly procedural), object-oriented and functional programming. It is often described as a “batteries included” language due to its comprehensive standard library. Guido van Rossum began working on Python in the late 1980s as a successor to the ABC programming language and first released it in 1991 as Python 0.9.0. Python 2.0 was released in 2000. Python 3.0, released in 2008, was a major revision not completely backward-compatible with earlier versions. Python 2.7.18, released in 2020, was the last release of Python 2. Python consistently ranks as one of the most popular programming languages, and has gained widespread use in the machine learning community.

JavaScript

JavaScript (), often abbreviated as JS, is a programming language and core technology of the Web, alongside HTML and CSS. 99% of websites use JavaScript on the client side for webpage behavior. Web browsers have a dedicated JavaScript engine that executes the client code. These engines are also utilized in some servers and a variety of apps. The most popular runtime system for non-browser usage is Node.js. JavaScript is a high-level, often just-in-time compiled language that conforms to the ECMAScript standard. It has dynamic typing, prototype-based object-orientation, and first-class functions. It is multi-paradigm, supporting event-driven, functional, and imperative programming styles. It has application programming interfaces (APIs) for working with text, dates, regular expressions, standard data structures, and the Document Object Model (DOM). The ECMAScript standard does not include any input/output (I/O), such as networking, storage, or graphics facilities. In practice, the web browser or other runtime system provides JavaScript APIs for I/O. Although Java and JavaScript are similar in name, syntax, and respective standard libraries, the two languages are distinct and differ greatly in design.

Inspecting a Function

The inspect module can give you the output of what’s inside many Python functions/objects. This can help you explore code behind what you are using.

import inspect 
from newspaper import Article

# inspect newspaper Article function
print(inspect.getsource(Article))

Python Data Types

Dynamic typing means that the type of the variable is determined only during runtime. Strong typing means that variables do have a type and that the type matters when performing operations. In the illustration below there are two functions

  • mean… shows types required prior to calling average function
  • average, average2… calculates the average of a list of numbers

Python has types. In the language you can use type hints, but most coders do not use them. In other languages like Java and ‘C’ you must specify types.

import sys
from typing import Union

# Define types for mean function, trying to analyze input possibilities
Number = Union[int, float]  # Number can be either int or float type
Numbers = list[Number] # Numbers is a list of Number types
Scores = Union[Number, Numbers] # Scores can be single or multiple 

def mean(scores: Scores, method: int = 1) -> float:
    """
    Calculate the mean of a list of scores.
    
    Average and Average2 are hidden functions performing mean algorithm

    If a single score is provided in scores, it is returned as the mean.
    If a list of scores is provided, the average is calculated and returned.
    """
    
    def average(scores): 
        """Calculate the average of a list of scores using a Python for loop with rounding."""
        sum = 0
        len = 0
        for score in scores:
            if isinstance(score, Number):
                sum += score
                len += 1
            else:
                print("Bad data: " + str(score) + " in " + str(scores))
                sys.exit()
        return sum / len
    
    def average2(scores):
        """Calculate the average of a list of scores using the built-in sum() function with rounding."""
        return sum(scores) / len(scores)

    # test to see if scores is  a list of numbers
    if isinstance(scores, list):
        if method == 1:  
            # long method
            result = average(scores)
        else:
            # built in method
            result = average2(scores)
        return round(result + 0.005, 2)
    
    return scores # case where scores is a single valu

# try with one number
singleScore = 100
print("Print test data: " + str(singleScore))  # concat data for single line
print("Mean of single number: " + str(mean(singleScore)))

print()

# define a list of numbers
testScores = [90.5, 100, 85.4, 88]
print("Print test data: " + str(testScores))
print("Average score, loop method: " + str(mean(testScores)))
print("Average score, function method: " +  str(mean(testScores, 2)))

print()

badData = [100, "NaN", 90]
print("Print test data: " + str(badData))
print("Mean with bad data: " + str(mean(badData)))


Hacks

Here is a summary of some of the things learned above.

  • Formatting messages with emoji
  • Exploring data with newspaper and wikipedia libraries
  • Finding code on how the library we used was made
  • Learning about data types while writing an algorithm for mean

Part of Project Based learning is the idea of combining concepts to form something more interesting. Make a plan, form some ideas, brainstorm ideas with pair. Produce something that is interesting and challenging. Samples…

import os
import wikipedia
from emoji import emojize
import google.generativeai as genai
import re

# api key for gemini
genai.configure(api_key="API_KEY")
model = genai.GenerativeModel("gemini-1.5-flash")

# enter a topic/keyword. search wikipedia and get the summary
input_topic = input("Enter a topic or keyword: ")
result = wikipedia.search(input_topic)
summary = wikipedia.summary(result[0])

# prompt for the model
prompt = """
Using the paragraph given, separate the paragraph (WITHOUT CHANGING THE ACTUAL WORD) into different sections or topics.
Then, generate a corresponding emoji for that paragraph. Here is an example:

unicodeCode - whatever content you chose to go into paragraph 1
unicodeCode - content you chose to go into paragraph 2
and so on

The unicodeCode is the unicode for an emoji. This is just backslash + U escape sequence followed by the hexadecimal code point for the desired emoji.
Only include your response and nothing else. Do not format text with ** for bold or anything else. 
"""

# content generation
response = model.generate_content(f"{prompt} {summary}")

# extract text
extraction = response._result.candidates[0].content.parts[0].text


# unicode parser
def parseUnicode(text):
    return re.sub(r"\\U([0-9A-Fa-f]{8})", lambda m: chr(int(m.group(1), 16)), text)


# emojify
converted_text = parseUnicode(extraction)
print(emojize(converted_text))
/home/administrator/Aashray/Apps/Anaconda/anaconda3/envs/blog/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm


🐦 - Minecraft is a 2011 sandbox game developed and published by Mojang Studios. Originally created by Markus "Notch" Persson using the Java programming language, it was developed over the span of two years, with many public test builds being released from May 2009 until its full release on 18 November 2011. After the game's full release, Persson gave Jens "Jeb" Bergensten control over the game's development. In the years since its release, it has been ported to several platforms, including smartphones and various video game consoles, primarily by 4J Studios. In 2014, Mojang and the Minecraft intellectual property were purchased by Microsoft for US$2.5 billion. Minecraft has become the best-selling video game of all-time, with over 300 million copies sold and nearly 140 million monthly active players as of 2023. 

🌀 - In Minecraft, players explore a procedurally generated, three-dimensional world with virtually infinite terrain made up of voxels. Players can discover and extract raw materials, craft tools and items, and build structures, earthworks, and machines. Depending on their chosen game mode, players can fight hostile mobs, as well as cooperate with or compete against other players. The game has two main modes; one being survival mode, where players must acquire resources to survive, and a creative mode where players have unlimited resources and the ability to fly. Several other game modes exist besides the two main ones, such as one that allows players to spectate others and one that plays identically to survival mode, but features permadeath. The game's large community also offers a wide variety of user-generated content, such as modifications, servers, skins, texture packs, and custom maps, which add new game mechanics and possibilities.

🎉 - Minecraft has received critical acclaim, winning several awards and being cited as one of the greatest video games of all time; social media, parodies, adaptations, merchandise, and the annual Minecon conventions played prominent roles in popularizing the game. The game has also been used in educational environments to teach chemistry, computer-aided design, and computer science. Several spin-offs have also been made, including Minicraft, Minecraft: Story Mode, Minecraft Earth, Minecraft Dungeons, and Minecraft Legends. In addition, a live-action film based on the game is scheduled for a theatrical release in April 2025.