Jupyter Notebook - Hello World

hello_jupyter

The following Python code is my version of the standard "Hello World" application. Rather than hello for Python, this is hello world for my introduction to Jupyter Notebooks.

Function validate_integer Takes a user input in the form of a string and tries to convert the string to an integer. Returns True if successful, False if not.

In [1]:
def validate_integer(usr_in):
    result = False 
    try:
        n = int(usr_in)
        result = True
    except ValueError:
        result = False
    
    return result

Function validate_float Takes a user input in the form of a string and tries to convert the string to an float. Returns True if successful, False if not.

In [2]:
def validate_float(usr_in):
    result = False 
    try:
        n = float(usr_in)
        result = True
    except ValueError:
        result = False
    
    return result

Function evaluate_string Takes a user input in the form of a string and determines if the string is alphabetic, alphanumeric or something else. The result returned is a description of the string.

In [3]:
def evaluate_string(usr_in):
    result = ""
    
    if usr_in.isalpha():
        result = "alphabetic"
    elif usr_in.isalnum():
        result = "alphanumeric"
    else:
        result = "weird!"
        
    return result

The main entry point in the python program. The programs asks for input once, evaluates the input the and then prints feedback about the input.

In [4]:
print()
print("User Input Examples")
print()

# prompt for and get user input
user_input = input("Enter something:")

result = validate_integer(user_input)
if result == True:
    print("You entered an integer:", user_input)
else:   
    result = validate_float(user_input) 
    if result == True:
        print("You entered a float:", user_input)       
    else:
        print("You entered a string:", user_input) 
        print("and the string was ", evaluate_string(user_input))        
print()
User Input Examples

Enter something:5280.25
You entered a float: 5280.25


Posting a Jupyter Notebook to Blogger is a challenge. I haven't figured out to post the live/active notebook itself but just an HTML version. Even just the HTML version required removing all style and mathjax configuration code. I also removed the top and bottom HTML tags.
The actual Jupyter Notebook file can be found on my GitHub Page
A live viewer for the notebook can be found on the Jupyter Site