Python Variables and Types

# variable of type string
name = "Yasha Khoshini"
print("name", name, type(name))

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

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

print()

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

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 Yasha Khoshini <class 'str'>
age 14 <class 'int'>
score 100.0 <class 'float'>

langs ['Python', 'JavaScript', 'Java', 'Bash', 'HTML'] <class 'list'>
- langs[0] Python <class 'str'>
langs[2]

person {'name': 'Yasha Khoshini', 'age': 14, 'score': 100.0, 'langs': ['Python', 'JavaScript', 'Java', 'Bash', 'HTML']} <class 'dict'>
- person["name"] Yasha Khoshini <class 'str'>

Defining InfoDB

InfoDb = []

# Append to List a Dictionary as a storage area that can be called in future code.
InfoDb.append({
    "FirstName": "yasha",
    "LastName": "khoshini",
    "DOB": "October 5",
    "Residence": "San Diego",
    "Email": "yashakhoshini@icloud.com",
    "Worst EPL Team": "Arsenal",
    "Family Members": ["Kian", "Iyla", "Hedieh", "Reza"]
}) # InfoDb[0]

InfoDb.append({
    "FirstName": "kian",
    "LastName": "khoshini",
    "DOB": "April 14",
    "Residence": "San Diego",
    "Email": "kiankhoshini@icloud.com",
    "Worst EPL Team": "Crystal Palace",
    "Family Members": ["Yasha", "Iyla", "Hedieh", "Reza"]
}) # InfoDb[1]


print()
print()

# Print the data structure
print(InfoDb)

[{'FirstName': 'yasha', 'LastName': 'khoshini', 'DOB': 'October 5', 'Residence': 'San Diego', 'Email': 'yashakhoshini@icloud.com', 'Worst EPL Team': 'Arsenal', 'Family Members': ['Kian', 'Iyla', 'Hedieh', 'Reza']}, {'FirstName': 'kian', 'LastName': 'khoshini', 'DOB': 'April 14', 'Residence': 'San Diego', 'Email': 'kiankhoshini@icloud.com', 'Worst EPL Team': 'Crystal Palace', 'Family Members': ['Yasha', 'Iyla', 'Hedieh', 'Reza']}]

For loop & Printing Data

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", "Family Members:", ", ".join(d_rec["Family Members"]))
    print("\t", "Worst EPL Team:", d_rec["Worst EPL Team"])



# 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

yasha khoshini
	 Residence: San Diego
	 Birth Day: October 5
	 Family Members: Kian, Iyla, Hedieh, Reza
	 Worst EPL Team: Arsenal
kian khoshini
	 Residence: San Diego
	 Birth Day: April 14
	 Family Members: Yasha, Iyla, Hedieh, Reza
	 Worst EPL Team: Crystal Palace

While Loop

# InfoDB is always greater than variable "i" because it proves the statement true or else there will be no output.
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

yasha khoshini
	 Residence: San Diego
	 Birth Day: October 5
	 Family Members: Kian, Iyla, Hedieh, Reza
	 Worst EPL Team: Arsenal
kian khoshini
	 Residence: San Diego
	 Birth Day: April 14
	 Family Members: Yasha, Iyla, Hedieh, Reza
	 Worst EPL Team: Crystal Palace

Recursive Loop

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

yasha khoshini
	 Residence: San Diego
	 Birth Day: October 5
	 Family Members: Kian, Iyla, Hedieh, Reza
	 Worst EPL Team: Arsenal
kian khoshini
	 Residence: San Diego
	 Birth Day: April 14
	 Family Members: Yasha, Iyla, Hedieh, Reza
	 Worst EPL Team: Crystal Palace

For Loop with Index

for index in range(len(InfoDb)):
        print_data(InfoDb[index])
yasha khoshini
	 Residence: San Diego
	 Birth Day: October 5
	 Family Members: Kian, Iyla, Hedieh, Reza
	 Worst EPL Team: Arsenal
kian khoshini
	 Residence: San Diego
	 Birth Day: April 14
	 Family Members: Yasha, Iyla, Hedieh, Reza
	 Worst EPL Team: Crystal Palace

Reversing A List

mylist = [5, 6, 7, 8, 9, 10]
print("before", mylist)

#Telling mylist to reverse
mylist.reverse()

# This is the reversed version
print("after", mylist)
before [5, 6, 7, 8, 9, 10]
after [10, 9, 8, 7, 6, 5]