Python and Displaying Variables

A quick display of the four Python Variable types: strings, integers, floats, and langs with Code.

# variable of type string
name = "Jeffrey Lee"
print("name", name, type(name))

# variable of type integer
age = 17
print("age", age, type(age))

# variable of type float
score = 20.0
print("score", score, type(score))

print()

# variable of type list (many values in one variable)
langs = ["Python", "JavaScript", "Java", "Bash"]
print("langs", langs, type(langs))
print("- langs[3]", langs[3], type(langs[3]))

print()

# variable of type dictionary (a group of keys and values)
person = {
    "name": name,
    "age": age,
    "score": score,
    "langs": langs
}
print("person", person, type(person))
print('- person["name"]', person["name"], type(person["name"]))
name Jeffrey Lee <class 'str'>
age 17 <class 'int'>
score 20.0 <class 'float'>

langs ['Python', 'JavaScript', 'Java', 'Bash'] <class 'list'>
- langs[3] Bash <class 'str'>

person {'name': 'Jeffrey Lee', 'age': 17, 'score': 20.0, 'langs': ['Python', 'JavaScript', 'Java', 'Bash']} <class 'dict'>
- person["name"] Jeffrey Lee <class 'str'>

Lists and Dictionaries

My first construction of lists with my personal info and preferences, along with my friend's information as well. Made using the InfoDb and Append Commands.

InfoDb = []

# Append to List a Dictionary of key/values related to a person and preferences
InfoDb.append({
    "FirstName": "Jeffrey",
    "LastName": "Lee",
    "DOB": "December 27",
    "Residence": "San Diego",
    "Email": "leejeffreysc@gmail.com",
    "Favorite_Food": ["Thai Food"]
})

# Append to List a 2nd Dictionary of key/values
InfoDb.append({
    "FirstName": "Avinh",
    "LastName": "Huynh",
    "DOB": "December 27",
    "Residence": "San Diego",
    "Email": "avinhahuynh@gmail.com",
    "Favorite_Food": ["Vietnamese Food"]
})

# Print the data structure
print(InfoDb)
[{'FirstName': 'Jeffrey', 'LastName': 'Lee', 'DOB': 'December 27', 'Residence': 'San Diego', 'Email': 'leejeffreysc@gmail.com', 'Favorite_Food': ['Thai Food']}, {'FirstName': 'Avinh', 'LastName': 'Huynh', 'DOB': 'December 27', 'Residence': 'San Diego', 'Email': 'avinhahuynh@gmail.com', 'Favorite_Food': ['Vietnamese Food']}]

Formatting my Lists/Dictionaries - for loop command

Using Python, I used formatting alongside the "for loop" command to format my dictionary in a nice, easy to read way.

def print_data(d_rec):
    print(d_rec["FirstName"], d_rec["LastName"])  # using comma puts space between values
    print("\t", "Residence:", d_rec["Residence"]) # \t is a tab indent
    print("\t", "Birth Day:", d_rec["DOB"])
    print("\t", "Favorite Food: ", end="")  # end="" make sure no return occurs
    print(", ".join(d_rec["Favorite_Food"]))  # join allows printing a string list with separator
    print()


# for loop iterates on length of InfoDb
def for_loop():
    print("For loop output\n")
    for record in InfoDb:
        print_data(record)

for_loop()
For loop output

Jeffrey Lee
	 Residence: San Diego
	 Birth Day: December 27
	 Favorite Food: Thai Food

Avinh Huynh
	 Residence: San Diego
	 Birth Day: December 27
	 Favorite Food: Vietnamese Food

Another method of Formatting - while loop

  • The while_loop() function contains a while loop, "while i < len(InfoDb):". This counts through the elements in the list start at zero, and passes the record to print_data()
  • Using Python, I used the "while loop" command to format my dictionary in a numerical order for the output to display.
def while_loop():
    print("While loop output\n")
    i = 0
    while i < len(InfoDb):
        record = InfoDb[i]
        print_data(record)
        i += 1
    return

while_loop()
While loop output

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/home/jeffrey/vscode/firstfastpages/_notebooks/2022-08-29-TP120-python_lists.ipynb Cell 9 in <cell line: 11>()
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jeffrey/vscode/firstfastpages/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=7'>8</a>         i += 1
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jeffrey/vscode/firstfastpages/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=8'>9</a>     return
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jeffrey/vscode/firstfastpages/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=10'>11</a> while_loop()

/home/jeffrey/vscode/firstfastpages/_notebooks/2022-08-29-TP120-python_lists.ipynb Cell 9 in while_loop()
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jeffrey/vscode/firstfastpages/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=2'>3</a> print("While loop output\n")
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jeffrey/vscode/firstfastpages/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=3'>4</a> i = 0
----> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jeffrey/vscode/firstfastpages/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=4'>5</a> while i < len(InfoDb):
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jeffrey/vscode/firstfastpages/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=5'>6</a>     record = InfoDb[i]
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/jeffrey/vscode/firstfastpages/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=6'>7</a>     print_data(record)

NameError: name 'InfoDb' is not defined

The third Formatting technique - recursion

I used the third technique, recursion, in Python to loop my dictionary by calling itself repeatedly.

def recursive_loop(i):
    if i < len(InfoDb):
        record = InfoDb[i]
        print_data(record)
        recursive_loop(i + 1)
    return
    
print("Recursive loop output\n")
recursive_loop(0)
Recursive loop output

Jeffrey Lee
	 Residence: San Diego
	 Birth Day: December 27
	 Favorite Food: Thai Food

Avinh Huynh
	 Residence: San Diego
	 Birth Day: December 27
	 Favorite Food: Vietnamese Food